-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
frozendict should be covariant on V, like Mapping. #93
Conversation
@Marco-Sulla this is a minor fix for the stubs added in #70. Thanks for the great work on this project. |
mypy output:
|
dict implements MutableMapping, as it's a mutable container and is therefore invariant. frozendict is a somewhat special case, as it implements the (covariant) Mapping interface. By analogy, frozendict is to dict as tuple is to list or frozenset is to set. It's a (essentially covariant) immutable container. I think you've extended this with non-covariant-ish update/set/ror methods, however this can be appropriately typed using a non-covariant V2 parameter returning a union type, as you've done here, because it does not modify the source frozendict. Would you be open to reopening with updates to allow covariant behavior on V, and non-covariant extensions? This is actually a pretty useful behavior for frozen dataclasses where we may want to have a concrete, dict-like property. |
Mh, you're right. This works:
For now I simply reopen this, but I check it more deeply later. |
(Personal rant: Python typing is a mess IMHO, and I repeat IMHO. The syntax is ugly, is quite difficult to implement and to debug.) |
Could you also add this to
|
I definitely ...don't disagree.... 😉 Thanks for pointing me to
In doing so, I found a few small bugs in This fixes a few errors I was seeing in type inference under |
Fantastic, thanks for the review. I'm happy to setup a quick tox-based test config that includes your existing suite and these type tests if you'd like. It might help if you have other contributors come by and could be run in actions. |
I can contribute, but please, let me know about the open points, especially the ones about |
I'm not sure I understand? There aren't any review comments open right now. The test suite with type assertions is passing on this branch w/ mypy and pyright. |
Emh. Can you scroll this discussion up? :D |
In particular all the "Why you removed" etc |
I don't see any review comments, maybe they're in draft? |
K2 = TypeVar("K2") | ||
V2 = TypeVar("V2") | ||
SelfT = TypeVar("SelfT", bound=frozendict[K, V]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you removed bound?
|
||
class frozendict(Mapping[K, V]): | ||
@overload | ||
def __new__(cls: Type[SelfT]) -> SelfT: ... | ||
@overload | ||
def __new__(cls: Type[SelfT], **kwargs: V) -> frozendict[str, V]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you removed many Type[SelfT]
def key(self: SelfT, index: int) -> K: ... | ||
def value(self: SelfT, index: int) -> V: ... | ||
def item(self: SelfT, index: int) -> Tuple[K, V]: ... | ||
@overload |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you removed the overload?
from collections.abc import Hashable | ||
from typing import ItemsView, Iterator, KeysView, Mapping, ValuesView | ||
|
||
from typing_extensions import assert_type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, that's cool! Can this file be removed and this code added to pytest suite?
popd | ||
fi | ||
|
||
if command -v pyright &> /dev/null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What the d@&^ is pyright? :D
@@ -1,3 +1,15 @@ | |||
rm test/core.* | |||
|
|||
./test/debug.py && pytest | |||
|
|||
if command -v mypy &> /dev/null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this code is obsolete now that you changed typed.py. In particular if the code of typed.py will be moved in pytest
Mh maybe I forgot to submit review... |
Superseeded by ebbc70b and subsequent commits. |
Immutable collections, like
Sequence
,tuple
andfrozendict
are covariant in their contained types.The
frozendict
base interface,Mapping[K, V]
, is covariant inV
.Update the
frozendict
.pyi stubs to also specify a covariantV
forfrozendict[K,V]
.This enables us to assign collections-of-subtypes to frozendict generics,
which currently raises a type error to value type invariance.
For example:
Currently raises a type error:
but is a valid assignment.