summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/ext/declarative/api.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2019-01-06 01:14:26 -0500
committermike bayer <mike_mp@zzzcomputing.com>2019-01-06 17:34:50 +0000
commit1e1a38e7801f410f244e4bbb44ec795ae152e04e (patch)
tree28e725c5c8188bd0cfd133d1e268dbca9b524978 /lib/sqlalchemy/ext/declarative/api.py
parent404e69426b05a82d905cbb3ad33adafccddb00dd (diff)
downloadsqlalchemy-1e1a38e7801f410f244e4bbb44ec795ae152e04e.tar.gz
Run black -l 79 against all source files
This is a straight reformat run using black as is, with no edits applied at all. The black run will format code consistently, however in some cases that are prevalent in SQLAlchemy code it produces too-long lines. The too-long lines will be resolved in the following commit that will resolve all remaining flake8 issues including shadowed builtins, long lines, import order, unused imports, duplicate imports, and docstring issues. Change-Id: I7eda77fed3d8e73df84b3651fd6cfcfe858d4dc9
Diffstat (limited to 'lib/sqlalchemy/ext/declarative/api.py')
-rw-r--r--lib/sqlalchemy/ext/declarative/api.py117
1 files changed, 75 insertions, 42 deletions
diff --git a/lib/sqlalchemy/ext/declarative/api.py b/lib/sqlalchemy/ext/declarative/api.py
index 865cd16f0..987e92119 100644
--- a/lib/sqlalchemy/ext/declarative/api.py
+++ b/lib/sqlalchemy/ext/declarative/api.py
@@ -8,9 +8,13 @@
from ...schema import Table, MetaData, Column
-from ...orm import synonym as _orm_synonym, \
- comparable_property,\
- interfaces, properties, attributes
+from ...orm import (
+ synonym as _orm_synonym,
+ comparable_property,
+ interfaces,
+ properties,
+ attributes,
+)
from ...orm.util import polymorphic_union
from ...orm.base import _mapper_or_none
from ...util import OrderedDict, hybridmethod, hybridproperty
@@ -19,9 +23,13 @@ from ... import exc
import weakref
import re
-from .base import _as_declarative, \
- _declarative_constructor,\
- _DeferredMapperConfig, _add_attribute, _del_attribute
+from .base import (
+ _as_declarative,
+ _declarative_constructor,
+ _DeferredMapperConfig,
+ _add_attribute,
+ _del_attribute,
+)
from .clsregistry import _class_resolver
@@ -31,10 +39,10 @@ def instrument_declarative(cls, registry, metadata):
MetaData object.
"""
- if '_decl_class_registry' in cls.__dict__:
+ if "_decl_class_registry" in cls.__dict__:
raise exc.InvalidRequestError(
- "Class %r already has been "
- "instrumented declaratively" % cls)
+ "Class %r already has been " "instrumented declaratively" % cls
+ )
cls._decl_class_registry = registry
cls.metadata = metadata
_as_declarative(cls, cls.__name__, cls.__dict__)
@@ -54,14 +62,14 @@ def has_inherited_table(cls):
"""
for class_ in cls.__mro__[1:]:
- if getattr(class_, '__table__', None) is not None:
+ if getattr(class_, "__table__", None) is not None:
return True
return False
class DeclarativeMeta(type):
def __init__(cls, classname, bases, dict_):
- if '_decl_class_registry' not in cls.__dict__:
+ if "_decl_class_registry" not in cls.__dict__:
_as_declarative(cls, classname, cls.__dict__)
type.__init__(cls, classname, bases, dict_)
@@ -71,6 +79,7 @@ class DeclarativeMeta(type):
def __delattr__(cls, key):
_del_attribute(cls, key)
+
def synonym_for(name, map_column=False):
"""Decorator that produces an :func:`.orm.synonym` attribute in conjunction
with a Python descriptor.
@@ -104,8 +113,10 @@ def synonym_for(name, map_column=False):
can be achieved with synonyms.
"""
+
def decorate(fn):
return _orm_synonym(name, map_column=map_column, descriptor=fn)
+
return decorate
@@ -127,8 +138,10 @@ def comparable_using(comparator_factory):
prop = comparable_property(MyComparatorType)
"""
+
def decorate(fn):
return comparable_property(comparator_factory, fn)
+
return decorate
@@ -190,14 +203,16 @@ class declared_attr(interfaces._MappedAttribute, property):
self._cascading = cascading
def __get__(desc, self, cls):
- reg = cls.__dict__.get('_sa_declared_attr_reg', None)
+ reg = cls.__dict__.get("_sa_declared_attr_reg", None)
if reg is None:
- if not re.match(r'^__.+__$', desc.fget.__name__) and \
- attributes.manager_of_class(cls) is None:
+ if (
+ not re.match(r"^__.+__$", desc.fget.__name__)
+ and attributes.manager_of_class(cls) is None
+ ):
util.warn(
"Unmanaged access of declarative attribute %s from "
- "non-mapped class %s" %
- (desc.fget.__name__, cls.__name__))
+ "non-mapped class %s" % (desc.fget.__name__, cls.__name__)
+ )
return desc.fget(cls)
elif desc in reg:
return reg[desc]
@@ -283,10 +298,16 @@ class _stateful_declared_attr(declared_attr):
return declared_attr(fn, **self.kw)
-def declarative_base(bind=None, metadata=None, mapper=None, cls=object,
- name='Base', constructor=_declarative_constructor,
- class_registry=None,
- metaclass=DeclarativeMeta):
+def declarative_base(
+ bind=None,
+ metadata=None,
+ mapper=None,
+ cls=object,
+ name="Base",
+ constructor=_declarative_constructor,
+ class_registry=None,
+ metaclass=DeclarativeMeta,
+):
r"""Construct a base class for declarative class definitions.
The new base class will be given a metaclass that produces
@@ -357,16 +378,17 @@ def declarative_base(bind=None, metadata=None, mapper=None, cls=object,
class_registry = weakref.WeakValueDictionary()
bases = not isinstance(cls, tuple) and (cls,) or cls
- class_dict = dict(_decl_class_registry=class_registry,
- metadata=lcl_metadata)
+ class_dict = dict(
+ _decl_class_registry=class_registry, metadata=lcl_metadata
+ )
if isinstance(cls, type):
- class_dict['__doc__'] = cls.__doc__
+ class_dict["__doc__"] = cls.__doc__
if constructor:
- class_dict['__init__'] = constructor
+ class_dict["__init__"] = constructor
if mapper:
- class_dict['__mapper_cls__'] = mapper
+ class_dict["__mapper_cls__"] = mapper
return metaclass(name, bases, class_dict)
@@ -401,9 +423,10 @@ def as_declarative(**kw):
:func:`.declarative_base`
"""
+
def decorate(cls):
- kw['cls'] = cls
- kw['name'] = cls.__name__
+ kw["cls"] = cls
+ kw["name"] = cls.__name__
return declarative_base(**kw)
return decorate
@@ -456,10 +479,13 @@ class ConcreteBase(object):
@classmethod
def _create_polymorphic_union(cls, mappers):
- return polymorphic_union(OrderedDict(
- (mp.polymorphic_identity, mp.local_table)
- for mp in mappers
- ), 'type', 'pjoin')
+ return polymorphic_union(
+ OrderedDict(
+ (mp.polymorphic_identity, mp.local_table) for mp in mappers
+ ),
+ "type",
+ "pjoin",
+ )
@classmethod
def __declare_first__(cls):
@@ -568,7 +594,7 @@ class AbstractConcreteBase(ConcreteBase):
@classmethod
def _sa_decl_prepare_nocascade(cls):
- if getattr(cls, '__mapper__', None):
+ if getattr(cls, "__mapper__", None):
return
to_map = _DeferredMapperConfig.config_for_cls(cls)
@@ -604,8 +630,9 @@ class AbstractConcreteBase(ConcreteBase):
def mapper_args():
args = m_args()
- args['polymorphic_on'] = pjoin.c.type
+ args["polymorphic_on"] = pjoin.c.type
return args
+
to_map.mapper_args_fn = mapper_args
m = to_map.map()
@@ -684,6 +711,7 @@ class DeferredReflection(object):
.. versionadded:: 0.8
"""
+
@classmethod
def prepare(cls, engine):
"""Reflect all :class:`.Table` objects for all current
@@ -696,8 +724,10 @@ class DeferredReflection(object):
mapper = thingy.cls.__mapper__
metadata = mapper.class_.metadata
for rel in mapper._props.values():
- if isinstance(rel, properties.RelationshipProperty) and \
- rel.secondary is not None:
+ if (
+ isinstance(rel, properties.RelationshipProperty)
+ and rel.secondary is not None
+ ):
if isinstance(rel.secondary, Table):
cls._reflect_table(rel.secondary, engine)
elif isinstance(rel.secondary, _class_resolver):
@@ -711,6 +741,7 @@ class DeferredReflection(object):
t1 = Table(key, metadata)
cls._reflect_table(t1, engine)
return t1
+
return _resolve
@classmethod
@@ -724,10 +755,12 @@ class DeferredReflection(object):
@classmethod
def _reflect_table(cls, table, engine):
- Table(table.name,
- table.metadata,
- extend_existing=True,
- autoload_replace=False,
- autoload=True,
- autoload_with=engine,
- schema=table.schema)
+ Table(
+ table.name,
+ table.metadata,
+ extend_existing=True,
+ autoload_replace=False,
+ autoload=True,
+ autoload_with=engine,
+ schema=table.schema,
+ )