MCPcopy Index your code
hub / github.com/sqlalchemy/sqlalchemy / _get_immediate_cls_attr

Function _get_immediate_cls_attr

lib/sqlalchemy/orm/decl_base.py:179–215  ·  view source on GitHub ↗

return an attribute of the class that is either present directly on the class, e.g. not on a superclass, or is from a superclass but this superclass is a non-mapped mixin, that is, not a descendant of the declarative base and is also not classically mapped. This is used to detect at

(
    cls: Type[Any], attrname: str, strict: bool = False
)

Source from the content-addressed store, hash-verified

177
178
179def _get_immediate_cls_attr(
180 cls: Type[Any], attrname: str, strict: bool = False
181) -> Optional[Any]:
182 """return an attribute of the class that is either present directly
183 on the class, e.g. not on a superclass, or is from a superclass but
184 this superclass is a non-mapped mixin, that is, not a descendant of
185 the declarative base and is also not classically mapped.
186
187 This is used to detect attributes that indicate something about
188 a mapped class independently from any mapped classes that it may
189 inherit from.
190
191 """
192
193 # the rules are different for this name than others,
194 # make sure we've moved it out. transitional
195 assert attrname != "__abstract__"
196
197 if not issubclass(cls, object):
198 return None
199
200 if attrname in cls.__dict__:
201 return getattr(cls, attrname)
202
203 for base in cls.__mro__[1:]:
204 _is_classical_inherits = _dive_for_cls_manager(base) is not None
205
206 if attrname in base.__dict__ and (
207 base is cls
208 or (
209 (base in cls.__bases__ if strict else True)
210 and not _is_classical_inherits
211 )
212 ):
213 return getattr(base, attrname)
214 else:
215 return None
216
217
218def _dive_for_cls_manager(cls: Type[_O]) -> Optional[ClassManager[_O]]:

Callers 8

_as_declarativeMethod · 0.85
_cls_attr_resolverMethod · 0.85
__init__Method · 0.85

Calls 1

_dive_for_cls_managerFunction · 0.85

Tested by

no test coverage detected