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

unused variables in list comprehension not reported #593

Open
lilydjwg opened this issue Nov 17, 2020 · 3 comments
Open

unused variables in list comprehension not reported #593

lilydjwg opened this issue Nov 17, 2020 · 3 comments

Comments

@lilydjwg
Copy link

Test code:

def t(items):
  return ['{x}' for x in items]

There is a missing f prefix which makes the local x variable unused, but pyflakes doesn't notice this.

$ pyflakes --version
2.2.0 Python 3.8.6 on Linux
@asottile
Copy link
Member

I agree this particular example should probably produce an error, but pyflakes is a bit timid about assignments in unpackings

for example, should this produce a warning (pyflakes currently assumes any sort of unpacking can introduce variables that don't need to be referenced)?

return [f'{x} for x, y in items]

@lilydjwg
Copy link
Author

Aren't unused names expected to be prefixed with _? If not, maybe pyflakes can detect missing f-prefixes in another way?

This kind of errors is not easy to notice otherwise: the types check with any issue; the {x} syntax inside normal strings is also special (for .format) so it's highlighted anyway.

@asottile
Copy link
Member

that's not something that's errorable either

consider this valid snippet:

FMT_S = 'foo {bar}'

def make_thing():
    return FMT_S.format(bar=...)

(of course this is a silly example that could be inlined, but it is still a case that comes up). this would introduce a high-false-positive rule which isn't really appropriate for pyflakes and is better left to a flake8 plugin or pylint

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