Skip to content

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

Merged
merged 8 commits into from
Aug 14, 2022

Conversation

97littleleaf11
Copy link
Collaborator

Description

Closes #10553

Support Final var in literal_hash()

@github-actions

This comment has been minimized.

2 similar comments
@github-actions

This comment has been minimized.

@github-actions

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:
Copy link
Member

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.

Copy link
Collaborator Author

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 Finals.

As for this TypedDict case, one_typeddict[general_var] is forbidden since we only allow TypedDict key to be a string literal.

Copy link
Member

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just added a test.

@97littleleaf11
Copy link
Collaborator Author

This PR only affects Final. Literals still have problems.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@97littleleaf11 97littleleaf11 merged commit c8a2289 into python:master Aug 14, 2022
@97littleleaf11 97littleleaf11 deleted the fix-10553 branch August 14, 2022 12:01
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.

No type narrowing on TypedDict with key name in Final variable
3 participants