Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

B909 improvements

Aperta
#454 4 commenti 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
python
Ambito
tooling

Direzione di ricerca

Inizia con B038Checker, in particolare con la sua lista MUTATING_FUNCTIONS e il metodo visit_Call, e rivedi il comportamento introdotto da #451 e #453. Definisci come rilevare assegnazioni, assegnazioni annotate, metodi specifici dei contenitori, setattr e delattr, risolvendo al contempo la distinzione ancora aperta tra eliminare un riferimento iterabile e mutarlo. Per completare il lavoro è necessario concordare l'ambito prima dell'implementazione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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 🙂

Lingua principale
Python
Stelle
1.1k
Fork
130
Merge medio
1g 20h
PR unite (30g)
26

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di PyCQA/flake8-bugbear

Tutte le issue di PyCQA/flake8-bugbear

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.