Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

B909 improvements #454

Open
mimre25 opened this issue Jan 17, 2024 · 4 comments
Open

B909 improvements #454

mimre25 opened this issue Jan 17, 2024 · 4 comments

Comments

@mimre25
Copy link
Contributor

mimre25 commented Jan 17, 2024

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 🙂

@JelleZijlstra
Copy link
Collaborator

Thanks for writing this up.

Assign and AnnAssign can be ignored, since they just rebind the name, they do not mutate the container. (foo: T += 3 and similar are syntax errors.) Similarly, I wouldn't worry about the attribute setting mechanisms (setattr/delattr/etc.), because you can't set an attribute on builtin containers like list, tuple, set, and dict. There could be user-defined subclasses where setting an attribute interferes with iteration, but that feels too speculative for a lint rule.

@mimre25
Copy link
Contributor Author

mimre25 commented Jan 18, 2024

A good catch with Assign and AnnAssign.

Regarding the syntax errors: you're right - AugAssign and AnnAssign cannot be mixed.

What do you think about del?
Should we also remove that, when it only targets the container itself instead of an element inside of it?

@JelleZijlstra
Copy link
Collaborator

We should catch del container[i], but not del container.

@mimre25
Copy link
Contributor Author

mimre25 commented Jan 18, 2024

Sounds good to me.

I'll implement these improvements some time next week 🙂

@mimre25 mimre25 changed the title B038 improvements B909 improvements Feb 8, 2024
charliermarsh pushed a commit to astral-sh/ruff that referenced this issue Apr 11, 2024
## Summary
This PR adds the implementation for the current
[flake8-bugbear](https://github.com/PyCQA/flake8-bugbear)'s B038 rule.
The B038 rule checks for mutation of loop iterators in the body of a for
loop and alerts when found.

Rational: 
Editing the loop iterator can lead to undesired behavior and is probably
a bug in most cases.

Closes #9511.

Note there will be a second iteration of B038 implemented in
`flake8-bugbear` soon, and this PR currently only implements the weakest
form of the rule.
I'd be happy to also implement the further improvements to B038 here in
ruff 🙂
See PyCQA/flake8-bugbear#454 for more
information on the planned improvements.

## Test Plan
Re-using the same test file that I've used for `flake8-bugbear`, which
is included in this PR (look for the `B038.py` file).


Note: this is my first time using `rust` (beside `rustlings`) - I'd be
very happy about thorough feedback on what I could've done better
:slightly_smiling_face: - Bring it on :grinning:
Glyphack pushed a commit to Glyphack/ruff that referenced this issue Apr 12, 2024
…l-sh#9578)

## Summary
This PR adds the implementation for the current
[flake8-bugbear](https://github.com/PyCQA/flake8-bugbear)'s B038 rule.
The B038 rule checks for mutation of loop iterators in the body of a for
loop and alerts when found.

Rational: 
Editing the loop iterator can lead to undesired behavior and is probably
a bug in most cases.

Closes astral-sh#9511.

Note there will be a second iteration of B038 implemented in
`flake8-bugbear` soon, and this PR currently only implements the weakest
form of the rule.
I'd be happy to also implement the further improvements to B038 here in
ruff 🙂
See PyCQA/flake8-bugbear#454 for more
information on the planned improvements.

## Test Plan
Re-using the same test file that I've used for `flake8-bugbear`, which
is included in this PR (look for the `B038.py` file).


Note: this is my first time using `rust` (beside `rustlings`) - I'd be
very happy about thorough feedback on what I could've done better
:slightly_smiling_face: - Bring it on :grinning:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants