用于rst文件和docstrings中的代码的flake8
flake8-rst的Python项目详细描述
薄片8 RST模块
允许对docstring或rst文件中的代码段运行flake8。
想法
迈克·拜尔在https://github.com/zzzeek/sqlalchemy/pull/362
That said, if there was some alternate form of "doctest" that could simply test a code example both for Python syntax, pep8 compliance (which would be AWESOME) as well as symbol consistency, that would be helpful. The tool could be configured with common imports and symbols significant to SQLAlchemy examples and be helpful as a basic sanity check for code examples. As it is, when writing new documentation I have to organize and run the code in a separate .py file to make sure it does the right thing. So this is a problem, just my experience with doctest in writing the tutorials has shown me what it's good at and where it's likely getting in the way.
实现灵感来自https://github.com/asottile/blacken-docs
用法
您可以从pippip install flake8-rst
安装工具。
工具搜索sourcecode
、code-block
和ipython
块,裁剪并在上面运行flake8:
.. sourcecode:: python class Example(Base): pass
或
.. code-block:: python class Example(Base): pass
支持除作业(临时)外的所有flake8参数和标志,并附加一个:
flake8-rst --bootstrap "import test"
flake8 rst使用此代码引导代码段,用于修复导入错误。
从[flake8-rst]
ini部分加载配置,如flake8。
高级用法
自定义角色
为了在使用Sphinx
的文档中使用flake8-rst
的自定义角色,请在conf.py
中使用flake8_rst.sphinxext.custom_roles
扩展sphinx。
角色对生成的文档没有影响。
extensions=[...,'flake8_rst.sphinxext.custom_roles']
role | example | |
---|---|---|
^{ | Blocks with same group are combined to one. | ^{ |
Blocks with group ^{ | ^{ | |
Blocks with group ^{ | ^{ | |
^{ | Overwrites ignore list for current block. | ^{ |
^{ | Adds arguments to ignore list for current block. | ^{ |
^{ | Overwrites select list for current block. | ^{ |
^{ | Adds arguments to select list for current block. | ^{ |
^{ | Overwrites ^{ | ^{ |
请记住:
- 添加到同一组(组
None
除外)中的块的角色除非出现在第一个块中,否则无效。 - 提供的引导代码将被
;
分割成单独的行。 E999 SyntaxError: invalid syntax
导致flake8
跳过AST
测试。在块中保留强制的E999
问题:flake8-group: Ignore
保留其余块的完整测试。
默认块命名
可以分别为所有指令指定默认组名:
flake8-rst --default-groupnames '<file-pattern>-><directive>: <groupname>'
file-pattern
和directive
按外观顺序由Unix filename pattern matching
匹配。
默认值是*.rst->*: default
,因此*.rst
文件中的所有块都合并到
其他文件他们保持独立。
但是也可以只合并*.rst
文件中的ipython
指令,并保留其他指令
单独治疗:"*.rst->ipython: default"
示例:
flake8-rst --default-groupnames "*.rst->*: default"
[flake8-rst] default-groupnames = *.rst-*: default *.py-code-block: default
断开连接的块不知道以前定义的名称:
.. code-block:: python class Example(Base): pass .. code-block:: python import datetime obj = Example(datetime.datetime.now()) # F821 undefined name 'Example'
连接块后,会发现不同的问题:
.. code-block:: python :flake8-group: ExampleGroup class Example(Base): pass .. code-block:: python :flake8-group: ExampleGroup import datetime # E402 module level import not at top of file obj = Example(datetime.datetime.now())
如果合适,可以忽略特定组的问题:
.. code-block:: python :flake8-group: ExampleGroup1 :flake8-set-ignore: E402 class Example(Base): pass .. code-block:: python :flake8-group: ExampleGroup1 import datetime obj = Example(datetime.datetime.now()) .. code-block:: python :flake8-group: ExampleGroup2 class Example(Base): pass .. code-block:: python :flake8-group: ExampleGroup2 :flake8-set-ignore: E402 # no effect, because it's not defined in first # block of ExampleGroup2 import datetime # E402 module level import not at top of file obj = Example(datetime.datetime.now())
示例
d.kataev:flake8-rst§ flake8-rst --filename="*.py *.rst" tests/data/* --bootstrap="from sqlalchemy import Table, Column, Sequence, Integer, ForeignKey, String, DateTime" tests/data/test.py:14:42: F821 undefined name 'metadata' tests/data/test.py:15:13: E128 continuation line under-indented for visual indent tests/data/test.py:16:28: F821 undefined name 'JSONB' tests/data/test.py:19:14: F821 undefined name 'engine' tests/data/test.py:22:21: E251 unexpected spaces around keyword / parameter equals tests/data/test.py:22:23: E251 unexpected spaces around keyword / parameter equals tests/data/test.rst:27:48: F821 undefined name 'metadata' tests/data/test.rst:41:22: F821 undefined name 'meta' tests/data/test.rst:56:52: F821 undefined name 'meta' tests/data/test.rst:57:32: F821 undefined name 'meta' tests/data/test.rst:69:20: F821 undefined name 'Base' tests/data/test.rst:72:56: F821 undefined name 'Base'