Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

B909 improvements

オープン
#454 コメント 4 件 リアクション 1 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
python
領域
tooling

調査の方向性

B038Checker、特にそのMUTATING_FUNCTIONSリストとvisit_Callメソッドから始め、#451と#453で導入された動作を確認してください。代入、型注釈付き代入、コンテナ固有のメソッド、setattr、delattrをどのように検出するかを定義し、反復可能オブジェクトへの参照を削除する場合と、それを変更する場合の未解決の区別も解決してください。完了条件は、実装前にスコープについて合意することです。

索引モデルが 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時間
マージ済み PR(30日)
26

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

PyCQA/flake8-bugbear のほかの issue

PyCQA/flake8-bugbear の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。