Skip to content

Commit c576839

Browse files
committed
Try restructuredtext_lint -> rstcheck
Idea here is rstcheck will also validate python blocks etc, as well as the reStructuredText via docutils. That would need new error codes: $ flake8 --select RST some_python.py some_python.py:8:1: RST999 Unexpected prefix: '(python) unexpected EOF while parsing' See GitHub issue #22, using the example there: def some(): """ ==== Test ==== .. code:: python print( End. """ pass
1 parent 3651dae commit c576839

1 file changed

Lines changed: 27 additions & 17 deletions

File tree

flake8_rst_docstrings.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from io import StringIO
1919
from io import TextIOWrapper
2020

21+
import rstcheck
22+
2123
#####################################
2224
# Start of backported tokenize code #
2325
#####################################
@@ -136,10 +138,7 @@ def tokenize_open(filename):
136138
# End of backported tokenize code #
137139
###################################
138140

139-
import restructuredtext_lint as rst_lint
140-
141-
142-
__version__ = "0.0.13"
141+
__version__ = "0.1.0"
143142

144143

145144
log = logging.getLogger(__name__)
@@ -149,6 +148,7 @@ def tokenize_open(filename):
149148
rst_fail_parse = 901
150149
rst_fail_all = 902
151150
rst_fail_lint = 903
151+
rst_unknown_prefix = 999
152152

153153
# Level 1 - info
154154
code_mapping_info = {
@@ -1054,10 +1054,9 @@ def run(self):
10541054
# leading whitespace from each line - this avoids false
10551055
# positive severe error "Unexpected section title."
10561056
unindented = trim(dequote_docstring(definition.docstring))
1057-
# Off load RST validation to reStructuredText-lint
1057+
# Off load RST validation to rstcheck
10581058
# which calls docutils internally.
1059-
# TODO: Should we pass the Python filename as filepath?
1060-
rst_errors = list(rst_lint.lint(unindented))
1059+
rst_errors = list(rstcheck.check(unindented))
10611060
except Exception as err:
10621061
# e.g. UnicodeDecodeError
10631062
msg = "%s%03i %s" % (
@@ -1067,10 +1066,7 @@ def run(self):
10671066
)
10681067
yield definition.start, 0, msg, type(self)
10691068
continue
1070-
for rst_error in rst_errors:
1071-
# TODO - make this a configuration option?
1072-
if rst_error.level <= 1:
1073-
continue
1069+
for line_number, rst_error in rst_errors:
10741070
# Levels:
10751071
#
10761072
# 0 - debug --> we don't receive these
@@ -1080,21 +1076,35 @@ def run(self):
10801076
# 4 - severe --> RST4## codes
10811077
#
10821078
# Map the string to a unique code:
1083-
msg = rst_error.message.split("\n", 1)[0]
1084-
code = code_mapping(
1085-
rst_error.level, msg, self.extra_directives, self.extra_roles
1086-
)
1079+
if rst_error.startswith("(INFO/1) "):
1080+
level = 1
1081+
elif rst_error.startswith("(WARNING/2) "):
1082+
level = 2
1083+
elif rst_error.startswith("(ERROR/3) "):
1084+
level = 3
1085+
elif rst_error.startswith("(SEVERE/4) "):
1086+
level = 4
1087+
else:
1088+
msg = "%s%03i %s" % (
1089+
rst_prefix,
1090+
rst_unknown_prefix,
1091+
"Unexpected prefix: %r" % rst_error,
1092+
)
1093+
yield definition.start + line_number, 0, msg, type(self)
1094+
continue
1095+
msg = rst_error.split(None, 1)[1]
1096+
code = code_mapping(level, msg, self.extra_directives, self.extra_roles)
10871097
if not code:
10881098
# We ignored it, e.g. a known Sphinx role
10891099
continue
10901100
assert 0 < code < 100, code
1091-
code += 100 * rst_error.level
1101+
code += 100 * level
10921102
msg = "%s%03i %s" % (rst_prefix, code, msg)
10931103

10941104
# This will return the line number by combining the
10951105
# start of the docstring with the offet within it.
10961106
# We don't know the column number, leaving as zero.
1097-
yield definition.start + rst_error.line, 0, msg, type(self)
1107+
yield definition.start + line_number, 0, msg, type(self)
10981108

10991109
def load_source(self):
11001110
"""Load the source for the specified file."""

0 commit comments

Comments
 (0)