Skip to content

Commit b796178

Browse files
committed
Add covariant mapping type
1 parent 1baf0a5 commit b796178

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/typing_extensions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
'TypedDict',
4343

4444
# Structural checks, a.k.a. protocols.
45+
'MappingAuxiliary',
4546
'SupportsIndex',
4647

4748
# One-off things.
@@ -145,7 +146,9 @@ def _collect_type_vars(types, typevar_types=None):
145146
# (These are not for export.)
146147
T = typing.TypeVar('T') # Any type.
147148
KT = typing.TypeVar('KT') # Key type.
149+
KT_co = typing.TypeVar('KT', covariant=True) # Covariant key type.
148150
VT = typing.TypeVar('VT') # Value type.
151+
VT_co = typing.TypeVar('VT', covariant=True) # Value type.
149152
T_co = typing.TypeVar('T_co', covariant=True) # Any type covariant containers.
150153
T_contra = typing.TypeVar('T_contra', contravariant=True) # Ditto contravariant.
151154

@@ -603,6 +606,19 @@ def runtime_checkable(cls):
603606
runtime = runtime_checkable
604607

605608

609+
# Not yet ported to typing, but who knows
610+
if hasattr(typing, 'MappingAuxiliary'):
611+
MappingAuxiliary = typing.MappingAuxiliary
612+
else:
613+
class MappingAuxiliary(typing.Collection[KT_co], Protocol[KT_co, VT_co]):
614+
"""Represents the covariant sub-protocol of the Mapping type.
615+
"""
616+
def items(self) -> ItemsView[KT_co, VT_co]: ...
617+
def keys(self) -> KeysView[KT_co]: ...
618+
def values(self) -> ValuesView[VT_co]: ...
619+
def __contains__(self, __o: object) -> bool: ...
620+
621+
606622
# 3.8+
607623
if hasattr(typing, 'SupportsIndex'):
608624
SupportsIndex = typing.SupportsIndex

0 commit comments

Comments
 (0)