Skip to content
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

Make Mapping covariant in keys (too). #273

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions python2/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,15 +838,15 @@ def test_covariance_sequence(self):
typing.Sequence[Manager])

def test_covariance_mapping(self):
# Ditto for Mapping (covariant in the value, invariant in the key).
# Ditto for Mapping (covariant in key and value).
self.assertIsSubclass(typing.Mapping[Employee, Manager],
typing.Mapping[Employee, Employee])
self.assertNotIsSubclass(typing.Mapping[Manager, Employee],
typing.Mapping[Employee, Employee])
self.assertIsSubclass(typing.Mapping[Manager, Employee],
typing.Mapping[Employee, Employee])
self.assertNotIsSubclass(typing.Mapping[Employee, Manager],
typing.Mapping[Manager, Manager])
typing.Mapping[Manager, Manager])
self.assertNotIsSubclass(typing.Mapping[Manager, Employee],
typing.Mapping[Manager, Manager])
typing.Mapping[Manager, Manager])


class CastTests(BaseTestCase):
Expand Down
7 changes: 5 additions & 2 deletions python2/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ def __subclasscheck__(self, cls):
VT = TypeVar('VT') # Value type.
T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
KT_co = TypeVar('KT', covariant=True) # Key type covariant containers.
VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.

Expand Down Expand Up @@ -1391,8 +1392,10 @@ class MutableSet(AbstractSet[T]):
__extra__ = collections_abc.MutableSet


# NOTE: It is only covariant in the value type.
class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co]):
# NOTE: Covariance in the key type is theoretically problematic because
# there are methods with KT parameters, but in this case it's okay.
# See https://github.com/python/typeshed/issues/510.
class Mapping(Sized, Iterable[KT_co], Container[KT_co], Generic[KT_co, VT_co]):
__extra__ = collections_abc.Mapping


Expand Down
10 changes: 5 additions & 5 deletions src/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,15 +863,15 @@ def test_covariance_sequence(self):
typing.Sequence[Manager])

def test_covariance_mapping(self):
# Ditto for Mapping (covariant in the value, invariant in the key).
# Ditto for Mapping (covariant in key and value).
self.assertIsSubclass(typing.Mapping[Employee, Manager],
typing.Mapping[Employee, Employee])
self.assertNotIsSubclass(typing.Mapping[Manager, Employee],
typing.Mapping[Employee, Employee])
self.assertIsSubclass(typing.Mapping[Manager, Employee],
typing.Mapping[Employee, Employee])
self.assertNotIsSubclass(typing.Mapping[Employee, Manager],
typing.Mapping[Manager, Manager])
typing.Mapping[Manager, Manager])
self.assertNotIsSubclass(typing.Mapping[Manager, Employee],
typing.Mapping[Manager, Manager])
typing.Mapping[Manager, Manager])


class CastTests(BaseTestCase):
Expand Down
9 changes: 6 additions & 3 deletions src/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ def __subclasscheck__(self, cls):
VT = TypeVar('VT') # Value type.
T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
KT_co = TypeVar('KT', covariant=True) # Key type covariant containers.
VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.

Expand Down Expand Up @@ -1454,13 +1455,15 @@ class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
pass


# NOTE: It is only covariant in the value type.
# NOTE: Covariance in the key type is theoretically problematic because
# there are methods with KT parameters, but in this case it's okay.
# See https://github.com/python/typeshed/issues/510.
if hasattr(collections_abc, 'Collection'):
class Mapping(Collection[KT], Generic[KT, VT_co],
class Mapping(Collection[KT_co], Generic[KT_co, VT_co],
extra=collections_abc.Mapping):
pass
else:
class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
class Mapping(Sized, Iterable[KT_co], Container[KT_co], Generic[KT_co, VT_co],
extra=collections_abc.Mapping):
pass

Expand Down