-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix type narrowing on TypedDict with key name in Final variable #11813
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
Conversation
This comment has been minimized.
This comment has been minimized.
2 similar comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@@ -112,6 +113,8 @@ def literal(e: Expression) -> int: | |||
return LITERAL_NO | |||
|
|||
elif isinstance(e, NameExpr): | |||
if isinstance(e.node, Var) and e.node.is_final and e.node.final_value is not None: |
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.
What other checks will be affected by this? 🤔
Maybe we can add tests for them as well.
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.
Other cases would fallback to LITERAL_TYPE
, which is much conservative than LITERAL_YES
. I think this change only expose those Final
s.
As for this TypedDict case, one_typeddict[general_var]
is forbidden since we only allow TypedDict key to be a string literal.
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.
I think this will affect e.g. if foo[index] is not None
etc. where index
is final for an arbitrary foo
not just TypedDicts. You can add a test for a tuple with final integer position.
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.
Just added a test.
This PR only affects |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Description
Closes #10553
Support
Final
var inliteral_hash()