Skip to content

Commit

Permalink
Define RST307; wildcard in mappings (#29)
Browse files Browse the repository at this point in the history
* Allow wildcard in mappings
* Define RST307
* Sphinx users may want to ignore RST307
* Include 'invalid' RST307 examples from Sphinx

https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html
  • Loading branch information
peterjc committed Sep 22, 2020
1 parent 2c7a584 commit 413bb30
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
8 changes: 7 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ RST303 Unknown directive type "XXX".
RST304 Unknown interpreted text role "XXX".
RST305 Undefined substitution referenced: "XXX".
RST306 Unknown target name: "XXX".
RST307 Error in "XXX" directive:
RST399 Previously unseen major error, not yet assigned a unique code.
====== =======================================================================

Expand Down Expand Up @@ -191,7 +192,8 @@ We assume you are familiar with `flake8 configuration

If you are using Sphinx or other extensions to reStructuredText, you will
want to define any additional directives or roles you are using to avoid
false positive ``RST303`` and ``RST304`` violations.
false positive ``RST303`` and ``RST304`` violations. You may also need to
ingore ``RST307`` if using Sphinx directives with arguments.

You can set these at the command line if you wish::

Expand All @@ -208,6 +210,9 @@ for example in your ``.flake8``, ``setup.cfg``, or ``tox.ini`` file, e.g.::
rst-directives =
envvar,
exception,
extend-ignore =
RST307,
# ...

Note that flake8 allows splitting the comma separated lists over multiple
lines, and allows including of hash comment lines.
Expand All @@ -231,6 +236,7 @@ Version History
======= ========== ===========================================================
Version Released Changes
------- ---------- -----------------------------------------------------------
v0.0.14 2020-09-22 - Adds ``RST307`` for error in directive (eg invalid args).
v0.0.13 2019-12-26 - Adds ``RST218`` and ``RST219``.
v0.0.12 2019-11-18 - Adds ``RST213`` to ``RST217``.
v0.0.11 2019-08-07 - Configuration options to define additional directives and
Expand Down
20 changes: 11 additions & 9 deletions flake8_rst_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def tokenize_open(filename):
import restructuredtext_lint as rst_lint


__version__ = "0.0.13"
__version__ = "0.0.14"


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -185,13 +185,15 @@ def tokenize_open(filename):
"Unexpected indentation.": 1,
"Malformed table.": 2,
# e.g. Unknown directive type "req".
"Unknown directive type": 3,
'Unknown directive type "*".': 3,
# e.g. Unknown interpreted text role "need".
"Unknown interpreted text role": 4,
'Unknown interpreted text role "*".': 4,
# e.g. Undefined substitution referenced: "dict".
"Undefined substitution referenced:": 5,
'Undefined substitution referenced: "*".': 5,
# e.g. Unknown target name: "license_txt".
"Unknown target name:": 6,
'Unknown target name: "*".': 6,
# e.g. Error in "code" directive:
'Error in "*" directive:': 7,
}

# Level 4 - severe
Expand All @@ -217,12 +219,12 @@ def code_mapping(level, msg, extra_directives, extra_roles, default=99):
# ---> 'Unknown directive type'
# e.g. 'Unknown interpreted text role "need".'
# ---> 'Unknown interpreted text role'
if msg.count('"') == 2 and ' "' in msg and msg.endswith('".'):
txt = msg[: msg.index(' "')]
if msg.count('"') == 2 and ' "' in msg:
value = msg.split('"', 2)[1]
if txt == "Unknown directive type" and value in extra_directives:
txt = msg.replace(' "' + value + '"', ' "*"')
if txt == 'Unknown directive type "*".' and value in extra_directives:
return 0
if txt == "Unknown interpreted text role" and value in extra_roles:
if txt == 'Unknown interpreted text role "*".' and value in extra_roles:
return 0
return code_mappings_by_level[level].get(txt, default)
return default
Expand Down
44 changes: 44 additions & 0 deletions tests/RST307/code_invalid_arg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Print 'Hello world' to the terminal.
This is a code block with not arguments other
than the format:
.. code:: pycon
>>> print("Hello")
Hello
Now with an invalid code block argument:
.. code:: pycon
:wibble:
>>> print("Hello")
Hello
Here are some actual examples from the Sphinx v4.0.0
documentation:
.. code-block:: python
:caption: this.py
:name: this-py
print 'Explicit is better than implicit.'
And:
.. code-block:: ruby
:dedent: 4
some ruby code
Sadly docutils considers all three examples to be invalid:
$ flake8 --select RST RST307/code_invalid_arg.py
RST307/code_invalid_arg.py:14:1: RST307 Error in "code" directive
RST307/code_invalid_arg.py:23:1: RST307 Error in "code-block" directive:
RST307/code_invalid_arg.py:31:1: RST307 Error in "code-block" directive:
"""

print("Hello world")

0 comments on commit 413bb30

Please sign in to comment.