diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-01-06 01:14:26 -0500 |
|---|---|---|
| committer | mike bayer <mike_mp@zzzcomputing.com> | 2019-01-06 17:34:50 +0000 |
| commit | 1e1a38e7801f410f244e4bbb44ec795ae152e04e (patch) | |
| tree | 28e725c5c8188bd0cfd133d1e268dbca9b524978 /lib/sqlalchemy/orm/strategy_options.py | |
| parent | 404e69426b05a82d905cbb3ad33adafccddb00dd (diff) | |
| download | sqlalchemy-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/orm/strategy_options.py')
| -rw-r--r-- | lib/sqlalchemy/orm/strategy_options.py | 310 |
1 files changed, 173 insertions, 137 deletions
diff --git a/lib/sqlalchemy/orm/strategy_options.py b/lib/sqlalchemy/orm/strategy_options.py index f0d209110..b2f6bcb11 100644 --- a/lib/sqlalchemy/orm/strategy_options.py +++ b/lib/sqlalchemy/orm/strategy_options.py @@ -13,11 +13,19 @@ from .attributes import QueryableAttribute from .. import util from ..sql.base import _generative, Generative from .. import exc as sa_exc, inspect -from .base import _is_aliased_class, _class_to_mapper, _is_mapped_class, \ - InspectionAttr +from .base import ( + _is_aliased_class, + _class_to_mapper, + _is_mapped_class, + InspectionAttr, +) from . import util as orm_util -from .path_registry import PathRegistry, TokenRegistry, \ - _WILDCARD_TOKEN, _DEFAULT_TOKEN +from .path_registry import ( + PathRegistry, + TokenRegistry, + _WILDCARD_TOKEN, + _DEFAULT_TOKEN, +) class Load(Generative, MapperOption): @@ -94,12 +102,14 @@ class Load(Generative, MapperOption): if ( # means loader_path and path are unrelated, # this does not need to be part of a cache key - chopped is None + chopped + is None ) or ( # means no additional path with loader_path + path # and the endpoint isn't using of_type so isn't modified # into an alias or other unsafe entity - not chopped and not obj._of_type + not chopped + and not obj._of_type ): continue @@ -124,12 +134,18 @@ class Load(Generative, MapperOption): serialized.append( ( - tuple(serialized_path) + - (obj.strategy or ()) + - (tuple([ - (key, obj.local_opts[key]) - for key in sorted(obj.local_opts) - ]) if obj.local_opts else ()) + tuple(serialized_path) + + (obj.strategy or ()) + + ( + tuple( + [ + (key, obj.local_opts[key]) + for key in sorted(obj.local_opts) + ] + ) + if obj.local_opts + else () + ) ) ) if not serialized: @@ -170,12 +186,13 @@ class Load(Generative, MapperOption): if raiseerr and not path.has_entity: if isinstance(path, TokenRegistry): raise sa_exc.ArgumentError( - "Wildcard token cannot be followed by another entity") + "Wildcard token cannot be followed by another entity" + ) else: raise sa_exc.ArgumentError( "Attribute '%s' of entity '%s' does not " - "refer to a mapped entity" % - (path.prop.key, path.parent.entity) + "refer to a mapped entity" + % (path.prop.key, path.parent.entity) ) if isinstance(attr, util.string_types): @@ -201,8 +218,7 @@ class Load(Generative, MapperOption): if raiseerr: raise sa_exc.ArgumentError( "Can't find property named '%s' on the " - "mapped entity %s in this Query. " % ( - attr, ent) + "mapped entity %s in this Query. " % (attr, ent) ) else: return None @@ -215,7 +231,8 @@ class Load(Generative, MapperOption): if raiseerr: raise sa_exc.ArgumentError( "Attribute '%s' does not " - "link from element '%s'" % (attr, path.entity)) + "link from element '%s'" % (attr, path.entity) + ) else: return None else: @@ -225,22 +242,26 @@ class Load(Generative, MapperOption): if raiseerr: raise sa_exc.ArgumentError( "Attribute '%s' does not " - "link from element '%s'" % (attr, path.entity)) + "link from element '%s'" % (attr, path.entity) + ) else: return None - if getattr(attr, '_of_type', None): + if getattr(attr, "_of_type", None): ac = attr._of_type ext_info = of_type_info = inspect(ac) existing = path.entity_path[prop].get( - self.context, "path_with_polymorphic") + self.context, "path_with_polymorphic" + ) if not ext_info.is_aliased_class: ac = orm_util.with_polymorphic( ext_info.mapper.base_mapper, - ext_info.mapper, aliased=True, + ext_info.mapper, + aliased=True, _use_mapper_path=True, - _existing_alias=existing) + _existing_alias=existing, + ) ext_info = inspect(ac) elif not ext_info.with_polymorphic_mappers: ext_info = orm_util.AliasedInsp( @@ -253,11 +274,12 @@ class Load(Generative, MapperOption): ext_info._base_alias, ext_info._use_mapper_path, ext_info._adapt_on_names, - ext_info.represents_outer_join + ext_info.represents_outer_join, ) path.entity_path[prop].set( - self.context, "path_with_polymorphic", ext_info) + self.context, "path_with_polymorphic", ext_info + ) # the path here will go into the context dictionary and # needs to match up to how the class graph is traversed. @@ -280,7 +302,7 @@ class Load(Generative, MapperOption): return path def __str__(self): - return "Load(strategy=%r)" % (self.strategy, ) + return "Load(strategy=%r)" % (self.strategy,) def _coerce_strat(self, strategy): if strategy is not None: @@ -289,7 +311,8 @@ class Load(Generative, MapperOption): @_generative def set_relationship_strategy( - self, attr, strategy, propagate_to_loaders=True): + self, attr, strategy, propagate_to_loaders=True + ): strategy = self._coerce_strat(strategy) self.is_class_strategy = False @@ -365,12 +388,18 @@ class Load(Generative, MapperOption): if effective_path.is_token: for path in effective_path.generate_for_superclasses(): self._set_for_path( - self.context, path, replace=True, - merge_opts=self.is_opts_only) + self.context, + path, + replace=True, + merge_opts=self.is_opts_only, + ) else: self._set_for_path( - self.context, effective_path, replace=True, - merge_opts=self.is_opts_only) + self.context, + effective_path, + replace=True, + merge_opts=self.is_opts_only, + ) def __getstate__(self): d = self.__dict__.copy() @@ -389,21 +418,26 @@ class Load(Generative, MapperOption): # TODO: this is approximated from the _UnboundLoad # version and probably has issues, not fully covered. - if i == 0 and c_token.endswith(':' + _DEFAULT_TOKEN): + if i == 0 and c_token.endswith(":" + _DEFAULT_TOKEN): return to_chop - elif c_token != 'relationship:%s' % (_WILDCARD_TOKEN,) and \ - c_token != p_token.key: + elif ( + c_token != "relationship:%s" % (_WILDCARD_TOKEN,) + and c_token != p_token.key + ): return None if c_token is p_token: continue - elif isinstance(c_token, InspectionAttr) and \ - c_token.is_mapper and p_token.is_mapper and \ - c_token.isa(p_token): + elif ( + isinstance(c_token, InspectionAttr) + and c_token.is_mapper + and p_token.is_mapper + and c_token.isa(p_token) + ): continue else: return None - return to_chop[i + 1:] + return to_chop[i + 1 :] class _UnboundLoad(Load): @@ -431,9 +465,7 @@ class _UnboundLoad(Load): if local_elem is not val_elem: break else: - opt = val._bind_loader( - [path.path[0]], - None, None, False) + opt = val._bind_loader([path.path[0]], None, None, False) if opt: c_key = opt._generate_cache_key(path) if c_key is False: @@ -449,26 +481,29 @@ class _UnboundLoad(Load): self._to_bind.append(self) def _generate_path(self, path, attr, wildcard_key): - if wildcard_key and isinstance(attr, util.string_types) and \ - attr in (_WILDCARD_TOKEN, _DEFAULT_TOKEN): + if ( + wildcard_key + and isinstance(attr, util.string_types) + and attr in (_WILDCARD_TOKEN, _DEFAULT_TOKEN) + ): if attr == _DEFAULT_TOKEN: self.propagate_to_loaders = False attr = "%s:%s" % (wildcard_key, attr) if path and _is_mapped_class(path[-1]) and not self.is_class_strategy: path = path[0:-1] if attr: - path = path + (attr, ) + path = path + (attr,) self.path = path return path def __getstate__(self): d = self.__dict__.copy() - d['path'] = self._serialize_path(self.path, filter_aliased_class=True) + d["path"] = self._serialize_path(self.path, filter_aliased_class=True) return d def __setstate__(self, state): ret = [] - for key in state['path']: + for key in state["path"]: if isinstance(key, tuple): if len(key) == 2: # support legacy @@ -482,17 +517,20 @@ class _UnboundLoad(Load): ret.append(prop) else: ret.append(key) - state['path'] = tuple(ret) + state["path"] = tuple(ret) self.__dict__ = state def _process(self, query, raiseerr): - dedupes = query._attributes['_unbound_load_dedupes'] + dedupes = query._attributes["_unbound_load_dedupes"] for val in self._to_bind: if val not in dedupes: dedupes.add(val) val._bind_loader( [ent.entity_zero for ent in query._mapper_entities], - query._current_path, query._attributes, raiseerr) + query._current_path, + query._attributes, + raiseerr, + ) @classmethod def _from_keys(cls, meth, keys, chained, kw): @@ -502,13 +540,14 @@ class _UnboundLoad(Load): if isinstance(key, util.string_types): # coerce fooload('*') into "default loader strategy" if key == _WILDCARD_TOKEN: - return (_DEFAULT_TOKEN, ) + return (_DEFAULT_TOKEN,) # coerce fooload(".*") into "wildcard on default entity" elif key.startswith("." + _WILDCARD_TOKEN): key = key[1:] return key.split(".") else: return (key,) + all_tokens = [token for key in keys for token in _split_key(key)] for token in all_tokens[0:-1]: @@ -526,21 +565,24 @@ class _UnboundLoad(Load): def _chop_path(self, to_chop, path): i = -1 for i, (c_token, (p_entity, p_prop)) in enumerate( - zip(to_chop, path.pairs())): + zip(to_chop, path.pairs()) + ): if isinstance(c_token, util.string_types): - if i == 0 and c_token.endswith(':' + _DEFAULT_TOKEN): + if i == 0 and c_token.endswith(":" + _DEFAULT_TOKEN): return to_chop - elif c_token != 'relationship:%s' % ( - _WILDCARD_TOKEN,) and c_token != p_prop.key: + elif ( + c_token != "relationship:%s" % (_WILDCARD_TOKEN,) + and c_token != p_prop.key + ): return None elif isinstance(c_token, PropComparator): - if c_token.property is not p_prop or \ - ( - c_token._parententity is not p_entity and ( - not c_token._parententity.is_mapper or - not c_token._parententity.isa(p_entity) - ) - ): + if c_token.property is not p_prop or ( + c_token._parententity is not p_entity + and ( + not c_token._parententity.is_mapper + or not c_token._parententity.isa(p_entity) + ) + ): return None else: i += 1 @@ -551,15 +593,16 @@ class _UnboundLoad(Load): ret = [] for token in path: if isinstance(token, QueryableAttribute): - if filter_aliased_class and token._of_type and \ - inspect(token._of_type).is_aliased_class: - ret.append( - (token._parentmapper.class_, - token.key, None)) + if ( + filter_aliased_class + and token._of_type + and inspect(token._of_type).is_aliased_class + ): + ret.append((token._parentmapper.class_, token.key, None)) else: ret.append( - (token._parentmapper.class_, token.key, - token._of_type)) + (token._parentmapper.class_, token.key, token._of_type) + ) elif isinstance(token, PropComparator): ret.append((token._parentmapper.class_, token.key, None)) else: @@ -605,7 +648,7 @@ class _UnboundLoad(Load): start_path = self.path if self.is_class_strategy and current_path: - start_path += (entities[0], ) + start_path += (entities[0],) # _current_path implies we're in a # secondary load with an existing path @@ -621,23 +664,20 @@ class _UnboundLoad(Load): token = start_path[0] if isinstance(token, util.string_types): - entity = self._find_entity_basestring( - entities, token, raiseerr) + entity = self._find_entity_basestring(entities, token, raiseerr) elif isinstance(token, PropComparator): prop = token.property entity = self._find_entity_prop_comparator( - entities, - prop.key, - token._parententity, - raiseerr) + entities, prop.key, token._parententity, raiseerr + ) elif self.is_class_strategy and _is_mapped_class(token): entity = inspect(token) if entity not in entities: entity = None else: raise sa_exc.ArgumentError( - "mapper option expects " - "string key or list of attributes") + "mapper option expects " "string key or list of attributes" + ) if not entity: return @@ -663,7 +703,8 @@ class _UnboundLoad(Load): if not loader.is_class_strategy: for token in start_path: if not loader._generate_path( - loader.path, token, None, raiseerr): + loader.path, token, None, raiseerr + ): return loader.local_opts.update(self.local_opts) @@ -680,14 +721,18 @@ class _UnboundLoad(Load): if effective_path.is_token: for path in effective_path.generate_for_superclasses(): loader._set_for_path( - context, path, + context, + path, replace=not self._is_chain_link, - merge_opts=self.is_opts_only) + merge_opts=self.is_opts_only, + ) else: loader._set_for_path( - context, effective_path, + context, + effective_path, replace=not self._is_chain_link, - merge_opts=self.is_opts_only) + merge_opts=self.is_opts_only, + ) return loader @@ -704,28 +749,27 @@ class _UnboundLoad(Load): if not list(entities): raise sa_exc.ArgumentError( "Query has only expression-based entities - " - "can't find property named '%s'." - % (token, ) + "can't find property named '%s'." % (token,) ) else: raise sa_exc.ArgumentError( "Can't find property '%s' on any entity " "specified in this Query. Note the full path " "from root (%s) to target entity must be specified." - % (token, ",".join(str(x) for - x in entities)) + % (token, ",".join(str(x) for x in entities)) ) else: return None def _find_entity_basestring(self, entities, token, raiseerr): - if token.endswith(':' + _WILDCARD_TOKEN): + if token.endswith(":" + _WILDCARD_TOKEN): if len(list(entities)) != 1: if raiseerr: raise sa_exc.ArgumentError( "Wildcard loader can only be used with exactly " "one entity. Use Load(ent) to specify " - "specific entities.") + "specific entities." + ) elif token.endswith(_DEFAULT_TOKEN): raiseerr = False @@ -738,8 +782,7 @@ class _UnboundLoad(Load): if raiseerr: raise sa_exc.ArgumentError( "Query has only expression-based entities - " - "can't find property named '%s'." - % (token, ) + "can't find property named '%s'." % (token,) ) else: return None @@ -766,7 +809,9 @@ class loader_option(object): See :func:`.orm.%(name)s` for usage examples. -""" % {"name": self.name} +""" % { + "name": self.name + } fn.__doc__ = fn_doc return self @@ -783,7 +828,9 @@ See :func:`.orm.%(name)s` for usage examples. %(name)s("someattribute").%(name)s("anotherattribute") ) -""" % {"name": self.name} +""" % { + "name": self.name + } return self @@ -840,23 +887,22 @@ def contains_eager(loadopt, attr, alias=None): info = inspect(alias) alias = info.selectable - elif getattr(attr, '_of_type', None): + elif getattr(attr, "_of_type", None): ot = inspect(attr._of_type) alias = ot.selectable cloned = loadopt.set_relationship_strategy( - attr, - {"lazy": "joined"}, - propagate_to_loaders=False + attr, {"lazy": "joined"}, propagate_to_loaders=False ) - cloned.local_opts['eager_from_alias'] = alias + cloned.local_opts["eager_from_alias"] = alias return cloned @contains_eager._add_unbound_fn def contains_eager(*keys, **kw): return _UnboundLoad()._from_keys( - _UnboundLoad.contains_eager, keys, True, kw) + _UnboundLoad.contains_eager, keys, True, kw + ) @loader_option() @@ -894,12 +940,11 @@ def load_only(loadopt, *attrs): """ cloned = loadopt.set_column_strategy( - attrs, - {"deferred": False, "instrument": True} + attrs, {"deferred": False, "instrument": True} + ) + cloned.set_column_strategy( + "*", {"deferred": True, "instrument": True}, {"undefer_pks": True} ) - cloned.set_column_strategy("*", - {"deferred": True, "instrument": True}, - {"undefer_pks": True}) return cloned @@ -996,20 +1041,18 @@ def joinedload(loadopt, attr, innerjoin=None): """ loader = loadopt.set_relationship_strategy(attr, {"lazy": "joined"}) if innerjoin is not None: - loader.local_opts['innerjoin'] = innerjoin + loader.local_opts["innerjoin"] = innerjoin return loader @joinedload._add_unbound_fn def joinedload(*keys, **kw): - return _UnboundLoad._from_keys( - _UnboundLoad.joinedload, keys, False, kw) + return _UnboundLoad._from_keys(_UnboundLoad.joinedload, keys, False, kw) @joinedload._add_unbound_all_fn def joinedload_all(*keys, **kw): - return _UnboundLoad._from_keys( - _UnboundLoad.joinedload, keys, True, kw) + return _UnboundLoad._from_keys(_UnboundLoad.joinedload, keys, True, kw) @loader_option() @@ -1152,8 +1195,7 @@ def immediateload(loadopt, attr): @immediateload._add_unbound_fn def immediateload(*keys): - return _UnboundLoad._from_keys( - _UnboundLoad.immediateload, keys, False, {}) + return _UnboundLoad._from_keys(_UnboundLoad.immediateload, keys, False, {}) @loader_option() @@ -1213,7 +1255,8 @@ def raiseload(loadopt, attr, sql_only=False): """ return loadopt.set_relationship_strategy( - attr, {"lazy": "raise_on_sql" if sql_only else "raise"}) + attr, {"lazy": "raise_on_sql" if sql_only else "raise"} + ) @raiseload._add_unbound_fn @@ -1251,10 +1294,7 @@ def defaultload(loadopt, attr): :ref:`deferred_loading_w_multiple` """ - return loadopt.set_relationship_strategy( - attr, - None - ) + return loadopt.set_relationship_strategy(attr, None) @defaultload._add_unbound_fn @@ -1315,15 +1355,15 @@ def defer(loadopt, key): """ return loadopt.set_column_strategy( - (key, ), - {"deferred": True, "instrument": True} + (key,), {"deferred": True, "instrument": True} ) @defer._add_unbound_fn def defer(key, *addl_attrs): return _UnboundLoad._from_keys( - _UnboundLoad.defer, (key, ) + addl_attrs, False, {}) + _UnboundLoad.defer, (key,) + addl_attrs, False, {} + ) @loader_option() @@ -1362,15 +1402,15 @@ def undefer(loadopt, key): """ return loadopt.set_column_strategy( - (key, ), - {"deferred": False, "instrument": True} + (key,), {"deferred": False, "instrument": True} ) @undefer._add_unbound_fn def undefer(key, *addl_attrs): return _UnboundLoad._from_keys( - _UnboundLoad.undefer, (key, ) + addl_attrs, False, {}) + _UnboundLoad.undefer, (key,) + addl_attrs, False, {} + ) @loader_option() @@ -1405,10 +1445,7 @@ def undefer_group(loadopt, name): """ return loadopt.set_column_strategy( - "*", - None, - {"undefer_group_%s" % name: True}, - opts_only=True + "*", None, {"undefer_group_%s" % name: True}, opts_only=True ) @@ -1448,21 +1485,18 @@ def with_expression(loadopt, key, expression): """ - expression = sql_expr._labeled( - _orm_full_deannotate(expression)) + expression = sql_expr._labeled(_orm_full_deannotate(expression)) return loadopt.set_column_strategy( - (key, ), - {"query_expression": True}, - opts={"expression": expression} + (key,), {"query_expression": True}, opts={"expression": expression} ) @with_expression._add_unbound_fn def with_expression(key, expression): return _UnboundLoad._from_keys( - _UnboundLoad.with_expression, (key, ), - False, {"expression": expression}) + _UnboundLoad.with_expression, (key,), False, {"expression": expression} + ) @loader_option() @@ -1483,7 +1517,11 @@ def selectin_polymorphic(loadopt, classes): """ loadopt.set_class_strategy( {"selectinload_polymorphic": True}, - opts={"entities": tuple(sorted((inspect(cls) for cls in classes), key=id))} + opts={ + "entities": tuple( + sorted((inspect(cls) for cls in classes), key=id) + ) + }, ) return loadopt @@ -1492,8 +1530,6 @@ def selectin_polymorphic(loadopt, classes): def selectin_polymorphic(base_cls, classes): ul = _UnboundLoad() ul.is_class_strategy = True - ul.path = (inspect(base_cls), ) - ul.selectin_polymorphic( - classes - ) + ul.path = (inspect(base_cls),) + ul.selectin_polymorphic(classes) return ul |
