Skip to content

fix: preserve PEP 661 sentinels in dict.get inference - #21867

Open
mgalore wants to merge 1 commit into
python:masterfrom
mgalore:fix/dict-get-sentinel-21866
Open

fix: preserve PEP 661 sentinels in dict.get inference#21867
mgalore wants to merge 1 commit into
python:masterfrom
mgalore:fix/dict-get-sentinel-21866

Conversation

@mgalore

@mgalore mgalore commented Aug 18, 2026

Copy link
Copy Markdown

Fixes #21866.

dict.get() infers its default through a type variable. For a PEP 661 sentinel, constraint inference used the Sentinel fallback instance instead of its sentinel literal, producing str | sentinel rather than str | Unknown.

Preserve a sentinel last_known_value when creating a direct type-variable constraint, and add the reported regression case.

Verification:

  • PATH="$PWD/.venv/bin:$PATH" .venv/bin/python -m pytest -n0 -q mypy/test/testcheck.py::TypeCheckSuite::check-sentinels.test
  • PATH="$PWD/.venv/bin:$PATH" .venv/bin/python runtests.py (12,860 + 601 + 55 tests passed; expected skips/xfails)
  • PATH="$PWD/.venv/bin:$PATH" .venv/bin/python runtests.py lint
  • .venv/bin/pre-commit run black --files mypy/constraints.py
  • .venv/bin/pre-commit run ruff-check --files mypy/constraints.py

@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. ✅

@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.

While this appears to solve the issue, I'm not sure we should change constaints.py. We don't do that for enums either.

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

2 participants