B023 false alarms
See original GitHub issueWhen upgrading, some false positives were found for B023 (implimented in https://github.com/PyCQA/flake8-bugbear/pull/265).
Example
"""B023.py"""
def filter_values(values: list[list[int]], max_percentage: float):
for value in values:
filter_val = max(value) * max_percentage
yield list(filter(lambda x: x < filter_val, value))
$ flake8 B023.py
bugbear_me.py:4:41: B023 Function definition does not bind loop variable 'filter_val' is valid, and doesn't fall into the
A silly example here, but one where the use of filter_val is valid.
cc @Zac-HD In the PR you mentioned some hard to detect false positives that you were okay with. Was this kind of pattern one of the ones you had in mind?
Thanks for this check btw, I think it’s a good idea. I’ve fallen for this in the past!
Issue Analytics
- State:
- Created a year ago
- Reactions:4
- Comments:24 (12 by maintainers)
Top Results From Across the Web
Fix some B023 false alarms #303 - PyCQA/flake8-bugbear
Fixes some common false alarms where a function is immediately consumed, as an argument to filter or reduce , as a keyword argument...
Read more >also fix map in B023 by jakkdl · Pull Request #305 · PyCQA/flake8 ...
Successfully merging this pull request may close these issues. B023 false alarms. 2 participants. @jakkdl · @Zac ...
Read more >flake8-bugbear - PyPI
B011: Do not call assert False since python -O removes these calls. ... B023: Avoid false alarms with filter, reduce, key= and return....
Read more >INSTRUCTION MANUAL
However, to prevent false alarms while your baby is not sleeping (such as while breast feeding, changing the baby's diaper, tummy time, etc.),...
Read more >flake8-bugbear: Docs, Community, Tutorials, Reviews | Openbase
N.B. this rule currently does not flag suppress calls to avoid potential false positives due to similarly named user-defined functions. B023: Functions ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yep, this is one of those false alarms! I’d use a list-comprehension instead, which is both faster and IMO more readable:
Unfortunately it’s more difficult to avoid this false alarm than you might think: for example “lambdas are OK if they’re the first argument in a call to
filter()” would miss bugs where the filter iterable (and therefore lambda) isn’t immediately evaluated.Not the same usecase as described here originally, but I found a false alarm with the following code:
And when I run flake8:
Basically - if I have a variable defined within the loop - but the same variable name is also within the function. The local variable shouldn’t have any problem in terms of execution ?
But - the rule thinks it is an issue