MCPcopy Index your code
hub / github.com/python/cpython / getattr_static

Function getattr_static

Lib/inspect.py:1750–1797  ·  view source on GitHub ↗

Retrieve attributes without triggering dynamic lookup via the descriptor protocol, __getattr__ or __getattribute__. Note: this function may not be able to retrieve all attributes that getattr can fetch (like dynamically created attributes) and may find attributes that g

(obj, attr, default=_sentinel)

Source from the content-addressed store, hash-verified

1748
1749
1750def getattr_static(obj, attr, default=_sentinel):
1751 """Retrieve attributes without triggering dynamic lookup via the
1752 descriptor protocol, __getattr__ or __getattribute__.
1753
1754 Note: this function may not be able to retrieve all attributes
1755 that getattr can fetch (like dynamically created attributes)
1756 and may find attributes that getattr can't (like descriptors
1757 that raise AttributeError). It can also return descriptor objects
1758 instead of instance members in some cases. See the
1759 documentation for details.
1760 """
1761 instance_result = _sentinel
1762
1763 objtype = type(obj)
1764 if type not in _static_getmro(objtype):
1765 klass = objtype
1766 dict_attr = _shadowed_dict(klass)
1767 if (dict_attr is _sentinel or
1768 type(dict_attr) is types.MemberDescriptorType):
1769 instance_result = _check_instance(obj, attr)
1770 else:
1771 klass = obj
1772
1773 klass_result = _check_class(klass, attr)
1774
1775 if instance_result is not _sentinel and klass_result is not _sentinel:
1776 if _check_class(type(klass_result), "__get__") is not _sentinel and (
1777 _check_class(type(klass_result), "__set__") is not _sentinel
1778 or _check_class(type(klass_result), "__delete__") is not _sentinel
1779 ):
1780 return klass_result
1781
1782 if instance_result is not _sentinel:
1783 return instance_result
1784 if klass_result is not _sentinel:
1785 return klass_result
1786
1787 if obj is klass:
1788 # for types we check the metaclass too
1789 for entry in _static_getmro(type(klass)):
1790 if (
1791 _shadowed_dict(type(entry)) is _sentinel
1792 and attr in entry.__dict__
1793 ):
1794 return entry.__dict__[attr]
1795 if default is not _sentinel:
1796 return default
1797 raise AttributeError(attr)
1798
1799
1800# ------------------------------------------------ generator introspection

Callers 5

__instancecheck__Method · 0.90
_lookup_specialFunction · 0.90
_signature_from_callableFunction · 0.85

Calls 3

_shadowed_dictFunction · 0.85
_check_instanceFunction · 0.85
_check_classFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…