Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

B909 improvements

未关闭
#454 4 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
停滞
技术栈
python
领域
tooling

调研方向

从 B038Checker 开始,重点查看其 MUTATING_FUNCTIONS 列表和 visit_Call 方法,并审查 #451 和 #453 引入的行为。明确应如何检测赋值、带注解的赋值、容器特定的方法、setattr 和 delattr,同时解决删除 iterable 引用与对其进行变异之间尚未明确的区别。完成的前提是在实现之前就范围达成一致。

由索引模型根据 Issue 内容生成。

描述

Note Rule B909 originally was B038, but after some discussion, it was decided that we make it optional and it's B909 now (see #456).

I have not updated the content of this issue, so please read "B038" as "B909".


Description

With the introduction of B038 (#445), I initially introduced a bug causing many false positives, which should be fixed with #453.
#453 implements a check for the methods listed in Python Builit-in Mutable Sequence Types documentation, but not the operators.
However, #453 left out some mutations for the sake of getting a fix to the flood of false positives (#451) out asap.

In this issue, I will propose further changes to harder B038, to detect more (hopefully all) mutations to iterables that the python builtins allow.

Assignment Operations

This is a very obvious one that was not implemented yet, and I wonder how I could've overseen it.
This part will cover the following operations:

for _ in foo:
  foo = bar
  foo *= bar
  foo += bar
  foo[i] = bar
  foo[i:j] = bar
  foo[i:j:k] = bar
  foo |= bar
  foo &= bar
  foo -= bar
  foo ^= bar

The implementation will check if any ast.Assign and ast.AugAssign have the name or a subscript of the name of the iterable as target.

Further, to also check for annotated assignments, the plan is to inspect ast.AnnAssign nodes that are not simple (ie have simple = 0).
simple = 1 are nodes of the form foo: bar, whereas simple = 0 have any of the following forms:

for _ in foo:
  foo: t = bar
  foo: t *= bar
  foo: t += bar
  foo[i]: t = bar
  foo[i:j]: t = bar
  foo[i:j:k]: t = bar
  foo: t |= bar
  foo: t &= bar
  foo: t -= bar
  foo: t ^= bar

I'm not sure if all of the above are legal syntax, but the implementation will likely look very similar and thus would cover all those cases out of the box.

Further, everything of the above also applies to object attributes, ie, we'd also cover foo.bar = -esque assignments if foo.bar is the loop iterable.

Container-Specific Operations

#451 already implements a check for 2 container specific operations: list.sort() and dict.popitem() .
Further operations are missing:

These all can be implemented similarly to the ones in #451 by adding them to the MUTATING_FUNCTIONS list of the B038Checker.

The nasty ones

These are functions that are not used often, or just don't fall into the scheme of the ones listed above:

These will need special handling in visit_Call of the B038Checker, but shouldn't be too complicated.

Discussion

I hope my proposal covers all the mutations that are doable with the python built-ins.
If not please let me know.

One special mention goes to del x - this is actually not mutating the iterable itself and only deletes the reference to the iterator.
The following snippet runs without problems

>>> a = [1,2,3]
>>> for i in a:
...   if i == 1:
...     del a
...   print(i)
... 
1
2
3

However, def x[y] does mutate x.
Should this case distinction be made? Currently both would be reported.

I also would like to refactor the code for B038 a bit to just make it far more readable, currently it's a bit messy 😅


Please let me know what you think about the proposed changes and whether I should go ahead and implement them 🙂

主要语言
Python
星标
1.1k
派生
130
平均合并
1 天 20 小时
30 天内合并 PR
26

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

PyCQA/flake8-bugbear 的其他 Issue

查看 PyCQA/flake8-bugbear 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。