Skip to content

Commit d1ed60b

Browse files
committed
Updated 3.2/typing.pyi with covariant type vars for immutable sequences.
1 parent 7376f90 commit d1ed60b

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

stubs/3.2/typing.pyi

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ AnyStr = TypeVar('AnyStr', str, bytes)
3535

3636
# Abstract base classes.
3737

38-
_T = TypeVar('_T', covariant=True)
39-
_KT = TypeVar('_KT', covariant=True)
40-
_VT = TypeVar('_VT', covariant=True)
38+
# Some unconstrained type variables. These are used by the container types.
39+
_T = TypeVar('T') # Any type.
40+
_KT = TypeVar('KT') # Key type.
41+
_VT = TypeVar('VT') # Value type.
42+
_T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
43+
_V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
44+
_KT_co = TypeVar('KT_co', covariant=True) # Key type covariant containers.
45+
_VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
46+
_T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
4147

4248
# TODO Container etc.
4349

@@ -72,32 +78,32 @@ class Hashable(metaclass=ABCMeta):
7278
@abstractmethod
7379
def __hash__(self) -> int: pass
7480

75-
class Iterable(Generic[_T]):
81+
class Iterable(Generic[_T_co]):
7682
@abstractmethod
77-
def __iter__(self) -> Iterator[_T]: pass
83+
def __iter__(self) -> Iterator[_T_co]: pass
7884

79-
class Iterator(Iterable[_T], Generic[_T]):
85+
class Iterator(Iterable[_T_co], Generic[_T_co]):
8086
@abstractmethod
81-
def __next__(self) -> _T: pass
82-
def __iter__(self) -> Iterator[_T]: pass
87+
def __next__(self) -> _T_co: pass
88+
def __iter__(self) -> Iterator[_T_co]: pass
8389

84-
class Container(Generic[_T]):
90+
class Container(Generic[_T_co]):
8591
@abstractmethod
8692
def __contains__(self, x: object) -> bool: pass
8793

88-
class Sequence(Iterable[_T], Container[_T], Sized, Reversible[_T], Generic[_T]):
94+
class Sequence(Iterable[_T_co], Container[_T_co], Sized, Reversible[_T_co], Generic[_T_co]):
8995
@overload
9096
@abstractmethod
91-
def __getitem__(self, i: int) -> _T: pass
97+
def __getitem__(self, i: int) -> _T_co: pass
9298
@overload
9399
@abstractmethod
94-
def __getitem__(self, s: slice) -> Sequence[_T]: pass
100+
def __getitem__(self, s: slice) -> Sequence[_T_co]: pass
95101
# Mixin methods
96102
def index(self, x: Any) -> int: pass
97103
def count(self, x: Any) -> int: pass
98104
def __contains__(self, x: object) -> bool: pass
99-
def __iter__(self) -> Iterator[_T]: pass
100-
def __reversed__(self) -> Iterator[_T]: pass
105+
def __iter__(self) -> Iterator[_T_co]: pass
106+
def __reversed__(self) -> Iterator[_T_co]: pass
101107

102108
class MutableSequence(Sequence[_T], Generic[_T]):
103109
@abstractmethod
@@ -118,20 +124,20 @@ class MutableSequence(Sequence[_T], Generic[_T]):
118124
def remove(self, object: _T) -> None: pass
119125
def __iadd__(self, x: Iterable[_T]) -> MutableSequence[_T]: pass
120126

121-
class AbstractSet(Iterable[_T], Container[_T], Sized, Generic[_T]):
127+
class AbstractSet(Iterable[_KT_co], Container[_KT_co], Sized, Generic[_KT_co]):
122128
@abstractmethod
123129
def __contains__(self, x: object) -> bool: pass
124130
# Mixin methods
125131
def __le__(self, s: AbstractSet[Any]) -> bool: pass
126132
def __lt__(self, s: AbstractSet[Any]) -> bool: pass
127133
def __gt__(self, s: AbstractSet[Any]) -> bool: pass
128134
def __ge__(self, s: AbstractSet[Any]) -> bool: pass
129-
def __and__(self, s: AbstractSet[Any]) -> AbstractSet[_T]: pass
135+
def __and__(self, s: AbstractSet[Any]) -> AbstractSet[_KT_co]: pass
130136
# In order to support covariance, _T should not be used within an argument
131137
# type. We need union types to properly model this.
132-
def __or__(self, s: AbstractSet[_T]) -> AbstractSet[_T]: pass
133-
def __sub__(self, s: AbstractSet[Any]) -> AbstractSet[_T]: pass
134-
def __xor__(self, s: AbstractSet[_T]) -> AbstractSet[_T]: pass
138+
def __or__(self, s: AbstractSet[_KT_co]) -> AbstractSet[_KT_co]: pass
139+
def __sub__(self, s: AbstractSet[Any]) -> AbstractSet[_KT_co]: pass
140+
def __xor__(self, s: AbstractSet[_KT_co]) -> AbstractSet[_KT_co]: pass
135141
# TODO: Argument can be a more general ABC?
136142
def isdisjoint(self, s: AbstractSet[Any]) -> bool: pass
137143

@@ -152,26 +158,26 @@ class MutableSet(AbstractSet[_T], Generic[_T]):
152158
class MappingView(Sized):
153159
def __len__(self) -> int: pass
154160

155-
class ItemsView(AbstractSet[Tuple[_KT, _VT]], MappingView, Generic[_KT, _VT]):
161+
class ItemsView(AbstractSet[Tuple[_KT_co, _VT_co]], MappingView, Generic[_KT_co, _VT_co]):
156162
def __contains__(self, o: object) -> bool: pass
157-
def __iter__(self) -> Iterator[Tuple[_KT, _VT]]: pass
163+
def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: pass
158164

159-
class KeysView(AbstractSet[_T], MappingView, Generic[_T]):
165+
class KeysView(AbstractSet[_KT_co], MappingView, Generic[_KT_co]):
160166
def __contains__(self, o: object) -> bool: pass
161-
def __iter__(self) -> Iterator[_T]: pass
167+
def __iter__(self) -> Iterator[_KT_co]: pass
162168

163-
class ValuesView(MappingView, Iterable[_T], Generic[_T]):
169+
class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
164170
def __contains__(self, o: object) -> bool: pass
165-
def __iter__(self) -> Iterator[_T]: pass
171+
def __iter__(self) -> Iterator[_VT_co]: pass
166172

167-
class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT]):
173+
class Mapping(Iterable[_KT_co], Container[_KT_co], Sized, Generic[_KT_co, _VT_co]):
168174
@abstractmethod
169-
def __getitem__(self, k: _KT) -> _VT: pass
175+
def __getitem__(self, k: _KT_co) -> _VT_co: pass
170176
# Mixin methods
171-
def get(self, k: _KT, default: _VT = ...) -> _VT: pass
172-
def items(self) -> AbstractSet[Tuple[_KT, _VT]]: pass
173-
def keys(self) -> AbstractSet[_KT]: pass
174-
def values(self) -> ValuesView[_VT]: pass
177+
def get(self, k: _KT, default: _VT = ...) -> _VT_co: pass
178+
def items(self) -> AbstractSet[Tuple[_KT_co, _VT_co]]: pass
179+
def keys(self) -> AbstractSet[_KT_co]: pass
180+
def values(self) -> ValuesView[_VT_co]: pass
175181
def __contains__(self, o: object) -> bool: pass
176182

177183
class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):

0 commit comments

Comments
 (0)