MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / _entity_descriptor

Function _entity_descriptor

lib/sqlalchemy/orm/base.py:481–505  ·  view source on GitHub ↗

Return a class attribute given an entity and string name. May return :class:`.InstrumentedAttribute` or user-defined attribute.

(entity: _EntityType[Any], key: str)

Source from the content-addressed store, hash-verified

479
480@no_type_check
481def _entity_descriptor(entity: _EntityType[Any], key: str) -> Any:
482 """Return a class attribute given an entity and string name.
483
484 May return :class:`.InstrumentedAttribute` or user-defined
485 attribute.
486
487 """
488 insp = inspection.inspect(entity)
489 if insp.is_selectable:
490 description = entity
491 entity = insp.c
492 elif insp.is_aliased_class:
493 entity = insp.entity
494 description = entity
495 elif hasattr(insp, "mapper"):
496 description = entity = insp.mapper.class_
497 else:
498 description = entity
499
500 try:
501 return getattr(entity, key)
502 except AttributeError as err:
503 raise sa_exc.InvalidRequestError(
504 "Entity '%s' has no property '%s'" % (description, key)
505 ) from err
506
507
508if TYPE_CHECKING:

Callers 1

Calls

no outgoing calls

Tested by 1