Skip to content
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

Introduce temporary named expressions for match subjects #18446

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

sterliakov
Copy link
Collaborator

@sterliakov sterliakov commented Jan 11, 2025

Fixes #18440. Fixes #17230.

#16503 did this specifically for CallExpr, but that isn't the only kind of such statements. I propose to expand this for more general expressions and believe that a blacklist is more reasonable here: we do not want to introduce a temporary name only for certain expressions that either are already named or can be used to infer contained variables (inline tuple/list/dict/set literals).

Writing logic to generate a name for every other kind of expression would be quite cumbersome - I circumvent this by using a simple counter to generate unique names on demand.

This comment has been minimized.

@sterliakov
Copy link
Collaborator Author

Whoa, I expected this to produce at least a couple of unused ignore diagnostics, but apparently pattern matching isn't very popular in wild...

@sterliakov sterliakov marked this pull request as ready for review January 11, 2025 21:44

This comment has been minimized.

@sterliakov

This comment was marked as resolved.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

@sterliakov
Copy link
Collaborator Author

This interferes with recently added narrowing by item/attribute (aka discriminated union, testMatchNarrowingUnionClassViaAttribute). So a simple solution like this won't fly. This information for index/attr should be stored in some other way to support narrowing of attr/item and the object itself at the same time. This may still work for other, less informative nodes (await?)

@sterliakov sterliakov closed this Mar 29, 2025
@sterliakov sterliakov reopened this Apr 4, 2025
@sterliakov sterliakov marked this pull request as draft April 4, 2025 22:41

This comment has been minimized.

@sterliakov
Copy link
Collaborator Author

To support narrowing the subject and its "dependencies" simultaneously, I propose to just push the narrowed type of subject to typemap as the type of its original form, allowing for any inference on it.

@sterliakov sterliakov marked this pull request as ready for review April 5, 2025 01:28
Copy link
Contributor

github-actions bot commented Apr 5, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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.

Type narrowing broken with dict items match narrowing fails when the operand is an await expression
1 participant