Skip to content

unused variables in list comprehension not reported #593

Open

Description

@lilydjwg

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

Activity

  1. asottile commented on Nov 17, 2020

    @asottile
    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]
  2. lilydjwg commented on Nov 18, 2020

    @lilydjwg
    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.

  3. asottile commented on Nov 26, 2020

    @asottile
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions