diff options
| author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-06-08 16:09:45 +0900 |
|---|---|---|
| committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-06-08 19:06:52 +0900 |
| commit | 0aed08b5be0244aef3d8bab7e50376fee1450480 (patch) | |
| tree | 634d5daf45edac7fa325fa5e5bc5c80f81cc3a2a | |
| parent | f0b1cbb734b2ad699d8bdcb41a53b221923aab61 (diff) | |
| download | sphinx-git-0aed08b5be0244aef3d8bab7e50376fee1450480.tar.gz | |
Fix #6451: autodoc: generates docs for "optional import"ed modules as variables
| -rw-r--r-- | CHANGES | 1 | ||||
| -rw-r--r-- | sphinx/pycode/parser.py | 11 | ||||
| -rw-r--r-- | tests/test_pycode_parser.py | 15 |
3 files changed, 27 insertions, 0 deletions
@@ -20,6 +20,7 @@ Bugs fixed immediately preceding section title by pagebreak * #6448: autodoc: crashed when autodocumenting classes with ``__slots__ = None`` * #6452: autosummary: crashed when generating document of properties +* #6451: autodoc: generates docs for "optional import"ed modules as variables Testing -------- diff --git a/sphinx/pycode/parser.py b/sphinx/pycode/parser.py index f9489e91a..1746537bb 100644 --- a/sphinx/pycode/parser.py +++ b/sphinx/pycode/parser.py @@ -357,6 +357,17 @@ class VariableCommentPicker(ast.NodeVisitor): except TypeError: pass # this assignment is not new definition! + def visit_Try(self, node): + # type: (ast.Try) -> None + """Handles Try node and processes body and else-clause. + + .. note:: pycode parser ignores objects definition in except-clause. + """ + for subnode in node.body: + self.visit(subnode) + for subnode in node.orelse: + self.visit(subnode) + def visit_ClassDef(self, node): # type: (ast.ClassDef) -> None """Handles ClassDef node and set context.""" diff --git a/tests/test_pycode_parser.py b/tests/test_pycode_parser.py index ba9778b80..8f8c6cbab 100644 --- a/tests/test_pycode_parser.py +++ b/tests/test_pycode_parser.py @@ -149,6 +149,21 @@ def test_complex_assignment_py3(): assert parser.definitions == {} +def test_assignment_in_try_clause(): + source = ('try:\n' + ' a = None #: comment\n' + 'except:\n' + ' b = None #: ignored\n' + 'else:\n' + ' c = None #: comment\n') + parser = Parser(source) + parser.parse() + assert parser.comments == {('', 'a'): 'comment', + ('', 'c'): 'comment'} + assert parser.deforders == {'a': 0, + 'c': 1} + + def test_obj_assignment(): source = ('obj = SomeObject() #: some object\n' 'obj.attr = 1 #: attr1\n' |
