-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[ty] pass type context to sequence literals in binary operations #24197
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
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
|fallback literal inferenceWhen the left operand is a dict literal and the
TypedDictfast-path rejects it, this fallback now re-infers the left literal withoperand_tcx(left), which may be aTypedDicttarget context. That reintroduces the same spuriousmissing-typed-dict-key/invalid-keydiagnostics this branch is meant to avoid (the comment still says it should re-infer withoutTypedDictcontext). In cases like assigning a merged dict expression to aTypedDict, the left partial literal can now emit premature key errors purely because of the outer context.Useful? React with 👍 / 👎.
Uh oh!
There was an error while loading. Please reload this page.
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 confusing, and I'm not sure what we actually want to have happen here. The situation is:
If inferring
expression2with the default context yields aTypedDicttype, then we speculatively check whetherdict_literalmatches that type. If it doesn't match, then currently on the main branch we fall back to inferringdict_literalalso with the default context. But it seems like it might be more correct to inferdict_literalwith the inheritedMyTypedDictcontext? In fact it seems like it might be more correct to infer both sides that way.On the other hand, do we care to support a wacky situation like this:
The problem there is that the unioned value is a valid
MyTypedDict, but the two halves of it aren't, so passingMyTypedDictas type context to either side will result in errors. On the other hand, this is pretty convoluted, and we don't support it today in any case. @ibraheemdev what's your instinct here?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 we failed to perform the speculative typed dict update,
dict_literalis not even a valid subset ofMyTypedDict, and so inferring it asMyTypedDictdirectly would likely lead to a lot ofmissing-key-errors that don't seem very useful.pyright and mypy doesn't seem to support this either, so it seems fine to ignore it. Maybe we should just ignore
TypedDictcontext entirely outside of the speculative updates to avoid the extra errors, if there is no way to produce a validTypedDictotherwise (unless there is a case I am missing)?