用于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窗口。位置和窗口。公开问题   java如何从存储在ArrayList<Node>中的动态生成的文本字段中获取文本?   java如何立即关闭InputStream?   如何重新启动Java程序以激活环境变量   java搜索字符串是否相差一个字符   java CFB模式输出与CTR输出相同;我做错什么了吗?   java如何在javaFX中将实例化对象添加到Stage   java如何在jtextarea上打印来自不同类的文本消息   java以编程方式确定IOException的原因?   限制Java NIO通道(文件或socket)中的可用内容   javajaxb与JDOM:是否可以使用JAXB更新xml文件   批处理文件到java测试   JavaFX:stage的作用是什么。可设置大小(false)是否会导致额外的页边距?   java有没有办法告诉IntelliJ按需堆叠参数?   java Seam会话范围的组件在下一个请求中消失   java Google Web Toolkit对开发复杂的java脚本有用吗?   安卓 studio java ArrayList正在检索最高值   java为什么递归地用随机数填充LinkedList时会出现StackOverflowException?