diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2021-01-31 12:41:28 +0300 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2021-01-31 12:42:32 +0300 |
commit | 50295f18c25020e15e9bc398a06894ac01c98e83 (patch) | |
tree | 0adc1460e7ec8568452873ea4faec3b195b2c37a /sphinx/ext | |
parent | e6f445f2f8fd5f2fbb85e330b36da561f43b61b7 (diff) |
refactor: AttributeError handling for getannotations() is not needed
Diffstat (limited to 'sphinx/ext')
-rw-r--r-- | sphinx/ext/autodoc/__init__.py | 23 | ||||
-rw-r--r-- | sphinx/ext/autodoc/importer.py | 31 |
2 files changed, 20 insertions, 34 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: |