Skip to content

Make frozendict covariant in its value type - #16669

Open
Parsiffall1 wants to merge 1 commit into
python:mainfrom
Parsiffall1:frozendict-covariant-values
Open

Parsiffall1 wants to merge 1 commit into
python:mainfrom
Parsiffall1:frozendict-covariant-values

Conversation

@Parsiffall1

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor

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

This branch has not been deployed

No deployments
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)

1 participant