mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
refactor: AttributeError handling for getannotations() is not needed
This commit is contained in:
parent
e6f445f2f8
commit
50295f18c2
@ -1064,14 +1064,11 @@ class ModuleDocumenter(Documenter):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
# annotation only member (ex. attr: int)
|
# annotation only member (ex. attr: int)
|
||||||
try:
|
|
||||||
for name in inspect.getannotations(self.object):
|
for name in inspect.getannotations(self.object):
|
||||||
if name not in members:
|
if name not in members:
|
||||||
docstring = attr_docs.get(('', name), [])
|
docstring = attr_docs.get(('', name), [])
|
||||||
members[name] = ObjectMember(name, INSTANCEATTR,
|
members[name] = ObjectMember(name, INSTANCEATTR,
|
||||||
docstring="\n".join(docstring))
|
docstring="\n".join(docstring))
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return members
|
return members
|
||||||
|
|
||||||
@ -1911,16 +1908,16 @@ class DataDocumenter(GenericAliasMixin, NewTypeMixin, TypeVarMixin,
|
|||||||
|
|
||||||
def update_annotations(self, parent: Any) -> None:
|
def update_annotations(self, parent: Any) -> None:
|
||||||
"""Update __annotations__ to support type_comment and so on."""
|
"""Update __annotations__ to support type_comment and so on."""
|
||||||
try:
|
|
||||||
annotations = dict(inspect.getannotations(parent))
|
annotations = dict(inspect.getannotations(parent))
|
||||||
parent.__annotations__ = annotations
|
parent.__annotations__ = annotations
|
||||||
|
|
||||||
|
try:
|
||||||
analyzer = ModuleAnalyzer.for_module(self.modname)
|
analyzer = ModuleAnalyzer.for_module(self.modname)
|
||||||
analyzer.analyze()
|
analyzer.analyze()
|
||||||
for (classname, attrname), annotation in analyzer.annotations.items():
|
for (classname, attrname), annotation in analyzer.annotations.items():
|
||||||
if classname == '' and attrname not in annotations:
|
if classname == '' and attrname not in annotations:
|
||||||
annotations[attrname] = annotation
|
annotations[attrname] = annotation
|
||||||
except AttributeError:
|
except PycodeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def import_object(self, raiseerror: bool = False) -> bool:
|
def import_object(self, raiseerror: bool = False) -> bool:
|
||||||
@ -2450,8 +2447,6 @@ class AttributeDocumenter(GenericAliasMixin, NewTypeMixin, SlotsMixin, # type:
|
|||||||
annotations[attrname] = annotation
|
annotations[attrname] = annotation
|
||||||
except (AttributeError, PycodeError):
|
except (AttributeError, PycodeError):
|
||||||
pass
|
pass
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
except TypeError:
|
except TypeError:
|
||||||
# Failed to set __annotations__ (built-in, extensions, etc.)
|
# Failed to set __annotations__ (built-in, extensions, etc.)
|
||||||
pass
|
pass
|
||||||
|
@ -157,12 +157,9 @@ def get_module_members(module: Any) -> List[Tuple[str, Any]]:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
# annotation only member (ex. attr: int)
|
# annotation only member (ex. attr: int)
|
||||||
try:
|
|
||||||
for name in getannotations(module):
|
for name in getannotations(module):
|
||||||
if name not in members:
|
if name not in members:
|
||||||
members[name] = (name, INSTANCEATTR)
|
members[name] = (name, INSTANCEATTR)
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return sorted(list(members.values()))
|
return sorted(list(members.values()))
|
||||||
|
|
||||||
@ -230,13 +227,10 @@ def get_object_members(subject: Any, objpath: List[str], attrgetter: Callable,
|
|||||||
|
|
||||||
# annotation only member (ex. attr: int)
|
# annotation only member (ex. attr: int)
|
||||||
for i, cls in enumerate(getmro(subject)):
|
for i, cls in enumerate(getmro(subject)):
|
||||||
try:
|
|
||||||
for name in getannotations(cls):
|
for name in getannotations(cls):
|
||||||
name = unmangle(cls, name)
|
name = unmangle(cls, name)
|
||||||
if name and name not in members:
|
if name and name not in members:
|
||||||
members[name] = Attribute(name, i == 0, INSTANCEATTR)
|
members[name] = Attribute(name, i == 0, INSTANCEATTR)
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if analyzer:
|
if analyzer:
|
||||||
# append instance attributes (cf. self.attr1) if analyzer knows
|
# append instance attributes (cf. self.attr1) if analyzer knows
|
||||||
@ -301,13 +295,10 @@ def get_class_members(subject: Any, objpath: List[str], attrgetter: Callable
|
|||||||
try:
|
try:
|
||||||
for cls in getmro(subject):
|
for cls in getmro(subject):
|
||||||
# annotation only member (ex. attr: int)
|
# annotation only member (ex. attr: int)
|
||||||
try:
|
|
||||||
for name in getannotations(cls):
|
for name in getannotations(cls):
|
||||||
name = unmangle(cls, name)
|
name = unmangle(cls, name)
|
||||||
if name and name not in members:
|
if name and name not in members:
|
||||||
members[name] = ObjectMember(name, INSTANCEATTR, class_=cls)
|
members[name] = ObjectMember(name, INSTANCEATTR, class_=cls)
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# append instance attributes (cf. self.attr1) if analyzer knows
|
# append instance attributes (cf. self.attr1) if analyzer knows
|
||||||
try:
|
try:
|
||||||
|
@ -159,10 +159,7 @@ def getall(obj: Any) -> Optional[Sequence[str]]:
|
|||||||
|
|
||||||
|
|
||||||
def getannotations(obj: Any) -> Mapping[str, Any]:
|
def getannotations(obj: Any) -> Mapping[str, Any]:
|
||||||
"""Get __annotations__ from given *obj* safely.
|
"""Get __annotations__ from given *obj* safely."""
|
||||||
|
|
||||||
Raises AttributeError if given *obj* raises an error on accessing __attribute__.
|
|
||||||
"""
|
|
||||||
__annotations__ = safe_getattr(obj, '__annotations__', None)
|
__annotations__ = safe_getattr(obj, '__annotations__', None)
|
||||||
if isinstance(__annotations__, Mapping):
|
if isinstance(__annotations__, Mapping):
|
||||||
return __annotations__
|
return __annotations__
|
||||||
|
Loading…
Reference in New Issue
Block a user