flake8-bugbear icon indicating copy to clipboard operation
flake8-bugbear copied to clipboard

How about checking that you're not using a list for an in?

Open yajo opened this issue 5 years ago • 2 comments

A very common pattern:

candidates = [some for some in another_stuff()]
if me in candidates:
    do_stuff()

If candidates is only used for an in, then we have 2 alternatives that are going to be more performant than the above in 100% of the cases:

Using a generator:

-candidates = [some for some in another_stuff()]
+candidates = (some for some in another_stuff())

Or using a set:

-candidates = [some for some in another_stuff()]
+candidates = {some for some in another_stuff()}

How about linting this?

yajo avatar Oct 06 '20 08:10 yajo

I like the intentions here, but feel it could be noisy.

Main reason: How do we know that the code does not need it to be a list elsewhere?

cooperlees avatar Oct 19 '20 01:10 cooperlees

El dom, 18 de oct de 2020 a las 18:25, Cooper Lees [email protected] escribió:

Main reason: How do we know that the code does not need it to be a list elsewhere?

Maybe just checking that it is a local variable that goes out of scope without further usage? I don't know how complex that can be...

yajo avatar Oct 20 '20 14:10 yajo