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

Error referencing local variables in dict comprehensions inside class definitions #78698

Closed
AlexMashianov mannequin opened this issue Aug 27, 2018 · 4 comments
Closed
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@AlexMashianov
Copy link
Mannequin

AlexMashianov mannequin commented Aug 27, 2018

BPO 34517
Nosy @mdickinson, @serhiy-storchaka
Superseder
  • bpo-3692: improper scope in list comprehension, when used in class declaration
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-08-27.09:30:55.284>
    created_at = <Date 2018-08-27.08:42:26.368>
    labels = ['interpreter-core', 'type-bug']
    title = 'Error referencing local variables in dict comprehensions inside class definitions'
    updated_at = <Date 2018-08-27.10:01:47.440>
    user = 'https://bugs.python.org/AlexMashianov'

    bugs.python.org fields:

    activity = <Date 2018-08-27.10:01:47.440>
    actor = 'mark.dickinson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-08-27.09:30:55.284>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2018-08-27.08:42:26.368>
    creator = 'Alex Mashianov'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34517
    keywords = []
    message_count = 4.0
    messages = ['324156', '324158', '324159', '324162']
    nosy_count = 3.0
    nosy_names = ['mark.dickinson', 'serhiy.storchaka', 'Alex Mashianov']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '3692'
    type = 'behavior'
    url = 'https://bugs.python.org/issue34517'
    versions = ['Python 3.6']

    @AlexMashianov
    Copy link
    Mannequin Author

    AlexMashianov mannequin commented Aug 27, 2018

    This code:
    class A:
    a = 1
    b = {str(x): x for x in range(5) if x != a}

    Produces following error:
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
      File "<input>", line 3, in A
      File "<input>", line 3, in <dictcomp>
    NameError: name 'a' is not defined

    Which i think it shouldn't produce. Issue occurs only in dict comprehensions inside class definitions referencing class attributes(which are in local scope during definition).

    @AlexMashianov AlexMashianov mannequin added 3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Aug 27, 2018
    @AlexMashianov
    Copy link
    Mannequin Author

    AlexMashianov mannequin commented Aug 27, 2018

    Update: any class definition comprehension referencing local variables inside filtering block produces this error.

    @AlexMashianov AlexMashianov mannequin removed the 3.7 (EOL) end of life label Aug 27, 2018
    @serhiy-storchaka
    Copy link
    Member

    This is a duplicate of bpo-3692.

    @mdickinson
    Copy link
    Member

    [I wrote this before Serhiy posted his reply; sending it anyway, in case the doc links are useful.]

    This is documented, by-design behaviour. Essentially name resolution skips class scopes.

    See https://docs.python.org/3/reference/executionmodel.html#resolution-of-names, and particularly the example at the end of that section, which is very similar to your example.

    See also: https://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries, and in particular this part:

    However, aside from the iterable expression in the leftmost for clause,
    the comprehension is executed in a separate implicitly nested scope.
    This ensures that names assigned to in the target list don’t “leak” into
    the enclosing scope.

    That "leftmost for clause" bit explains why, in the following example, the first comprehension is evaluated successfully, but the second raises a NameError.

    >>> class A:
    ...     a = 3
    ...     b = [x+y for x in range(a) for y in range(3)]  # okay
    ...     c = [x+y for x in range(3) for y in range(a)]  # raises

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants