Repository navigation
Allow building type qualifiers like NotRequired[...] at runtime - #5450
Open
MarcoGorelli wants to merge 1 commit into
Open
MarcoGorelli wants to merge 1 commit into
MarcoGorelli wants to merge 1 commit into
Conversation
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D128312510. (Because this pull request was imported automatically, there will not be any future comments.) |
This comment has been minimized.
This comment has been minimized.
Code that builds qualifiers dynamically, such as
`{key: NotRequired[hint] for key, hint in hints.items()}` (used by
huggingface-hub), was rejected with "`NotRequired` is not allowed in this
context". A plain value expression was checked as if it were a type
expression, so the subscript was treated as a misplaced annotation.
Fixes meta#5429.
A subscript of `ClassVar`, `Final`, `ReadOnly`, `Required`, or `NotRequired`
that has no type-form context is now a runtime value of type `object`, which
matches typeshed's `_SpecialForm.__getitem__`. Misuse is still caught: using
such a value as an annotation reports "Expected a type form", and class
keywords like `extra_items=NotRequired[int]` are now checked as type forms,
so they keep their existing error.
MarcoGorelli
force-pushed
the
fix-qualifier-runtime-value
branch
from
October 29, 2026 14:49
f228845 to
5ced393
Compare
MarcoGorelli
marked this pull request as ready for review
October 29, 2026 14:59
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The idea is:
x = my_list[0]where we know thatmy_listis alist[int], thenxgets inferred asint. That's whatpyrefly/pyrefly/lib/alt/expr.rs
Lines 945 to 951 in 8003f9b
x = NotRequired[int], thenNotRequiredis a_SpecialFormwhich is typed to returnobjectfor__getitem__, so we need to inferxasobjectrather than reporting it as an invalid annotationclass MyClass(TypedDict, total=False), however, we want to preserve the current behaviour and raise on something likeextra_items=NotRequired[int], which is why infn expr_class_keywordwe need to pass downSome(TypeFormContext::ClassKeyword)so thatif type_form_context.is_none()inexpr_infer_without_shape_commentdoesn't evaluate totrueNote that
x: NotRequired[int]still gets checked as-before, as this change only covers theif type_form_context.is_none()case, and in thex: NotRequired[int]case, there is a type form contextFixes #5429
I used Claude to generate the code, but the description here is fully written by me
Test Plan