[Python-checkins] cpython: Mapping key type is invariant.

guido.van.rossum python-checkins at python.org
Sun Jun 7 22:36:26 CEST 2015


https://hg.python.org/cpython/rev/f4b4e23475d0
changeset:   96532:f4b4e23475d0
user:        Guido van Rossum <guido at python.org>
date:        Sun Jun 07 13:36:19 2015 -0700
summary:
  Mapping key type is invariant.

files:
  Lib/test/test_typing.py |   6 +++---
  Lib/typing.py           |  10 +++++-----
  2 files changed, 8 insertions(+), 8 deletions(-)


diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -707,11 +707,11 @@
                               typing.Sequence[Manager])
 
     def test_covariance_mapping(self):
-        # Ditto for Mapping (a generic class with two parameters).
+        # Ditto for Mapping (covariant in the value, invariant in the key).
         assert issubclass(typing.Mapping[Employee, Manager],
                           typing.Mapping[Employee, Employee])
-        assert issubclass(typing.Mapping[Manager, Employee],
-                          typing.Mapping[Employee, Employee])
+        assert not issubclass(typing.Mapping[Manager, Employee],
+                              typing.Mapping[Employee, Employee])
         assert not issubclass(typing.Mapping[Employee, Manager],
                               typing.Mapping[Manager, Manager])
         assert not issubclass(typing.Mapping[Manager, Employee],
diff --git a/Lib/typing.py b/Lib/typing.py
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -439,7 +439,6 @@
 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_co', 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.
 
@@ -1308,7 +1307,8 @@
     pass
 
 
-class Mapping(Sized, Iterable[KT_co], Container[KT_co], Generic[KT_co, VT_co],
+# NOTE: Only the value type is covariant.
+class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
               extra=collections_abc.Mapping):
     pass
 
@@ -1378,13 +1378,13 @@
     pass
 
 
-class KeysView(MappingView[KT_co], AbstractSet[KT_co],
+class KeysView(MappingView[KT], AbstractSet[KT],
                extra=collections_abc.KeysView):
     pass
 
 
-# TODO: Enable Set[Tuple[KT_co, VT_co]] instead of Generic[KT_co, VT_co].
-class ItemsView(MappingView, Generic[KT_co, VT_co],
+# TODO: Enable Set[Tuple[KT, VT_co]] instead of Generic[KT, VT_co].
+class ItemsView(MappingView, Generic[KT, VT_co],
                 extra=collections_abc.ItemsView):
     pass
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list