summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2018-05-21 00:24:15 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2018-05-21 00:25:11 +0900
commitbe8e2be47b405723e39ff46e473829bcc8d17cb9 (patch)
tree00e122bd69e09a5809e3ecc494ebe431f2e6b1da
parent679002c483958a041772bd2a1f1014bb9dada9a8 (diff)
downloadsphinx-git-be8e2be47b405723e39ff46e473829bcc8d17cb9.tar.gz
Fix #4914: autodoc: Parsing error when using dataclasses without default values
-rw-r--r--CHANGES1
-rw-r--r--sphinx/pycode/parser.py9
-rw-r--r--tests/test_pycode_parser.py6
3 files changed, 10 insertions, 6 deletions
diff --git a/CHANGES b/CHANGES
index f9b6e6af5..a2918b7b2 100644
--- a/CHANGES
+++ b/CHANGES
@@ -33,6 +33,7 @@ Bugs fixed
* #4978: latex: shorthandoff is not set up for Brazil locale
* #4928: i18n: Ignore dot-directories like .git/ in LC_MESSAGES/
* #4946: py domain: type field could not handle "None" as a type
+* #4914: autodoc: Parsing error when using dataclasses without default values
Testing
--------
diff --git a/sphinx/pycode/parser.py b/sphinx/pycode/parser.py
index deba48b1c..5c4291d3d 100644
--- a/sphinx/pycode/parser.py
+++ b/sphinx/pycode/parser.py
@@ -224,12 +224,13 @@ class AfterCommentParser(TokenProcessor):
def parse(self):
# type: () -> None
"""Parse the code and obtain comment after assignment."""
- # skip lvalue (until '=' operator)
- while self.fetch_token() != [OP, '=']:
+ # skip lvalue (or whole of AnnAssign)
+ while not self.fetch_token().match([OP, '='], NEWLINE, COMMENT):
assert self.current
- # skip rvalue
- self.fetch_rvalue()
+ # skip rvalue (if exists)
+ if self.current == [OP, '=']:
+ self.fetch_rvalue()
if self.current == COMMENT:
self.comment = self.current.value
diff --git a/tests/test_pycode_parser.py b/tests/test_pycode_parser.py
index 09f1f41f5..29363e17e 100644
--- a/tests/test_pycode_parser.py
+++ b/tests/test_pycode_parser.py
@@ -100,11 +100,13 @@ def test_comment_picker_location():
def test_annotated_assignment_py36():
source = ('a: str = "Sphinx" #: comment\n'
'b: int = 1\n'
- '"""string on next line"""')
+ '"""string on next line"""\n'
+ 'c: int #: comment')
parser = Parser(source)
parser.parse()
assert parser.comments == {('', 'a'): 'comment',
- ('', 'b'): 'string on next line'}
+ ('', 'b'): 'string on next line',
+ ('', 'c'): 'comment'}
assert parser.definitions == {}