Skip to content

Fix internal error on list/dict comprehension with walrus operator in global scope #9062

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

Conversation

dhood
Copy link
Contributor

@dhood dhood commented Jun 28, 2020

Fixes #8986

The reported issue is for list comprehensions involving assignment expressions, though I noticed that dictionary comprehensions are also impacted.

# mwe.py
lst = [1, 2, 3, 4]
processed_list = [e for elt in lst if (e := elt - 1) > 1]

dct = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
processed_dict = {k: new_v for k, v in dct.items() if (new_v := v - 1) > 1

On the other hand, the same declarations inside a function pass:

# valid_comprehensions.py
def calling_function() -> None:
    lst = [1, 2, 3, 4]
    processed_list = [e for elt in lst if (e := elt - 1) > 1]
    reveal_type(processed_list)

    dct = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    processed_dict = {k: new_v for k, v in dct.items() if (new_v := v - 1) > 1}
    reveal_type(processed_dict)

This tipped me off to it being related to the special logic added in #6899 to store the variables in the scope of the comprehension's caller, which I believe can benefit from special treatment for the edge case of the caller being in the global scope. That's the approach that I took, though it's my first contribution to MyPy so another approach may be more appropriate.

I added regression tests for these cases, but I also added some tests that were already passing. I've kept them in separate commits (with CI triggered separately) to be clear about which ones are being addressed by this PR!

reveal_type(prefix) # N: Revealed type is 'builtins.str'
reveal_type(start_val) # N: Revealed type is 'builtins.int'

filtered_dict = {k: new_v for k, v in [('a', 1), ('b', 2), ('c', 3)] if (new_v := v + 1) == 2}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If reviewers are wondering why I didn't just use d.items() here instead of the list of tuples, it's because it's causing error: "Dict[str, int]" has no attribute "items".
I couldn't figure out why that was happening, I tried different flags and casting and whatnot but something in the test environment must be different... in a regular python script filtered_dict = {k: new_v for k, v in d.items() if (new_v := v + 1) == 2} can have its type revealed without such an error. Open to suggestions if you have ideas what's causing that!

Copy link
Member

Choose a reason for hiding this comment

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

This is almost certainly because the test fixture you're using happens not to define dict.items. What you're doing here is fine.

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Thanks for catching this! Sorry for missing it when I wrote the code. Note that the specification for this behavior is in https://www.python.org/dev/peps/pep-0572/#scope-of-the-target.

Could you also add some tests with assignments happening in class scopes?

mypy/semanal.py Outdated
# The caller of the comprehension is in the global space.
names = self.globals
else:
names = cast(SymbolTable, self.locals[-1 - i])
Copy link
Member

Choose a reason for hiding this comment

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

Why is this cast() needed? Could you use a # type: comment on the first assignment instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the cast here was because self.locals is a list of Optional[SymbolTable], and so without it, you would get:

mypy\semanal.py:4552: error: Incompatible types in assignment (expression has type "Optional[SymbolTable]", variable has type "SymbolTable")  [assignment]
                                names = self.locals[-1 - i]
                                        ^

I reasoned (at the time) that it is a safe cast as long as we are not retrieving the None that is the first element in the list.

self.locals = [None]

However, now that you have mentioned to check the class scope, I have realised that there may be other elements in the list that are None:

self.locals.append(None) # Add class scope

Perhaps once I deal with the class case properly this cast will not be necessary. Either way, for now, my original assumption that only the first entry is None seems invalid! So I will find an alternative approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since comprehensions in a class scope e.g.

class Example:
    [(j := i) for i in range(5)]

can also result in the retrieved self.locals[-1 -i] symbol table being None, I added extra logic for that case, which removed the need for the cast, in a68bd68.

Because this specific case is called out in https://www.python.org/dev/peps/pep-0572/#scope-of-the-target as being invalid syntax (I know that you know this, I am just being verbose), the specific logic that's added is actually just an assertion that we don't end up in that case.

@dhood
Copy link
Contributor Author

dhood commented Jun 29, 2020

Sorry for missing it when I wrote the code.

No, not at all. Certainly no anger or frustration from me, I am grateful of the initial implementation that you wrote! (which worked great in most cases, this is just an edge case). Actually I was excited to stumble upon this bug... I am very happy to have something small to contribute towards the support of the walrus operator! 😄

Will add those class-scope tests and ping you again.

@dhood dhood marked this pull request as draft June 29, 2020 22:14
dhood added 2 commits July 5, 2020 19:43
It may be None in the case of a list comprehension being declared
in a class scope (not in a function of a class). This, however,
is not valid python syntax.
@dhood
Copy link
Contributor Author

dhood commented Jul 5, 2020

@JelleZijlstra, I added tests for the class scope in 8585a4f

In doing so, I noticed some cases of what I consider inappropriate mypy behaviour:

  1. The invalid syntax highlighted in https://www.python.org/dev/peps/pep-0572/#scope-of-the-target where a comprehension is used in a class scope does not raise a Syntax error. I opened PEP 572: Invalid python syntax for walrus operator not raising parser error #9095
  2. Use of assignment operator within a list and subsequent re-use of the variable in the same list e.g. l = [x2 := 1, x2 + 2] from a class scope does not work. This is another variation of Walrus operators unrecognised in dictionary initialisation of a class attribute #8379, so I have commented there.
  3. Re-use of variables defined in comprehensions (in any scope) is not permitted. Opened PEP 572: re-use of variable defined with assignment expression / walrus operator not permitted #9096

Since I consider these to be separate issues (they happen on master also), and because I didn't plan time to address those this weekend, I have spun them off to not block this PR.

@dhood dhood marked this pull request as ready for review July 5, 2020 10:30
@JelleZijlstra JelleZijlstra merged commit 5a9d022 into python:master Aug 4, 2020
@dhood
Copy link
Contributor Author

dhood commented Aug 4, 2020

thanks for the review, @JelleZijlstra - I'll try to make progress on the spin-off issues this weekend

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.

Bug: INTERNAL ERROR from := assignment expression within list comprehension
2 participants