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

Comprehension in class scope should see class variables #1000

Closed
gvanrossum opened this issue Nov 25, 2015 · 5 comments
Closed

Comprehension in class scope should see class variables #1000

gvanrossum opened this issue Nov 25, 2015 · 5 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low topic-python2 issues only applicable to Python 2 topic-runtime-semantics mypy doesn't model runtime semantics correctly

Comments

@gvanrossum
Copy link
Member

Example:

class C:
    A = 'abc'
    B = [c for c in A]

This is valid Python code, but mypy doesn't believe that A exists. (Note that it's specific to comprehensions and generator expressions -- if I put a plain C = A.lower() there it works fine.)

@JukkaL JukkaL added bug mypy got something wrong priority labels Nov 25, 2015
@JukkaL
Copy link
Collaborator

JukkaL commented Nov 25, 2015

Marking this as priority because this also affects unannotated code (class bodies).

@JukkaL JukkaL closed this as completed in 5c8122a Nov 25, 2015
@gvanrossum gvanrossum reopened this Nov 25, 2015
@gvanrossum
Copy link
Member Author

That's fixed, but there's a followup issue where there's a difference between Python 2 and 3. In the following, the reference to A in the condition or in the target expression is valid in Python 2 but not in 3:

class C:
    A = 'abc'
    B = [c for c in 'abcde' if c in A]
    C = [A+c for c in 'abcde']

@gvanrossum
Copy link
Member Author

(This is lower priority I haven't seen this in the wild yet.)

@JukkaL JukkaL removed the priority label Nov 26, 2015
@JukkaL JukkaL added priority-2-low false-positive mypy gave an error on correct code labels May 18, 2018
@JukkaL
Copy link
Collaborator

JukkaL commented May 18, 2018

Clarification: In Python 2, this example works at runtime but mypy generates an error about A:

class C:
    A = 'abc'
    B = [c for c in 'abcde' if c in A]  # 'A' is not defined
    C = [A+c for c in 'abcde']  # 'A' is not defined

In Python 3 mode mypy works as expected.

@AlexWaygood AlexWaygood added topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-python2 issues only applicable to Python 2 labels Mar 27, 2022
@JukkaL
Copy link
Collaborator

JukkaL commented Mar 31, 2022

Closing due to Python 2 feature freeze (#12237).

@JukkaL JukkaL closed this as completed Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low topic-python2 issues only applicable to Python 2 topic-runtime-semantics mypy doesn't model runtime semantics correctly
Projects
None yet
Development

No branches or pull requests

3 participants