B023 false alarms

See original GitHub issue

When 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:closed
  • Created a year ago
  • Reactions:4
  • Comments:24 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
Zac-HDcommented, Jul 4, 2022

Yep, this is one of those false alarms! I’d use a list-comprehension instead, which is both faster and IMO more readable:

def filter_values(values: list[list[int]], max_percentage: float):
    for value in values:
        filter_val = max(value) * max_percentage
        yield [x for x in values if x < filter_val]

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.

2reactions
AbdealiLoKocommented, Jul 4, 2022

Not the same usecase as described here originally, but I found a false alarm with the following code:

$ cat app.py
from app import db


for name in ['a', 'b']:
    def myfunc(name):
        qry = db.query(name=name)
        db.run(qry)

    myfunc(name=name)
    qry = db.query(description__contains=name)
    db.run(qry)

And when I run flake8:

$ venv/bin/flake8 app.py
app.py:7:16: B023 Function definition does not bind loop variable 'qry'.

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found