def import_object(self, raiseerror: bool = False) -> bool: """ Import the object given by *self.modname* and *self.objpath* and set it as ``self.object``. :returns: :py:obj:`True` if successful, :py:obj:`False` if an error occurred. """ try: ret = super().import_object(raiseerror=True) if inspect.isenumattribute(self.object): self.object = self.object.value if inspect.isattributedescriptor(self.object): self._datadescriptor = True else: # if it's not a data descriptor self._datadescriptor = False except ImportError as exc: if self.isinstanceattribute(): self.object = INSTANCEATTR self._datadescriptor = False ret = True elif raiseerror: raise else: logger.warning(exc.args[0], type="autodoc", subtype="import_object") self.env.note_reread() ret = False return ret
def can_document_member(cls, member: Any, membername: str, isattr: bool, parent: Any) -> bool: """ Called to see if a member can be documented by this documenter. """ if inspect.isattributedescriptor(member): return True elif (not isinstance(parent, ModuleDocumenter) and not inspect.isroutine(member) and not isinstance(member, type)): return True else: return False
def test_isattributedescriptor(app): from target.methods import Base class Descriptor: def __get__(self, obj, typ=None): pass assert inspect.isattributedescriptor(Base.prop) is True # property assert inspect.isattributedescriptor(Base.meth) is False # method assert inspect.isattributedescriptor( Base.staticmeth) is False # staticmethod assert inspect.isattributedescriptor(Base.classmeth) is False # classmetho assert inspect.isattributedescriptor( Descriptor) is False # custom descriptor class # NOQA assert inspect.isattributedescriptor( str.join) is False # MethodDescriptorType # NOQA assert inspect.isattributedescriptor( object.__init__) is False # WrapperDescriptorType # NOQA assert inspect.isattributedescriptor( dict.__dict__['fromkeys'] ) is False # ClassMethodDescriptorType # NOQA assert inspect.isattributedescriptor( types.FrameType.f_locals) is True # GetSetDescriptorType # NOQA assert inspect.isattributedescriptor( datetime.timedelta.days) is True # MemberDescriptorType # NOQA try: # _testcapi module cannot be importable in some distro # refs: https://github.com/sphinx-doc/sphinx/issues/9868 import _testcapi testinstancemethod = _testcapi.instancemethod(str.__repr__) assert inspect.isattributedescriptor( testinstancemethod) is False # instancemethod (C-API) # NOQA except ImportError: pass
def test_isattributedescriptor(app): from target.methods import Base class Descriptor: def __get__(self, obj, typ=None): pass testinstancemethod = _testcapi.instancemethod(str.__repr__) assert inspect.isattributedescriptor(Base.prop) is True # property assert inspect.isattributedescriptor(Base.meth) is False # method assert inspect.isattributedescriptor( Base.staticmeth) is False # staticmethod assert inspect.isattributedescriptor(Base.classmeth) is False # classmetho assert inspect.isattributedescriptor( Descriptor) is False # custom descriptor class # NOQA assert inspect.isattributedescriptor( str.join) is False # MethodDescriptorType # NOQA assert inspect.isattributedescriptor( object.__init__) is False # WrapperDescriptorType # NOQA assert inspect.isattributedescriptor( dict.__dict__['fromkeys'] ) is False # ClassMethodDescriptorType # NOQA assert inspect.isattributedescriptor( types.FrameType.f_locals) is True # GetSetDescriptorType # NOQA assert inspect.isattributedescriptor( datetime.timedelta.days) is True # MemberDescriptorType # NOQA assert inspect.isattributedescriptor( testinstancemethod) is False # instancemethod (C-API) # NOQA