Skip to content

Make frozendict covariant in its value type - #16669

Merged
srittau merged 2 commits into
python:mainfrom
Parsiffall1:frozendict-covariant-values
Nov 2, 2026
Merged

srittau merged 2 commits into
python:mainfrom
Parsiffall1:frozendict-covariant-values

Conversation

@Parsiffall1

Copy link
Copy Markdown
Contributor

Fixes #16668.

frozendict is immutable, so like Mapping it can be covariant in its value type. The key type stays invariant, since keys are still accepted as parameters (__getitem__, get), which matches Mapping[_KT, _VT_co].

The class now uses a new _VT_co type variable. get(key, default: _VT_co) carries the same ignore comments Mapping.get uses for a covariant parameter. The __new__ overloads keep their own _VT, which is solved per call.

Added test cases: a frozendict[str, int] is accepted where frozendict[str, int | None] is expected, get() still narrows as before, and the key type is still invariant.

@github-actions

This comment has been minimized.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau 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!

@srittau
srittau merged commit 93fc928 into python:main Nov 2, 2026
88 checks passed
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.

frozendict is invariant (python 3.15)

3 participants