-
Notifications
You must be signed in to change notification settings - Fork 7
Consolidate package config to pyproject.toml (Sourcery refactored) #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: pyproject
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,3 @@ | ||
| from .__version__ import ( | ||
| __author__, | ||
| __author_email__, | ||
| __description__, | ||
| __version__, | ||
| ) | ||
| __version__ = "0.12.0" | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "__author__", | ||
| "__author_email__", | ||
| "__description__", | ||
| "__version__", | ||
| ] | ||
| VERSION = tuple(__version__.split(".")) |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,5 +141,4 @@ | |
| from .sqlalchemy import SQLAlchemy # noqa | ||
| from .utils import dbdict | ||
|
|
||
|
|
||
| databases = dbdict() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,6 @@ | |
|
|
||
| from .url import DIALECT_MAP_TO_DJANGO | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "BigIntegerField", | ||
| "BinaryField", | ||
|
|
@@ -46,11 +45,8 @@ class Field(sa.Column): | |
| def __init__(self, *args, **kwargs): | ||
| self.db = kwargs.pop("db", None) | ||
|
|
||
| name = None | ||
| args = list(args) | ||
| if args and isinstance(args[0], str): | ||
| name = args.pop(0) | ||
|
|
||
| name = args.pop(0) if args and isinstance(args[0], str) else None | ||
| column_type = kwargs.pop("type_", None) | ||
| if args and hasattr(args[0], "_sqla_type"): | ||
| column_type = args.pop(0) | ||
|
|
@@ -267,11 +263,14 @@ def get_django_dialect_ranges(self): | |
| ops = operations.BaseDatabaseOperations | ||
| with suppress(ImportError): | ||
| ops = ( | ||
| import_string(DIALECT_MAP_TO_DJANGO.get(self.db.url.get_dialect().name) + ".base.DatabaseOperations") | ||
| import_string( | ||
| f"{DIALECT_MAP_TO_DJANGO.get(self.db.url.get_dialect().name)}.base.DatabaseOperations" | ||
| ) | ||
| if self.db | ||
| else operations.BaseDatabaseOperations | ||
| ) | ||
|
|
||
|
|
||
|
||
| return ops.integer_field_ranges | ||
|
|
||
| def get_dialect_range(self): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,8 @@ | |
| import enum | ||
| from contextlib import suppress | ||
|
|
||
| import sqlalchemy as sa | ||
| from dateutil.parser import parse | ||
| import sqlalchemy as sa | ||
| from django import forms as djangoforms | ||
| from django.conf import settings | ||
| from django.core import validators as djangovalidators | ||
|
|
@@ -166,7 +166,7 @@ def clean(self, value, instance): | |
|
|
||
| def validate(self, value, instance): | ||
| """Validate value and raise ValidationError if necessary.""" | ||
| getattr(instance, "clean_" + self.name, bool)() | ||
| getattr(instance, f"clean_{self.name}", bool)() | ||
|
||
|
|
||
| def run_validators(self, value): | ||
| """Run field's validators and raise ValidationError if necessary.""" | ||
|
|
@@ -194,9 +194,7 @@ def __init__(self, column, prop=None, parent=None, name=None): | |
| self.field_kwargs["max_length"] = self.column.type.length | ||
|
|
||
| def to_python(self, value): | ||
| if value is None: | ||
| return value | ||
| return str(value).strip() | ||
| return value if value is None else str(value).strip() | ||
|
||
|
|
||
|
|
||
| class text_column_info(string_column_info): | ||
|
|
@@ -324,9 +322,7 @@ def to_python(self, value): | |
| return bool(value) | ||
| if value in ("t", "T"): | ||
| return True | ||
| if value in ("f", "F"): | ||
| return False | ||
| return self.coercer.to_python(value) | ||
| return False if value in ("f", "F") else self.coercer.to_python(value) | ||
|
||
|
|
||
|
|
||
| class date_column_info(column_info): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| """Metadata for sqlalchemy models.""" | ||
| from collections import OrderedDict, namedtuple | ||
| from collections import OrderedDict | ||
| from collections import namedtuple | ||
| from functools import partial | ||
| from itertools import chain | ||
|
|
||
| import inflect | ||
| import sqlalchemy as sa | ||
| from django.apps import apps | ||
| from django.core.exceptions import FieldDoesNotExist, ValidationError | ||
| from django.core.exceptions import FieldDoesNotExist | ||
| from django.core.exceptions import ValidationError | ||
|
|
||
| from ...exceptions import NestedValidationError | ||
| from ...validators import ValidationRunner | ||
|
|
@@ -15,7 +17,6 @@ | |
| from .composite import composite_info | ||
| from .relations import relation_info | ||
|
|
||
|
|
||
| Identity = namedtuple("Key", ["model", "pk"]) | ||
| inflector = inflect.engine() | ||
|
|
||
|
|
@@ -75,8 +76,8 @@ def __init__(self, model): | |
| app_config = apps.get_containing_app_config(self.model_class.__module__) | ||
| self.app_label = getattr(app_config, "label", None) or "django_sorcery" | ||
|
|
||
| self.label = "{}.{}".format(self.app_label, self.object_name) | ||
| self.label_lower = "{}.{}".format(self.app_label, self.model_name) | ||
| self.label = f"{self.app_label}.{self.object_name}" | ||
| self.label_lower = f"{self.app_label}.{self.model_name}" | ||
|
||
|
|
||
| sa.event.listen(self.mapper, "mapper_configured", self._init) | ||
| self._init(self.mapper, self.model_class) | ||
|
|
@@ -147,10 +148,16 @@ def __getattr__(self, name): | |
|
|
||
| def __repr__(self): | ||
| reprs = ["<model_info({!s})>".format(self.model_class.__name__)] | ||
| reprs.extend(" " + repr(i) for i in self.primary_keys.values()) | ||
| reprs.extend(" " + repr(i) for _, i in sorted(self.properties.items())) | ||
| reprs.extend(" " + i for i in chain(*[repr(c).split("\n") for _, c in sorted(self.composites.items())])) | ||
| reprs.extend(" " + repr(i) for _, i in sorted(self.relationships.items())) | ||
| reprs.extend(f" {repr(i)}" for i in self.primary_keys.values()) | ||
| reprs.extend(f" {repr(i)}" for _, i in sorted(self.properties.items())) | ||
| reprs.extend( | ||
| f" {i}" | ||
| for i in chain( | ||
| *[repr(c).split("\n") for _, c in sorted(self.composites.items())] | ||
| ) | ||
| ) | ||
|
|
||
| reprs.extend(f" {repr(i)}" for _, i in sorted(self.relationships.items())) | ||
|
||
| return "\n".join(reprs) | ||
|
|
||
| @property | ||
|
|
@@ -185,10 +192,7 @@ def primary_keys_from_dict(self, kwargs): | |
| if any(pk is None for pk in pks): | ||
| return None | ||
|
|
||
| if len(pks) < 2: | ||
| return next(iter(pks), None) | ||
|
|
||
| return tuple(pks) | ||
| return next(iter(pks), None) if len(pks) < 2 else tuple(pks) | ||
|
||
|
|
||
| def primary_keys_from_instance(self, instance): | ||
| """Return a dict containing the primary keys of the ``instance``""" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,8 +55,10 @@ def __init__(self, relationship): | |
| self.local_remote_pairs_for_identity_key = [] | ||
| try: | ||
| # ensure local_remote pairs are of same order as remote pk | ||
| for i in target_pk: | ||
| self.local_remote_pairs_for_identity_key.append((pairs[i], i)) | ||
| self.local_remote_pairs_for_identity_key.extend( | ||
| (pairs[i], i) for i in target_pk | ||
| ) | ||
|
|
||
|
||
| except KeyError: | ||
| # if relation is missing one of related pk columns | ||
| # but actual constraint has it defined | ||
|
|
@@ -78,8 +80,10 @@ def __init__(self, relationship): | |
|
|
||
| if len(matching_constraints) == 1: | ||
| pairs = {i.column: i.parent for i in matching_constraints[0].elements} | ||
| for i in target_pk: | ||
| self.local_remote_pairs_for_identity_key.append((pairs[i], i)) | ||
| self.local_remote_pairs_for_identity_key.extend( | ||
| (pairs[i], i) for i in target_pk | ||
| ) | ||
|
|
||
| else: | ||
| # if everything fails, return default pairs | ||
| self.local_remote_pairs_for_identity_key = self.local_remote_pairs[:] | ||
|
|
@@ -95,12 +99,10 @@ def formfield(self, form_class=None, **kwargs): | |
| return form_class(self.related_model, **field_kwargs) | ||
|
|
||
| def get_form_class(self): | ||
| from ...fields import ModelChoiceField, ModelMultipleChoiceField | ||
|
|
||
| if self.uselist: | ||
| return ModelMultipleChoiceField | ||
| from ...fields import ModelChoiceField | ||
| from ...fields import ModelMultipleChoiceField | ||
|
|
||
| return ModelChoiceField | ||
| return ModelMultipleChoiceField if self.uselist else ModelChoiceField | ||
|
|
||
| def __repr__(self): | ||
| return "<relation_info({}.{})>".format(self.parent_model.__name__, self.name) | ||
| return f"<relation_info({self.parent_model.__name__}.{self.name})>" | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,13 @@ | |
| import sqlalchemy as sa | ||
| import sqlalchemy.ext.declarative # noqa | ||
| import sqlalchemy.orm # noqa | ||
| from sqlalchemy.orm.base import MANYTOONE | ||
| from sqlalchemy.orm.base import NO_VALUE | ||
| from django.core.exceptions import ValidationError | ||
| from django.utils.text import camel_case_to_spaces | ||
| from sqlalchemy.orm.base import MANYTOONE, NO_VALUE | ||
|
|
||
| from . import meta, signals | ||
| from . import meta | ||
| from . import signals | ||
| from .mixins import CleanMixin | ||
|
|
||
|
|
||
|
|
@@ -231,9 +233,9 @@ class BaseMeta(sqlalchemy.ext.declarative.DeclarativeMeta): | |
| """Base metaclass for models which registers models to DB model registry | ||
| when models are created.""" | ||
|
|
||
| def __new__(mcs, name, bases, attrs): | ||
| klass = super().__new__(mcs, name, bases, attrs) | ||
| mcs.db.models_registry.append(klass) | ||
| def __new__(cls, name, bases, attrs): | ||
| klass = super().__new__(cls, name, bases, attrs) | ||
| cls.db.models_registry.append(klass) | ||
|
||
| return klass | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,14 @@ | ||
| """sqlalchemy profiling things.""" | ||
| import logging | ||
| import time | ||
| from collections import defaultdict, namedtuple | ||
| from collections import defaultdict | ||
| from collections import namedtuple | ||
| from functools import partial | ||
| from threading import local | ||
|
|
||
| import sqlalchemy as sa | ||
| from django.conf import settings | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
| STATEMENT_TYPES = {"SELECT": "select", "INSERT INTO": "insert", "UPDATE": "update", "DELETE": "delete"} | ||
|
|
||
|
|
@@ -190,17 +190,20 @@ def process_response(self, request, response): | |
| try: | ||
| stats = self.profiler.stats | ||
| if stats["duration"] or self.log_results: | ||
| self.log(**{"sa_{}".format(k): v for k, v in stats.items()}) | ||
| self.log(**{f"sa_{k}": v for k, v in stats.items()}) | ||
| except Exception: # pragma: nocover | ||
| # The show must go on... | ||
| pass # pragma: nocover | ||
| else: | ||
| if self.header_results: | ||
| for k, v in stats.items(): | ||
| response["X-SA-{}".format("".join(i.title() for i in k.split("_")))] = v | ||
| response[f'X-SA-{"".join(i.title() for i in k.split("_"))}'] = v | ||
|
||
| self.profiler.clear() | ||
| return response | ||
|
|
||
| def log(self, **kwargs): | ||
| """Log sqlalchemy stats for current request.""" | ||
| self.logger.info("SQLAlchemy profiler %s", " ".join("{}={}".format(k, v) for k, v in kwargs.items())) | ||
| self.logger.info( | ||
| "SQLAlchemy profiler %s", | ||
| " ".join(f"{k}={v}" for k, v in kwargs.items()), | ||
| ) | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
Field.__init__refactored with the following changes:assign-if-exp)elsebranch (introduce-default-else)move-assign-in-block)