Skip to content

Fix interaction of list comprehension shadowing and binders #7476

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

Merged
merged 1 commit into from
Sep 5, 2019

Conversation

msullivan
Copy link
Collaborator

Binders are keyed by "literal_hash" which uses the name of the
variable as part of the key. Use the Var node instead so that
shadowing works.

Fixes mypyc/mypyc#695 but also was a bug separate from mypyc.

Binders are keyed by "literal_hash" which uses the name of the
variable as part of the key. Use the Var node instead so that
shadowing works.

Fixes mypyc/mypyc#695 but also was a bug separate from mypyc.
# N.B: We use the node itself as the key, and not the name,
# because using the name causes issues when there is shadowing
# (for example, in list comprehensions).
return ('Var', e.node)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we instead push a new binber frame when accepting comprehension to match what Python does?

I mean binder works fine with:

def foo(x: object) -> None:
    if isinstance(x, str):
        def nested() -> None:
            x = 42
            reveal_type(x)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would discard other bindings, right? Which we wouldn't want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, this may cause tons of new false positives (unlike for normal functions where this is questionable, comprehensions are functions executed immediately after definition so always safe).

OK, although this somehow looks dangerous on some unconscious level, I can't really object, so go ahead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it is a little perilous seeming, but the Var node is fundamentally how we represent a variable binding, and having separate Var nodes is how we implement the new scope in a comprehension, so I think it is OK.

@msullivan msullivan merged commit d4151a8 into master Sep 5, 2019
@msullivan msullivan deleted the comprehension-shadowing branch September 5, 2019 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shadowing a variable in a list comprehension can cause type errors
2 participants