-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Fix internal error on list/dict comprehension with walrus operator in global scope #9062
Conversation
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} |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this 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]) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Line 226 in ed9ba4f
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:
Line 1166 in ed9ba4f
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.
There was a problem hiding this comment.
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.
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. |
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.
@JelleZijlstra, I added tests for the class scope in 8585a4f In doing so, I noticed some cases of what I consider inappropriate mypy behaviour:
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. |
thanks for the review, @JelleZijlstra - I'll try to make progress on the spin-off issues this weekend |
Fixes #8986
The reported issue is for list comprehensions involving assignment expressions, though I noticed that dictionary comprehensions are also impacted.
On the other hand, the same declarations inside a function pass:
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!