Skip to content

Commit

Permalink
Cope with Python 3.8 AST lineno changes
Browse files Browse the repository at this point in the history
Also fixes a historic off-by-one error when the violation
was in a module docstring.

Tested with single-line and multi-line class/def lines
  • Loading branch information
peterjc committed Apr 30, 2021
1 parent 3c518da commit c4394a6
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions flake8_rst_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,23 @@ def run(self):
continue

if rst_errors:
start = node.body[0].lineno - len(
ast.get_docstring(node, clean=False).splitlines()
try:
node.body[0].end_lineno
# Worked, on Python 3.8+ and can trust the start
start = node.body[0].lineno - 1 # AST value 1 based
except AttributeError:
# On Python 3.7 or older, and must compute start line
start = node.body[0].lineno - len(
ast.get_docstring(node, clean=False).splitlines()
)
if isinstance(node, ast.Module):
start -= 1 # Why?
assert (
node.body[0].lineno >= 1 and start >= 0
), "Bad start line, node line number %i for: %s\n" % (
node.body[0].lineno,
docstring,
)

for rst_error in rst_errors:
# TODO - make this a configuration option?
if rst_error.level <= 1:
Expand Down

0 comments on commit c4394a6

Please sign in to comment.