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