用于rst文件和docstrings中的代码的flake8

flake8-rst的Python项目详细描述


薄片8 RST模块

PyPIconda-forgeBuild Status

允许对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安装工具。

工具搜索sourcecodecode-blockipython块,裁剪并在上面运行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']
roleexample
^{}Blocks with same group are combined to one.^{}
Blocks with group ^{} are checked individual.^{}
Blocks with group ^{} are not checked.^{}
^{}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 ^{} for current block^{}

请记住:

  • 添加到同一组(组None除外)中的块的角色除非出现在第一个块中,否则无效。
  • 提供的引导代码将被;分割成单独的行。
  • E999 SyntaxError: invalid syntax导致flake8跳过AST测试。在块中保留强制的E999问题 :flake8-group: Ignore保留其余块的完整测试。

默认块命名

可以分别为所有指令指定默认组名:

flake8-rst --default-groupnames '<file-pattern>-><directive>: <groupname>'

file-patterndirective按外观顺序由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'

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JavaBridge errno=10054被Remotehost关闭   javafx如何在每次调用方法中的变量时更新它?(爪哇)   java mod_群集在发现后未平衡负载   Java软件编辑器/语法高亮   java为什么不能强制转换数组的结果。asList()到ArrayList?   java HIBERNATE:无法使用HIBERNATE从MySQL中提取数据   java在Google地图片段上添加布局   java在AbstractTableModel中执行setValueAt之前,我如何做一些事情?   java在整个Tomcat运行时保存变量   java如何在Thymeleaf模板中获取环境变量的值?   java Selenium Chrome驱动程序针对属性的标签   java正则表达式捕获未知数量的重复组