MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _as_instance

Method _as_instance

lib/sqlalchemy/ext/associationproxy.py:497–522  ·  view source on GitHub ↗
(
        self, class_: Any, obj: Any
    )

Source from the content-addressed store, hash-verified

495 return self._as_instance(class_, obj)
496
497 def _as_instance(
498 self, class_: Any, obj: Any
499 ) -> AssociationProxyInstance[_T]:
500 try:
501 inst = class_.__dict__[self.key + "_inst"]
502 except KeyError:
503 inst = None
504
505 # avoid exception context
506 if inst is None:
507 owner = self._calc_owner(class_)
508 if owner is not None:
509 inst = AssociationProxyInstance.for_proxy(self, owner, obj)
510 setattr(class_, self.key + "_inst", inst)
511 else:
512 inst = None
513
514 if inst is not None and not inst._is_canonical:
515 # the AssociationProxyInstance can't be generalized
516 # since the proxied attribute is not on the targeted
517 # class, only on subclasses of it, which might be
518 # different. only return for the specific
519 # object's current value
520 return inst._non_canonical_get_for_object(obj) # type: ignore
521 else:
522 return inst # type: ignore # TODO
523
524 def _calc_owner(self, target_cls: Any) -> Any:
525 # we might be getting invoked for a subclass

Callers 4

__get__Method · 0.95
__set__Method · 0.95
__delete__Method · 0.95
for_classMethod · 0.95

Calls 3

_calc_ownerMethod · 0.95
for_proxyMethod · 0.80

Tested by

no test coverage detected