diff options
Diffstat (limited to 'lib/sqlalchemy/orm/base.py')
| -rw-r--r-- | lib/sqlalchemy/orm/base.py | 128 |
1 files changed, 70 insertions, 58 deletions
diff --git a/lib/sqlalchemy/orm/base.py b/lib/sqlalchemy/orm/base.py index deddaa5a4..abc572d9a 100644 --- a/lib/sqlalchemy/orm/base.py +++ b/lib/sqlalchemy/orm/base.py @@ -15,66 +15,69 @@ from . import exc import operator PASSIVE_NO_RESULT = util.symbol( - 'PASSIVE_NO_RESULT', + "PASSIVE_NO_RESULT", """Symbol returned by a loader callable or other attribute/history retrieval operation when a value could not be determined, based on loader callable flags. - """ + """, ) ATTR_WAS_SET = util.symbol( - 'ATTR_WAS_SET', + "ATTR_WAS_SET", """Symbol returned by a loader callable to indicate the retrieved value, or values, were assigned to their attributes on the target object. - """ + """, ) ATTR_EMPTY = util.symbol( - 'ATTR_EMPTY', - """Symbol used internally to indicate an attribute had no callable.""" + "ATTR_EMPTY", + """Symbol used internally to indicate an attribute had no callable.""", ) NO_VALUE = util.symbol( - 'NO_VALUE', + "NO_VALUE", """Symbol which may be placed as the 'previous' value of an attribute, indicating no value was loaded for an attribute when it was modified, and flags indicated we were not to load it. - """ + """, ) NEVER_SET = util.symbol( - 'NEVER_SET', + "NEVER_SET", """Symbol which may be placed as the 'previous' value of an attribute indicating that the attribute had not been assigned to previously. - """ + """, ) NO_CHANGE = util.symbol( "NO_CHANGE", """No callables or SQL should be emitted on attribute access and no state should change - """, canonical=0 + """, + canonical=0, ) CALLABLES_OK = util.symbol( "CALLABLES_OK", """Loader callables can be fired off if a value is not present. - """, canonical=1 + """, + canonical=1, ) SQL_OK = util.symbol( "SQL_OK", """Loader callables can emit SQL at least on scalar value attributes.""", - canonical=2 + canonical=2, ) RELATED_OBJECT_OK = util.symbol( "RELATED_OBJECT_OK", """Callables can use SQL to load related objects as well as scalar value attributes. - """, canonical=4 + """, + canonical=4, ) INIT_OK = util.symbol( @@ -82,111 +85,116 @@ INIT_OK = util.symbol( """Attributes should be initialized with a blank value (None or an empty collection) upon get, if no other value can be obtained. - """, canonical=8 + """, + canonical=8, ) NON_PERSISTENT_OK = util.symbol( "NON_PERSISTENT_OK", """Callables can be emitted if the parent is not persistent.""", - canonical=16 + canonical=16, ) LOAD_AGAINST_COMMITTED = util.symbol( "LOAD_AGAINST_COMMITTED", """Callables should use committed values as primary/foreign keys during a load. - """, canonical=32 + """, + canonical=32, ) NO_AUTOFLUSH = util.symbol( "NO_AUTOFLUSH", """Loader callables should disable autoflush.""", - canonical=64 + canonical=64, ) NO_RAISE = util.symbol( "NO_RAISE", """Loader callables should not raise any assertions""", - canonical=128 + canonical=128, ) # pre-packaged sets of flags used as inputs PASSIVE_OFF = util.symbol( "PASSIVE_OFF", "Callables can be emitted in all cases.", - canonical=(RELATED_OBJECT_OK | NON_PERSISTENT_OK | - INIT_OK | CALLABLES_OK | SQL_OK) + canonical=( + RELATED_OBJECT_OK | NON_PERSISTENT_OK | INIT_OK | CALLABLES_OK | SQL_OK + ), ) PASSIVE_RETURN_NEVER_SET = util.symbol( "PASSIVE_RETURN_NEVER_SET", """PASSIVE_OFF ^ INIT_OK""", - canonical=PASSIVE_OFF ^ INIT_OK + canonical=PASSIVE_OFF ^ INIT_OK, ) PASSIVE_NO_INITIALIZE = util.symbol( "PASSIVE_NO_INITIALIZE", "PASSIVE_RETURN_NEVER_SET ^ CALLABLES_OK", - canonical=PASSIVE_RETURN_NEVER_SET ^ CALLABLES_OK + canonical=PASSIVE_RETURN_NEVER_SET ^ CALLABLES_OK, ) PASSIVE_NO_FETCH = util.symbol( - "PASSIVE_NO_FETCH", - "PASSIVE_OFF ^ SQL_OK", - canonical=PASSIVE_OFF ^ SQL_OK + "PASSIVE_NO_FETCH", "PASSIVE_OFF ^ SQL_OK", canonical=PASSIVE_OFF ^ SQL_OK ) PASSIVE_NO_FETCH_RELATED = util.symbol( "PASSIVE_NO_FETCH_RELATED", "PASSIVE_OFF ^ RELATED_OBJECT_OK", - canonical=PASSIVE_OFF ^ RELATED_OBJECT_OK + canonical=PASSIVE_OFF ^ RELATED_OBJECT_OK, ) PASSIVE_ONLY_PERSISTENT = util.symbol( "PASSIVE_ONLY_PERSISTENT", "PASSIVE_OFF ^ NON_PERSISTENT_OK", - canonical=PASSIVE_OFF ^ NON_PERSISTENT_OK + canonical=PASSIVE_OFF ^ NON_PERSISTENT_OK, ) -DEFAULT_MANAGER_ATTR = '_sa_class_manager' -DEFAULT_STATE_ATTR = '_sa_instance_state' -_INSTRUMENTOR = ('mapper', 'instrumentor') +DEFAULT_MANAGER_ATTR = "_sa_class_manager" +DEFAULT_STATE_ATTR = "_sa_instance_state" +_INSTRUMENTOR = ("mapper", "instrumentor") -EXT_CONTINUE = util.symbol('EXT_CONTINUE') -EXT_STOP = util.symbol('EXT_STOP') -EXT_SKIP = util.symbol('EXT_SKIP') +EXT_CONTINUE = util.symbol("EXT_CONTINUE") +EXT_STOP = util.symbol("EXT_STOP") +EXT_SKIP = util.symbol("EXT_SKIP") ONETOMANY = util.symbol( - 'ONETOMANY', + "ONETOMANY", """Indicates the one-to-many direction for a :func:`.relationship`. This symbol is typically used by the internals but may be exposed within certain API features. - """) + """, +) MANYTOONE = util.symbol( - 'MANYTOONE', + "MANYTOONE", """Indicates the many-to-one direction for a :func:`.relationship`. This symbol is typically used by the internals but may be exposed within certain API features. - """) + """, +) MANYTOMANY = util.symbol( - 'MANYTOMANY', + "MANYTOMANY", """Indicates the many-to-many direction for a :func:`.relationship`. This symbol is typically used by the internals but may be exposed within certain API features. - """) + """, +) NOT_EXTENSION = util.symbol( - 'NOT_EXTENSION', + "NOT_EXTENSION", """Symbol indicating an :class:`InspectionAttr` that's not part of sqlalchemy.ext. Is assigned to the :attr:`.InspectionAttr.extension_type` attibute. - """) + """, +) _never_set = frozenset([NEVER_SET]) @@ -207,6 +215,7 @@ def _generative(*assertions): assertion(self, fn.__name__) fn(self, *args[1:], **kw) return self + return generate @@ -215,9 +224,10 @@ def _generative(*assertions): def manager_of_class(cls): return cls.__dict__.get(DEFAULT_MANAGER_ATTR, None) + instance_state = operator.attrgetter(DEFAULT_STATE_ATTR) -instance_dict = operator.attrgetter('__dict__') +instance_dict = operator.attrgetter("__dict__") def instance_str(instance): @@ -232,7 +242,7 @@ def state_str(state): if state is None: return "None" else: - return '<%s at 0x%x>' % (state.class_.__name__, id(state.obj())) + return "<%s at 0x%x>" % (state.class_.__name__, id(state.obj())) def state_class_str(state): @@ -243,7 +253,7 @@ def state_class_str(state): if state is None: return "None" else: - return '<%s>' % (state.class_.__name__, ) + return "<%s>" % (state.class_.__name__,) def attribute_str(instance, attribute): @@ -335,15 +345,15 @@ def _is_mapped_class(entity): """ insp = inspection.inspect(entity, False) - return insp is not None and \ - not insp.is_clause_element and \ - ( - insp.is_mapper or insp.is_aliased_class - ) + return ( + insp is not None + and not insp.is_clause_element + and (insp.is_mapper or insp.is_aliased_class) + ) def _attr_as_key(attr): - if hasattr(attr, 'key'): + if hasattr(attr, "key"): return attr.key else: return expression._column_as_key(attr) @@ -351,7 +361,7 @@ def _attr_as_key(attr): def _orm_columns(entity): insp = inspection.inspect(entity, False) - if hasattr(insp, 'selectable') and hasattr(insp.selectable, 'c'): + if hasattr(insp, "selectable") and hasattr(insp.selectable, "c"): return [c for c in insp.selectable.c] else: return [entity] @@ -359,8 +369,7 @@ def _orm_columns(entity): def _is_aliased_class(entity): insp = inspection.inspect(entity, False) - return insp is not None and \ - getattr(insp, "is_aliased_class", False) + return insp is not None and getattr(insp, "is_aliased_class", False) def _entity_descriptor(entity, key): @@ -386,11 +395,11 @@ def _entity_descriptor(entity, key): return getattr(entity, key) except AttributeError: raise sa_exc.InvalidRequestError( - "Entity '%s' has no property '%s'" % - (description, key) + "Entity '%s' has no property '%s'" % (description, key) ) -_state_mapper = util.dottedgetter('manager.mapper') + +_state_mapper = util.dottedgetter("manager.mapper") @inspection._inspects(type) @@ -429,7 +438,8 @@ def class_mapper(class_, configure=True): if mapper is None: if not isinstance(class_, type): raise sa_exc.ArgumentError( - "Class object expected, got '%r'." % (class_, )) + "Class object expected, got '%r'." % (class_,) + ) raise exc.UnmappedClassError(class_) else: return mapper @@ -449,6 +459,7 @@ class InspectionAttr(object): here intact for forwards-compatibility. """ + __slots__ = () is_selectable = False @@ -551,4 +562,5 @@ class _MappedAttribute(object): attributes. """ + __slots__ = () |
