Skip to content

False negative for undefined-variable when attempting to access class scope from comprehension #3494

Closed
@anjsimmo

Description

@anjsimmo

Steps to reproduce

Consider the following example from the Python execution model reference here:

class A:
    a = 42
    b = list(a + i for i in range(10))

As per Python's documentation, executing this file will fail with the message NameError: name 'a' is not defined. (The class attribute a is considered out of scope from within the body of the generator expression. As of Python3, it would also be out of scope from within the body of a list comprehension.)

Current behavior

Pylint assumes that the class attribute a is visible from within the generator expression, and doesn't give a warning (i.e. a false negative).

Expected behavior

Pylint should print an undefined-variable warning when attempting to access class attributes from within the body of a generator expression or comprehension.

pylint --version output

pylint 2.4.4
astroid 2.3.3
Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
[GCC 7.3.0]

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