Skip to content

name resolution in class-level list comprehension (python 2) #5708

Description

@bwo

This code is legal in Python 2:

class A(object):
    x = 1
    y = [x + 1 for _ in range(10)]

It seems not to be legal in Python 3.

However, even with -2, mypy does not allow it:

~/src 12:38:46 $ python3 foo.py
Traceback (most recent call last):
  File "foo.py", line 1, in <module>
    class A(object):
  File "foo.py", line 3, in A
    y = [x + 1 for _ in range(10)]
  File "foo.py", line 3, in <listcomp>
    y = [x + 1 for _ in range(10)]
NameError: name 'x' is not defined
~/src 12:38:55 $ python foo.py
~/src 12:38:57 $ mypy -2 foo.py
foo.py:3: error: Name 'x' is not defined

Expected behavior: the code should typecheck in py2 mode.

ETA: version 0.550; apologies if this is fixed. (Confirmed on 0.630.)

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions