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
|
||||
|
||||
# annotation only member (ex. attr: int)
|
||||
try:
|
||||
for name in inspect.getannotations(self.object):
|
||||
if name not in members:
|
||||
docstring = attr_docs.get(('', name), [])
|
||||
members[name] = ObjectMember(name, INSTANCEATTR,
|
||||
docstring="\n".join(docstring))
|
||||
except AttributeError:
|
||||
pass
|
||||
for name in inspect.getannotations(self.object):
|
||||
if name not in members:
|
||||
docstring = attr_docs.get(('', name), [])
|
||||
members[name] = ObjectMember(name, INSTANCEATTR,
|
||||
docstring="\n".join(docstring))
|
||||
|
||||
return members
|
||||
|
||||
@ -1911,16 +1908,16 @@ class DataDocumenter(GenericAliasMixin, NewTypeMixin, TypeVarMixin,
|
||||
|
||||
def update_annotations(self, parent: Any) -> None:
|
||||
"""Update __annotations__ to support type_comment and so on."""
|
||||
try:
|
||||
annotations = dict(inspect.getannotations(parent))
|
||||
parent.__annotations__ = annotations
|
||||
annotations = dict(inspect.getannotations(parent))
|
||||
parent.__annotations__ = annotations
|
||||
|
||||
try:
|
||||
analyzer = ModuleAnalyzer.for_module(self.modname)
|
||||
analyzer.analyze()
|
||||
for (classname, attrname), annotation in analyzer.annotations.items():
|
||||
if classname == '' and attrname not in annotations:
|
||||
annotations[attrname] = annotation
|
||||
except AttributeError:
|
||||
except PycodeError:
|
||||
pass
|
||||
|
||||
def import_object(self, raiseerror: bool = False) -> bool:
|
||||
@ -2450,8 +2447,6 @@ class AttributeDocumenter(GenericAliasMixin, NewTypeMixin, SlotsMixin, # type:
|
||||
annotations[attrname] = annotation
|
||||
except (AttributeError, PycodeError):
|
||||
pass
|
||||
except AttributeError:
|
||||
pass
|
||||
except TypeError:
|
||||
# Failed to set __annotations__ (built-in, extensions, etc.)
|
||||
pass
|
||||
|
@ -157,12 +157,9 @@ def get_module_members(module: Any) -> List[Tuple[str, Any]]:
|
||||
continue
|
||||
|
||||
# annotation only member (ex. attr: int)
|
||||
try:
|
||||
for name in getannotations(module):
|
||||
if name not in members:
|
||||
members[name] = (name, INSTANCEATTR)
|
||||
except AttributeError:
|
||||
pass
|
||||
for name in getannotations(module):
|
||||
if name not in members:
|
||||
members[name] = (name, INSTANCEATTR)
|
||||
|
||||
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)
|
||||
for i, cls in enumerate(getmro(subject)):
|
||||
try:
|
||||
for name in getannotations(cls):
|
||||
name = unmangle(cls, name)
|
||||
if name and name not in members:
|
||||
members[name] = Attribute(name, i == 0, INSTANCEATTR)
|
||||
except AttributeError:
|
||||
pass
|
||||
for name in getannotations(cls):
|
||||
name = unmangle(cls, name)
|
||||
if name and name not in members:
|
||||
members[name] = Attribute(name, i == 0, INSTANCEATTR)
|
||||
|
||||
if analyzer:
|
||||
# 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:
|
||||
for cls in getmro(subject):
|
||||
# annotation only member (ex. attr: int)
|
||||
try:
|
||||
for name in getannotations(cls):
|
||||
name = unmangle(cls, name)
|
||||
if name and name not in members:
|
||||
members[name] = ObjectMember(name, INSTANCEATTR, class_=cls)
|
||||
except AttributeError:
|
||||
pass
|
||||
for name in getannotations(cls):
|
||||
name = unmangle(cls, name)
|
||||
if name and name not in members:
|
||||
members[name] = ObjectMember(name, INSTANCEATTR, class_=cls)
|
||||
|
||||
# append instance attributes (cf. self.attr1) if analyzer knows
|
||||
try:
|
||||
|
@ -159,10 +159,7 @@ def getall(obj: Any) -> Optional[Sequence[str]]:
|
||||
|
||||
|
||||
def getannotations(obj: Any) -> Mapping[str, Any]:
|
||||
"""Get __annotations__ from given *obj* safely.
|
||||
|
||||
Raises AttributeError if given *obj* raises an error on accessing __attribute__.
|
||||
"""
|
||||
"""Get __annotations__ from given *obj* safely."""
|
||||
__annotations__ = safe_getattr(obj, '__annotations__', None)
|
||||
if isinstance(__annotations__, Mapping):
|
||||
return __annotations__
|
||||
|
Loading…
Reference in New Issue
Block a user