Skip to content

Recognize plain aliases of an existing sentinel as sentinel declarations - #21972

Draft
edgarrmondragon wants to merge 3 commits into
python:masterfrom
edgarrmondragon:fix-sentinel-alias
Draft

Recognize plain aliases of an existing sentinel as sentinel declarations#21972
edgarrmondragon wants to merge 3 commits into
python:masterfrom
edgarrmondragon:fix-sentinel-alias

Conversation

@edgarrmondragon

@edgarrmondragon edgarrmondragon commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

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.

FWIW the spec1 doesn't say anything about re-assignment, so this seems valid.

Closes #21971

Footnotes

  1. https://typing.python.org/en/latest/spec/special-types.html#sentinels

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>
@github-actions

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>
@github-actions

Copy link
Copy Markdown
Contributor

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.

PEP 661 sentinel loses type information when reassigned

1 participant