flake8-bugbear
.. image:: https://github.com/PyCQA/flake8-bugbear/actions/workflows/ci.yml/badge.svg :target: https://github.com/PyCQA/flake8-bugbear/actions/workflows/ci.yml
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg :target: https://github.com/psf/black
.. image:: https://results.pre-commit.ci/badge/github/PyCQA/flake8-bugbear/main.svg :target: https://results.pre-commit.ci/latest/github/PyCQA/flake8-bugbear/main :alt: pre-commit.ci status
一个用于 flake8 的插件,用于查找程序中可能的 bug 和设计问题。包含不属于 pyflakes 和
pycodestyle 的警告::
bug·bear (bŭg′bâr′)
n.
1. 恐惧、焦虑或烦恼的原因:*过度拥挤常常是火车通勤者的烦恼。*
2. 一个困难或持续存在的问题:*“传统人工智能的主要难题之一是,
编程让计算机识别出不同但相似的对象是同一类型事物的实例,
这非常困难”(Jack Copeland)。*
3. 一种可怕的虚构生物,尤其是用来吓唬孩子的那种。
人们认为这些 lint 规则不应包含在主 Python 工具中,因为它们带有强烈的个人偏好,且背后没有 PEP 或标准支持。由于 flake8 被设计为可扩展的,这些 lint 规则的原始创建者认为插件是最佳途径。这为贡献者带来了更快的开发速度,并为 flake8 用户提供了灵活的部署方式。
Installation
从 pip 安装:
.. code-block:: sh
pip install flake8-bugbear
之后它将自动作为 flake8 的一部分运行;你可以使用以下命令检查它是否已被识别:
.. code-block:: sh
$ flake8 --version 3.5.0 (assertive: 1.0.1, flake8-bugbear: 18.2.0, flake8-comprehensions: 1.4.1, mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.6.0) CPython 3.7.0 on Darwin
开发
如果您希望提交 PR,我们有开发说明 here <https://github.com/PyCQA/flake8-bugbear/blob/master/DEVELOPMENT.md>_。
警告列表
.. _B001:
B001: 不要使用裸的 except:,它也会捕获意外事件,
例如内存错误、中断、系统退出等。 优先使用 except Exception:。 如果你确定自己在做什么,请明确写出
except BaseException:. Disable E722 以避免重复警告。
.. _B002:
B002: Python 不支持一元前缀自增。编写
++n is equivalent to +(+(n)), which equals n. You meant n += 1。
.. _B003:
B003: 对 os.environ 的赋值不会清除
环境。 子进程将看到过时的
变量,与当前进程不一致。 请使用
Popen 的 os.environ.clear() or the env= 参数。
.. _B004:
B004: 使用 hasattr(x, '__call__') to test if x 来判断是否可调用
是不可靠的。 如果 x implements custom __getattr__ 或其
__call__ 本身不可调用,你可能会得到误导性的
结果。 请使用 callable(x) 以获得一致的结果。
.. _B005:
B005: 使用 .strip() 处理多字符字符串会误导
读者。它看起来像是在剥离子字符串。如果这是有意为之,请将
字符集移至常量。使用
.replace(), .removeprefix(), .removesuffix() 或正则
表达式来移除字符串片段。
.. _B006:
B006: 不要使用可变数据结构作为参数默认值。 它们 在函数定义时创建。对该函数的所有调用 都会复用该数据结构的这一个实例,从而在调用之间 持久化更改。
.. _B007:
B007: 循环控制变量未在循环体内使用。 如果这是 有意为之,请以 underscores 开头命名。
.. _B008:
B008: 不要在参数默认值中执行函数调用。该调用 仅在函数定义时执行一次。对你 函数的所有调用都将复用该定义时函数调用的结果。如果 这是预期行为,请将函数调用赋值给一个模块级变量,并 使用该变量作为默认值。
.. _B009:
B009: 不要调用 getattr(x, 'attr'),而是使用普通的
属性访问:x.attr. Missing a default to getattr 会对不存在的属性
引发 AttributeError。如果你事先知道属性名称,
使用 getattr 并不会提供额外的安全性。
.. _B010:
B010: 不要调用 setattr(x, 'attr', val),而是使用普通的
属性访问:x.attr = val。如果你事先知道属性名称,
使用 setattr 并不会带来额外的安全性。
.. _B011:
B011: 不要调用 assert False since python -O 移除了这些调用。
调用方应改为 raise AssertionError()。
.. _B012:
B012: 使用 break, continue or return inside finally 块会
静默异常或覆盖 try or except 块的返回值。
若要静默异常,请在 except 块中显式执行。若要正确使用
break, continue or return,请重构代码,使这些语句不位于
finally 块中。
.. _B013:
B013: 长度为 1 的元组字面量是冗余的。 请写 except SomeError:
而不是 except (SomeError,):。
.. _B014:
B014: except (Exception, TypeError): 中冗余的异常类型。
请编写 except Exception:,它捕获的异常完全相同。
.. _B015:
B015: 无意义的比较。此比较除了
浪费 CPU 指令外毫无作用。要么在 assert 前添加,要么将其移除。
.. _B016:
B016: 不能抛出字面量。您是否打算返回它或抛出 一个 Exception?
.. _B017:
B017: assertRaises(Exception) and pytest.raises(Exception) 应当
被视为有害。它们可能导致测试通过,即使
被测代码因拼写错误而从未被执行。请断言更
具体的异常(内置或自定义),或使用 assertRaisesRegex
(如果使用 assertRaises), or add the match 关键字参数(如果
使用 pytest.raises),或使用带有 target 的上下文管理器形式
(例如 with self.assertRaises(Exception) as ex:)。
.. _B018:
B018: 发现无用的表达式。请将其赋值给变量或将其删除。
该检查还会考虑没有副作用的函数调用,例如 isinstance。
请注意,悬空的逗号会导致内容被解释为无用的元组。
例如,在语句 print(".."), is the same as (print(".."),)
中,这是一个未赋值的元组。只需删除逗号即可清除错误。
.. _B019:
B019: 在方法上使用 functools.lru_cache or functools.cache
可能导致内存泄漏。缓存可能保留实例引用,从而阻止
垃圾回收。
.. _B020:
B020: 循环控制变量覆盖了它所迭代的可迭代对象
.. _B021:
B021: f-string 用作 docstring。Python 会将其解释为拼接字符串,而非 docstring。
.. _B022:
B022: 未向 contextlib.suppress 传递任何参数。
不会抑制任何异常,因此该上下文管理器是冗余的。
注意:为避免因同名用户定义函数导致的潜在误报,此规则目前不会标记 suppress 调用。
.. _B023:
B023: 在循环内部定义的函数不得使用在循环中重新定义的变量,因为 late-binding closures are a classic gotcha <https://docs.python-guide.org/writing/gotchas/#late-binding-closures>__。
.. _B024:
B024: 抽象基类包含方法,但其中没有任何一个是抽象方法。这不一定是一个错误,但你可能忘记了添加 @abstractmethod 装饰器,可能还需要与 @classmethod、@property 和/或 @staticmethod 结合使用。
.. _B025:
B025: 在 try-except 块中发现了重复的异常。
此检查会识别在多个 except
子句中指定的异常类型。只有第一个指定会被考虑,因此可以移除所有其他指定。
.. _B026:
B026: 强烈不推荐在关键字参数之后使用星号解包,因为
它仅在关键字参数声明在所有由解包序列提供的参数之后时才有效,
而这种顺序变化可能会让读者感到意外并产生误导。
曾有关于禁止此语法的 cpython 讨论 <https://github.com/python/cpython/issues/82741>_,但遗留用法和解析器
限制使其难以实现。
.. _B027:
B027: 抽象基类中的空方法,但未使用抽象装饰器。建议添加 @abstractmethod。
.. _B028:
B028: 未找到显式的 stacklevel 参数。warnings 模块中的 warn 方法默认使用 stacklevel 为 1。这将仅显示调用 warn 方法所在行的堆栈跟踪。 因此,建议使用 2 或更大的 stacklevel 以向用户提供更多信息。 当使用 skip_file_prefixes 时,该检查将被跳过。
.. _B029:
B029: 使用 except (): 配合空元组不会处理/捕获任何内容。请添加异常以进行处理。
.. _B030:
B030: except 处理程序应仅为异常类或异常类的元组。
.. _B031:
B031: 多次使用从 itertools.groupby() 返回的生成器,第二次及之后的使用将不产生任何效果。如果结果需要多次使用,请将其保存为列表。
.. _B032:
B032: 可能的非预期类型注解(使用了 :). Did you mean to assign (using =)?
.. _B033:
B033: 集合不应包含重复项。重复项在运行时将被替换为单个项。
.. _B034:
B034: 对 re.sub、re.subn 或 re.split 的调用应通过关键字参数传递 flags 或 count/maxsplit。通常人们会假设 flags 是第三个位置参数,而忘记了 count/maxsplit,因为许多其他 re 模块函数的形式为 f(pattern, string, flags)。
.. _B035:
B035: 发现字典推导式使用了静态键 - 即常量值或并非来自推导式表达式的变量。这将导致生成一个仅包含单个键的字典,且该键被反复覆盖。
.. _B036:
B036: 发现 except BaseException: without re-raising (no raise in the top-level of the except 块)。这会捕获所有类型的异常(Exception、SystemExit、KeyboardInterrupt...),并可能阻止程序按预期退出。
.. _B037:
B037: 发现 return <value>, yield, yield <value>, or yield from <value> in class __init__() method. No values should be returned or yielded, only bare return\s 正常。
.. _B038:
B038: 已移至 B909 - 在循环体内发现了可变循环可迭代对象的变异。对循环可迭代对象的更改,例如调用 list.remove() 或通过 del,可能会导致意外的 bug。
.. _B039:
B039: ContextVar with mutable literal or function call as default. This is only evaluated once, and all subsequent calls to .get() would return the same instance of the default. This uses the same logic as B006 and B008, including ignoring values in extend-immutable-calls.
.. _B040:
B040: 调用 add_note not used. Did you forget to raise 时捕获到异常,是吗?
.. _B041:
B041: 字典字面量中存在重复的键值对。仅当键的值也相同时才发出错误,这与 pyflakes 类似的检查相反。
.. _B042:
B042: 具有自定义 __init__ 的异常类应将所有参数传递给 super().__init__(),以便与 copy.copy 和 pickle 正确配合工作。
BaseException.__reduce__ 和 BaseException.__str__ 都依赖于 args 属性被正确设置,该属性在 BaseException.__new__ 和 BaseException.__init__ 中设置。
如果您自己定义 __init__ 而没有将所有参数传递给 super().__init__,则很容易破坏 pickling,特别是当它们传递了 BaseException.__new__ 和
BaseException.__init__ 都会忽略的关键字参数时。同样重要的是,__init__ 不应接受任何仅限关键字的参数。
或者,您可以同时定义 __str__ 和 __reduce__,以绕过对 args 正确处理的需求。
如果您在父类中定义 __str__/__reduce__,此检查将无法检测到它,我们建议禁用它。
.. _B043:
B043: 不要调用 delattr(x, 'attr'), instead use del x.attr。
如果你事先知道属性名称,使用 delattr 并不会带来额外的安全性。
有主见的警告
以下警告默认被禁用,因为它们具有争议性。
它们可能适用于你,也可能不适用,如果你发现它们有用,请在配置中显式启用它们。
阅读下文了解如何启用。
.. _B901:
**B901**: 在生成器函数中使用 ``return x`` 在 Python 2 中曾经是
语法无效的。在 Python 3 中,``return x`` 可以与 ``yield from`` 结合
用作生成器中的返回值。
来自 Python 2 的用户可能期望旧的行为,这可能导致
错误。 使用原生 ``async def`` 协程,或在同一行上标记有意的
``return x`` usage with ``# noqa``。
.. _B902:
**B902**: 方法使用了无效的第一个参数。对于
实例方法,请使用 ``self``,以及 ``cls`` for class methods (which includes ``__new__``
和 ``__init_subclass__``) 或元类的实例方法(检测为
继承自 ``type``, ``ABCMeta`` or ``EnumMeta`` 的类,以
裸形式或点分形式(如 ``abc.ABCMeta``)书写)。
.. _B903:
**B903**: 对于仅在 ``__init__`` 方法中设置属性且不做其他任何操作的数据类,请使用 ``collections.namedtuple`` (or ``)。如果属性应该是可变的,请在 ``__slots__`` 中定义这些属性,以节省每个实例的内存并防止意外地在实例上创建额外的属性。
.. _B904:
**B904**: 在 ``except`` clause, raise exceptions with ``raise ... from err``
或 ``raise ... from None`` 中,以将其与异常处理中的错误区分开来。
详见 `the exception chaining tutorial <https://docs.python.org/3/tutorial/errors.html#exception-chaining>`_
.. _B905:
**B905**: ``zip()`` without an explicit `strict=` parameter set. ``strict=True`` 会导致生成的迭代器
在参数长度不一致时耗尽时引发 ``ValueError``。
排除项为 `itertools.count <https://docs.python.org/3/library/itertools.html#itertools.count>`_、`itertools.cycle <https://docs.python.org/3/library/itertools.html#itertools.cycle>`_ 和 `itertools.repeat <https://docs.python.org/3/library/itertools.html#itertools.repeat>`_(times=None),因为它们是无限迭代器。
``strict=`` 参数是在 Python 3.10 中添加的,因此不要为需要在 <3.10 上运行的代码启用此标志。
更多信息:https://peps.python.org/pep-0618/
.. _B906:
**B906**: 在函数末尾的 ``visit_`` function with no further call to a ``visit`` function. This is often an error, and will stop the visitor from recursing into the subnodes of a visited node. Consider adding a call ``self.generic_visit(node)``。
仅当函数名称中 ``visit_`` is a valid ``ast`` type with a non-empty ``_fields`` 属性之后的部分时才会触发。
此规则旨在供使用 ``ast`` 模块编写访问者的开发者启用,例如 flake8 插件作者。
.. _B907:
**B907**: 考虑替换 ``f"'{foo}'"`` with ``f"{foo!r}"`` which is both easier to read and will escape quotes inside ``foo`` if that would appear. The check tries to filter out any format specs that are invalid together with ``!r``. If you're using other conversion flags then e.g. ``f"'{foo!a}'"`` can be replaced with ``f"{ascii(foo)!r}"``. Not currently implemented for python<3.8 or ``str.format()`` 调用。
.. _B908:
**B908**: 包含类似 ``with self.assertRaises`` or ``with pytest.raises`` 的异常断言的上下文不应包含多个顶级语句。每个语句应位于其自身的上下文中。这样,测试才能确保异常仅在预期的确切语句处被抛出。
.. _B909:
**B909**: **原为 B038** - 在循环体内发现了可变循环可迭代对象的变异。对循环可迭代对象的修改,例如调用 `list.remove()` 或通过 `del`,可能会导致非预期的错误。
.. _B910:
**B910**: 使用 Counter() 代替 defaultdict(int),以避免过度使用内存,因为默认字典在访问时会以默认值记录缺失的键。
.. _B911:
**B911**: ``itertools.batched()`` without an explicit `strict=` parameter set. ``strict=True`` causes the resulting iterator to raise a ``ValueError`` if the final batch is shorter than ``n``.
``strict=`` 参数是在 Python 3.13 中添加的,因此不要为需要在 <3.13 上运行的代码启用此标志。
.. _B912:
**B912**: ``map()`` without an explicit `strict=` parameter set. ``strict=True`` 会导致生成的迭代器
在参数以不同长度耗尽时引发 ``ValueError``。
.. _B950:
**B950**: 行过长。这是
``pycodestyle``'s `` 的实用等效项:它考虑了 "max-line-length",但仅在
超出值 **10% 以上** 时触发。``noqa`` and ``type: ignore`` 注释将被忽略。你不再
会因为右括号多了一个字符无法满足 linter 要求而被迫重新格式化代码。同时,如果你
严重违反行长度限制,你将收到一条说明实际限制的消息。这受到 Raymond Hettinger 的
`"Beyond PEP 8" talk <https://www.youtube.com/watch?v=wf-BqAjZb8M>`_ 以及
高速公路巡逻队不会因超速 < 5mph 而拦下你的启发。禁用
``E501`` to avoid duplicate warnings. Like ``E501``,此错误会忽略第一行的长 shebang
以及独占一行的 url 或路径::
#! 长 shebang 被忽略
# https://some-super-long-domain-name.com/with/some/very/long/paths
url = (
"https://some-super-long-domain-name.com/with/some/very/long/paths"
)
如何启用主观性警告
To enable Bugbear's opinionated checks (B9xx), specify an --extend-select
command-line option or extend-select= option in your config file
(requires flake8 >=4.0)::
[flake8] max-line-length = 80 max-complexity = 12 ... extend-ignore = E501 extend-select = B950
Some of Bugbear's checks require other flake8 checks disabled - e.g. E501 must
be disabled when enabling B950.
If you'd like all optional warnings to be enabled for you (future proof your config!),
say B9 instead of B950. You will need flake8 >=3.2 for this feature.
For flake8 <=4.0, you will need to use the --select command-line option or
select= option in your config file. For flake8 >=3.0, this option is a whitelist
(checks not listed are implicitly disabled), so you have to explicitly specify all
checks you want enabled (e.g. select = C,E,F,W,B,B950).
The --extend-ignore command-line option and extend-ignore= config file option
require flake8 >=3.6. For older flake8 versions, the --ignore and
ignore= options can be used. Using ignore will override all codes that are
disabled by default from all installed linters, so you will need to specify these codes
in your configuration to silence them. I think this behavior is surprising so Bugbear's
opinionated warnings require explicit selection.
Note: Bugbear's enforcement of explicit opinionated warning selection is deprecated
and will be removed in a future release. It is recommended to use extend-ignore and
extend-select in your flake8 configuration to avoid implicitly altering selected
and/or ignored codes.
Configuration
The plugin currently has the following settings:
.. _extend_immutable_calls:
extend-immutable-calls: Specify a list of additional immutable calls.
This could be useful, when using other libraries that provide more immutable calls,
beside those already handled by flake8-bugbear. Calls to these method will no longer
raise a B008 or B039 warning.
.. _classmethod_decorators:
classmethod-decorators: Specify a list of decorators to additionally mark a method as a classmethod as used by B902. The default only checks for classmethod. When an @obj.name decorator is specified it will match against either name or obj.name.
This functions similarly to how pep8-naming <https://github.com/PyCQA/pep8-naming> handles it, but with different defaults, and they don't support specifying attributes such that a decorator will never match against a specified value obj.name even if decorated with @obj.name.
For example::
[flake8] max-line-length = 80 max-complexity = 12 ... extend-immutable-calls = pathlib.Path, Path classmethod-decorators = myclassmethod, mylibrary.otherclassmethod
Tests / Lints
Just run::
coverage run tests/test_bugbear.py
For linting::
pre-commit run -a
License
MIT
Change Log
UNRELEASED
* B018: 处理诸如 `isinstance(x, int)` 等无用调用,即使未分配或使用其结果
* B031: 不要将存储上下文引用(例如像 `group: T` 这样的注解目标)计为 `groupby` 生成器的一次使用 (#465)
* B902: 不要对使用点分基类(如 `abc.ABCMeta` 或 `enum.EnumMeta`)定义的元类产生误报 (#411)
25.11.29
~~~~~~~~
* B043: 添加新检查,提示不要使用常量调用 delattr (#514)
* B042: 忽略重载的 init,忽略 str+pickle 的 dunder,改进 README
25.10.21
~~~~~~~~
* B042: 新增检查,提醒在自定义异常中调用 super().__init__
* B028: 如果使用了 skip_file_prefixes 则跳过 (#503)
* B912: 新增检查,针对没有显式 `strict=` 参数的 `map()`。 (#516)
* 添加 python3.14 支持 / CI
* 移除 python3.9 支持 / CI
* flake8-bugbear 现在至少需要 Python 3.10,与 flake8 的下一个版本一致
24.12.12
~~~~~~~~
* B012 和 B025 现在也处理 try/except* (#500)
* 如果 `warnings.warn` 以 ``*args`` or ``**kwargs`` 调用,则跳过 B028 (#501)
* 添加 B911:未使用 strict= 的 itertools.batched (#502)
* Readme 为每个检查添加了锚点(不过它们似乎无法在 GitHub 上渲染)
24.10.31
~~~~~~~~
* B041: 新增字典相同键且值相同的检查 (#496)
* B037: 修复错误信息中的拼写错误
* B024: 不再将已赋值的类变量视为抽象方法 (#471)
* 将必需的 attrs 版本提升至 22.2.0
24.8.19
~~~~~~~
* B910: 实现建议,提示使用 Counter() 代替 defaultdict(int) (#489)
* B901: 当存在显式 Generator 返回类型时不触发 (#481)
* B008: 添加一些注释,重命名 b008_extend_immutable_calls (#476)
* B040: 添加了带有注释的异常但未重新抛出或使用 (#477)
* B039, 添加 ``ContextVar``,当默认值为可变字面量或函数调用时
* B040: 添加带有注释的异常但未重新抛出。 (#474)
* 在 Python 3.13 中运行测试
* 类型注解代码 (#481 + #483)
* 用 unsafe_hash 替换 hash (#486)
24.4.26
~~~~~~~
* B909: 修复影响可变对象容器的误报 (#469)
24.4.21
~~~~~~~
* B950: 为行长度忽略项添加 pragma 注释 (#463)
* B909: 添加更多检测用例 + 更多容器变更函数 (#460)
24.2.6
~~~~~~
* B902: 从 B902 检查中移除名为 validator 和 root_validator 的装饰器 (#459)
* B038: 将 B038 更改为 B909 并使其变为可选 (#456)
24.1.17
~~~~~~~
* B038: 将规则限制为仅适用于变异函数 (#453)
24.1.16
~~~~~~~
* B036: 修复在 ``raise`` 语句中抛出非裸名称时导致的崩溃 (#450)
24.1.15
~~~~~~~
* B038: 添加对循环迭代器变更的检查 (#446)
* B037: 添加对 __init__() 中 yield 或 return 值的检查 (#442)
* B017: 使 B017 也适用于 BaseException (#439)
* B036: 添加对未重新抛出 BaseException 的 except 检查 (#438)
23.12.2
~~~~~~~
* B018: 检测所有层级的无用语句 (#434)
* B018: 在 b018 无用表达式输出中添加类名 (#433)
* B018: 在 b018 无用语句检查中包含元组 (#432)
23.11.28
~~~~~~~~
* B035: 修复使用命名表达式时出现的误报 (#430)
23.11.26
~~~~~~~~
* B035: 在 dict-comprehension 中添加对静态键的检查 (#426)
* B902: 为标准库元类添加例外 (#415)
* B017: 修改以消除当 raises() 直接从 pytest 导入时的假阴性 (#424)
* B026: 修复调用者为属性时检查未触发的 bug (#420)
23.9.16
~~~~~~~
* 添加 --classmethod-decorators (#405)
* 修复 python 3.12 上 node_stack 的名称冲突 (#406)
* 使用 pypa/build 来构建包 (#404)
23.7.10
~~~~~~~
* 添加 B034:re.sub/subn/split 必须将 flags/count/maxsplit 作为关键字参数传递。
* 修复 Python 3.12 上的崩溃和多个测试失败,均与 B907
检查相关。
* 声明支持 Python 3.12。
23.6.5
~~~~~~
* 在 MANIFEST.in 中包含 tox.ini 以用于 sdist。 (#389)
* 改进 B033(重复的集合项) (#385)
23.5.9
~~~~~~
* 添加 B033:检测集合中的重复项
* 添加 B908:检测仅包含可能抛出异常的顶层语句的 assertRaises 上下文
* 添加 B028:允许将 stacklevel 显式指定为位置参数
* 移除更多 < 3.8 的检查 / 断言
23.3.23
- flake8-bugbear is now >= 3.8.1 project like flake8>=6.0.0
- This has allowed some more modern AST usage cleanup and less CI running etc.
- B030: Fix crash on certain unusual except handlers (e.g.
except a[0].b:) - Add B033: Check for duplicate items in sets.
23.3.12
* B950: now ignores 'noqa' and 'type: ignore' comments.
* B005: Do not flag when using the ``strip()`` method on an imported module.
* B030: Allow calls and starred expressions in except handlers.
23.2.13
- B906: Add
visit_Bytes,visit_Numandvisit_Strto the list ofvisit_*functions that are ignored by the B906 check. Theast.Bytes,ast.Numandast.Strnodes are all deprecated, but may still be used by some codebases in order to maintain backwards compatibility with Python 3.7. - B016: Warn when raising f-strings.
- Add B028: Check for an explicit stacklevel keyword argument on the warn method from the warnings module.
- Add B029: Check when trying to use
exceptwith an empty tuple i.e.except ():. - Add B030: Check that except handlers only use exception classes or tuples of exception classes. Fixes crash on some rare except handlers.
- Add B031: Check that
itertools.groupby()is not used multiple times. - Add B032: Check for possible unintentional type annotations instead of assignments.
23.1.20
* B024: 现在忽略没有任何方法的类。 (#336)
* B017: 当 ``pytest.raises()`` has a ``match`` 参数时不发出警告。 (#334)
* B906: 忽略无法包含 ast.AST 子节点的 ``visit_`` functions with a ``_fields`` 属性。 (#330)
23.1.17
- Rename B028 to B907, making it optional/opinionated.
23.1.14
* 添加 B906:``visit_`` function with no further calls to a ``visit`` 函数。 (#313)
* 添加 B028:当 f-string 中格式化的值被引号包围时,建议 ``!r``。 (#319)
22.12.6
- Add B905:
zip()without an explicitstrict=parameter. (#314) - B027: ignore @overload when typing is imported with other names (#309)
22.10.27
* B027: 忽略 @overload 装饰器 (#306)
* B023: 同时修复 map (#305)
* B023: 避免 filter、reduce、key= 和 return 的误报。为 functools 添加了测试 (#303)
22.10.25
- Make B015 and B018 messages slightly more polite (#298)
- Add B027: Empty method in abstract base class with no abstract decorator
- Multiple B024 false positive fixes
- Move CI to use
tox(#294) - Move to using PEP621 /
pyproject.tomlpackage (#291) - Tested in 3.11
22.9.23
* 添加 B026:在关键字参数之后查找参数解包 (#287)
* 像 flake8 一样迁移到 setup.cfg (#288)
22.9.11
- Add B025: find duplicate except clauses (#284)
22.8.23
* 在 B024 的消息中添加 B024 错误代码 (#276)
22.8.22
- Add B024: abstract base class with no abstract methods (#273)
22.7.1
* 实现延迟绑定循环检查 (#265)
* `late-binding closures are a classic gotcha <https://docs.python-guide.org/writing/gotchas/#late-binding-closures>`__.
22.6.22
- Don't crash when select / extend_select are None (#261)
- Ignore lambda arguments for B020 (#259)
- Fix missing space typos in B021, B022 error messages (#257)
22.4.25
* 忽略 b013 测试用例的 black 格式化 (#251)
* B010 修复 lambda 误报 (#246)
* B008 修复 lambda 函数的边界情况 (#243)
22.3.23
- B006 and B008: Detect function calls at any level of the default expression (#239)
- B020: Fix comprehension false postives (#238)
- Tweak B019 desc (#237)
22.3.20
* B022: 未向 contextlib.suppress 传递参数 (#231)
* B021: f-string 被用作 docstring。 (#230)
* B020: 确保循环控制变量不会覆盖其迭代的可迭代对象 (#220)
* B019: 检查以查找类方法上的缓存装饰器 (#218)
* 修复长空字符串导致的崩溃 (#223)
22.1.11
- B018: Ignore JoinedStr (#216)
- Build universal Python 3 wheels (#214)
- B950: Add same special cases as E501 (#213)
21.11.29
* B018: 暂时禁用字符串检查 (#209)
21.11.28
- B904: ensure the raise is in the same context with the except (#191)
- Add Option to extend the list of immutable calls (#204)
- Update B014:
binascii.Erroris now treated as a subclass ofValueError(#206) - add simple pre-commit config (#205)
- Test with 3.10 official
- Add B018 check to find useless declarations (#196, #202)
21.9.2
* 修复在 except 语句中调用 _to_name_str 时导致的崩溃 (#187)
* 更新 B006:列表、字典和集合推导式现在也被禁止 (#186)
21.9.1
~~~~~~
* 更新 B008:将更多不可变函数调用加入白名单 (#173)
* 移除 Python 兼容性警告 (#182)
* 添加 B904:检查 ``raise`` without ``from`` in an ``except`` 子句 (#181)
* 添加 Python 3.10 测试以确保通过 (#183)
21.4.3
~~~~~~
* 验证 item_context.args 中的元素对于 b017 是否为 ast.Name 类型
21.4.2
~~~~~~
* 在 b017 的 visit 中为 .func 添加另一个 hasattr() 检查
21.4.1
~~~~~~
* 添加 B017:检查必须捕获所有异常的 assertRaises(Exception)
21.3.2
~~~~~~
* 修复在 try/except 块中元组解包时的崩溃问题 (#161)
21.3.1
~~~~~~
* 修复 B015 中的语法问题 (#150)
* 确保浮点数的无穷大/NaN 不会触发 B008 (#155)
* 处理类方法中的仅限位置参数 (#158)
20.11.1
- 正确支持异常别名(B014)(#129)
- 添加 B015:无意义的比较(#130)
- 移除对 # noqa 注释的检查(#134)
- 忽略非类型的异常类(#135)
- 引入 B016 以检查抛出字面量。(#141)
- 将 types.MappingProxyType() 从 B008 中排除。(#144)
20.1.4
* 忽略 B009/B010 的关键字
20.1.3
- 对非标识符静默 B009/B010
- 说明可能需要忽略可选的 B9x 检查
20.1.2
* 修复 `except (...):` 条款中属性之属性的错误
20.1.1
- 允许在 finally 子句中的循环内使用 continue/break,针对 B012
- 对于 B001,同时检查
except (): - 引入 B013 和 B014 以检查
except (..., ):语句中的元组
20.1.0
* 警告 finally 块中的 continue/return/break (#100)
* 移除了 B008 描述性消息中的一个冒号。 (#96)
19.8.0
- 修复 .travis.yml 语法 + 添加 Python 3.8 + 夜间测试
- 修复
black格式 + 通过 CI 强制执行 - 使 B901 不适用于 await 方法
19.3.0
* 允许元类类方法的第一个参数为 'mcs'(PyCharm 默认)
* 引入 B011
* 引入 B009 和 B010
* 将 tuple() 和 frozenset() 等不可变调用排除在 B008 之外
* 对于 B902,元类类方法的第一个参数可以是
"mcs",与 PyCharm 偏好的名称一致。
18.8.0
- 使用 black 格式化所有 .py 文件
- 检查可变默认值的仅关键字参数
- 测试 Python 3.7
18.2.0
* 打包修复
17.12.0
-
在 trove 分类器中升级为 Production/Stable
-
引入了 B008
17.4.0
* bugfix: 同时检查异步函数以检测 B006 + B902
17.3.0
-
引入了 B903(补丁由 Martijn Pieters 贡献)
-
修复:B902 现在对元类上的实例方法强制使用
cls,对元类上的类方法强制使用metacls
17.2.0
* 引入 B902
* 修复:Syntastic 中不再隐藏 opinionated 警告
* 修复:当命令行使用完整的三位错误代码与 --select 一起使用时,opinionated 警告保持可见
16.12.2
- bugfix: 当用户在配置中指定
ignore =时,不再启用带有主观倾向的警告。 现在,在这种情况下,它们也需要如上所述进行显式选择。
16.12.1
* bugfix: B007 不再在 for 循环中的元组解包时崩溃
16.12.0
-
引入了 B007
-
修复:移除错误格式化中多余的一个冒号,该问题导致 Bugbear 错误在 Syntastic 中不可见
-
在 trove 分类器中标记为 "Beta",已在生产环境中 使用超过 8 个月
16.11.1
* 引入 B005
* 引入 B006
* 引入 B950
16.11.0
-
bugfix: 不要在生成器内的闭包中对 B901 产生误报
-
在 setup.py 中针对 Python 2 优雅地失败
16.10.0
* 引入了 B004
* 引入了 B901,感谢 Markus!
* 将 ``flake8`` 约束更新为至少 3.0.0
16.9.0
~~~~~~
* 引入了 B003
16.7.1
~~~~~~
* bugfix: 不要在 B306 的警告中省略消息代码
* 更改对 ``pep8`` to dependency on `` 的依赖,更新
``flake8`` 约束为至少 2.6.2
16.7.0
~~~~~~
* 引入了 B306
16.6.1
~~~~~~
* bugfix: 修复在类体中包含元组解包的文件上崩溃的问题
16.6.0
~~~~~~
* 引入了 B002、B301、B302、B303、B304 和 B305
16.4.2
~~~~~~
* 打包 herp derp
16.4.1
~~~~~~
* bugfix: 在源代码包中包含测试(以便 ``setup.py test``
对所有人都能正常工作)
* bugfix: 在 setup.py 中显式以 UTF-8 编码打开 README.rst,以适配
默认编码不同的系统
16.4.0
~~~~~~
* 首次发布版本
* 带日期版本号
作者
-------
由 `Łukasz Langa <mailto:lukasz@langa.pl>`_ 整合而成。
`Markus Unterwaditzer <mailto:markus@unterwaditzer.net>`_、
`Martijn Pieters <mailto:github.com@zopatista.com>`_、
`Cooper Lees <mailto:me@cooperlees.com>`_ 和 `Ryan May <mailto:rmay31@gmail.com>`_ 进行了多项改进。