Skip to content

Fix sentinel identity loss through generic substitution and inference - #21888

Open
edgarrmondragon wants to merge 2 commits into
python:masterfrom
edgarrmondragon:sentinel-typevar-expand
Open

Fix sentinel identity loss through generic substitution and inference#21888
edgarrmondragon wants to merge 2 commits into
python:masterfrom
edgarrmondragon:sentinel-typevar-expand

Conversation

@edgarrmondragon

@edgarrmondragon edgarrmondragon commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

A sentinel's only way to identify itself is via its attached literal value, unlike e.g. enum members whose class already carries identity. That literal was being unconditionally stripped both when a TypeVar was substituted with a sentinel instance (e.g. dict.get's overloaded default parameter) and when inferring the type of a plain variable assignment, collapsing every sentinel down to the same uninformative sentinel type. Preserve it in both cases.

Fixes #21866
Follow-up from #21647

A sentinel's only way to identify itself is via its attached literal
value, unlike e.g. enum members whose class already carries identity.
That literal was being unconditionally stripped both when a TypeVar
was substituted with a sentinel instance (e.g. `dict.get`'s overloaded
default parameter) and when inferring the type of a plain variable
assignment, collapsing every sentinel down to the same uninformative
`sentinel` type. Preserve it in both cases.

Fixes python#21866
Follow-up from python#21647

Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>

assert_type(ALIAS, sentinel)
# The value still identifies as the same sentinel...
assert_type(ALIAS, MISSING)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is more useful now and still in line with #21647 (comment) and the spec:

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me, do you think something: sentinel = MISSING should pass or fail?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should pass. It does seem intuitive that a sentinel instance should be assignable to the sentinel type.

Related:

@A5rocks A5rocks Aug 25, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, could you add one more test case for that? (If there's not one already)

@github-actions

This comment has been minimized.

@edgarrmondragon
edgarrmondragon marked this pull request as ready for review August 24, 2026 19:19

@cdce8p cdce8p left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into it @edgarrmondragon! The changes make sense and look good to me.

@cdce8p
cdce8p requested a review from JelleZijlstra August 24, 2026 21:59

@A5rocks A5rocks left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix makes sense to me.

Comment thread mypy/erasetype.py
return t
if t.last_known_value is not None and t.last_known_value.is_sentinel_literal():
# Sentinel values (PEP 661) have no other way to identify themselves than
# via their literal, unlike e.g. enum members, so it must be preserved.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh I'm a bit frightened about storing everything in last_known_value :-)

I assume this was already litigated in an early PR and there's no better cleaner method?

@edgarrmondragon edgarrmondragon Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think it was litigated at all. There's perhaps a better way to do all of this for sentinel without playing whack-a-mole with last_known_value (e.g. ff4d71b), but it surely involves a large refactor to handle sentinels in a similar manner to enums and NewType.


assert_type(ALIAS, sentinel)
# The value still identifies as the same sentinel...
assert_type(ALIAS, MISSING)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me, do you think something: sentinel = MISSING should pass or fail?


[builtins fixtures/sentinel.pyi]

[case testSentinelReassignmentIsNotTypeAlias]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[case testSentinelReassignmentIsTypeAlias]

It sounds to me like saying reassignment isn't a type alias was dropped from the spec. Maybe I'm misreading!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I think the current name is technically correct but maybe a bit misleading.

Reassignment creates an instance of the same type, but doesn't create a valid type, i.e. the new variable is not a type alias.

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.

Invalid return type with sentinel and dict.get

3 participants