Recognize plain aliases of an existing sentinel as sentinel declarations - #21972
Draft
edgarrmondragon wants to merge 3 commits into
Draft
Recognize plain aliases of an existing sentinel as sentinel declarations#21972edgarrmondragon wants to merge 3 commits into
edgarrmondragon wants to merge 3 commits into
Conversation
Currently `is_sentinel_declaration` only recognizes the exact `NAME = Sentinel("NAME")` call shape, so a re-export like `ALIAS = NAME` (same module or via `import mod; ALIAS = mod.NAME`) silently loses is_sentinel, and ALIAS becomes invalid in type position.
This is exactly the pattern pydantic 2.14 uses to re-export MISSING from pydantic_core: `MISSING = pydantic_core.MISSING`.
Extend is_sentinel_declaration/setup_sentinel_var to also accept a `RefExpr` rvalue (`NameExpr` or `MemberExpr`) that resolves to a Var already flagged `is_sentinel`, reusing its already-computed `LiteralType` rather than requiring a fresh `Sentinel(...)` call.
Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
This comment has been minimized.
This comment has been minimized.
…signment does not result in a special form Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
Currently
is_sentinel_declarationonly recognizes the exactNAME = Sentinel("NAME")call shape, so a re-export likeALIAS = NAME(same module or viaimport mod; ALIAS = mod.NAME) silently losesis_sentinel, andALIASbecomes invalid in type position.This is exactly the pattern pydantic 2.14 uses to re-export
MISSINGfrom pydantic_core:MISSING = pydantic_core.MISSING.Extend
is_sentinel_declaration/setup_sentinel_varto also accept aRefExprrvalue (NameExprorMemberExpr) that resolves to a Var already flaggedis_sentinel, reusing its already-computedLiteralTyperather than requiring a freshSentinel(...)call.FWIW the spec1 doesn't say anything about re-assignment, so this seems valid.
Closes #21971
Footnotes
https://typing.python.org/en/latest/spec/special-types.html#sentinels ↩