From 50295f18c25020e15e9bc398a06894ac01c98e83 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 31 Jan 2021 18:41:28 +0900 Subject: [PATCH] refactor: AttributeError handling for getannotations() is not needed --- sphinx/ext/autodoc/__init__.py | 23 +++++++++-------------- sphinx/ext/autodoc/importer.py | 31 +++++++++++-------------------- sphinx/util/inspect.py | 5 +---- 3 files changed, 21 insertions(+), 38 deletions(-) diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index 797b80dea..f8deecb41 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -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 diff --git a/sphinx/ext/autodoc/importer.py b/sphinx/ext/autodoc/importer.py index d7c9b93f5..1593792ea 100644 --- a/sphinx/ext/autodoc/importer.py +++ b/sphinx/ext/autodoc/importer.py @@ -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: diff --git a/sphinx/util/inspect.py b/sphinx/util/inspect.py index faef8ce94..986049acf 100644 --- a/sphinx/util/inspect.py +++ b/sphinx/util/inspect.py @@ -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__