@@ -35,9 +35,15 @@ AnyStr = TypeVar('AnyStr', str, bytes)
35
35
36
36
# Abstract base classes.
37
37
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.
41
47
42
48
# TODO Container etc.
43
49
@@ -72,32 +78,32 @@ class Hashable(metaclass=ABCMeta):
72
78
@abstractmethod
73
79
def __hash__ (self ) -> int : pass
74
80
75
- class Iterable (Generic [_T ]):
81
+ class Iterable (Generic [_T_co ]):
76
82
@abstractmethod
77
- def __iter__ (self ) -> Iterator [_T ]: pass
83
+ def __iter__ (self ) -> Iterator [_T_co ]: pass
78
84
79
- class Iterator (Iterable [_T ], Generic [_T ]):
85
+ class Iterator (Iterable [_T_co ], Generic [_T_co ]):
80
86
@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
83
89
84
- class Container (Generic [_T ]):
90
+ class Container (Generic [_T_co ]):
85
91
@abstractmethod
86
92
def __contains__ (self , x : object ) -> bool : pass
87
93
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 ]):
89
95
@overload
90
96
@abstractmethod
91
- def __getitem__ (self , i : int ) -> _T : pass
97
+ def __getitem__ (self , i : int ) -> _T_co : pass
92
98
@overload
93
99
@abstractmethod
94
- def __getitem__ (self , s : slice ) -> Sequence [_T ]: pass
100
+ def __getitem__ (self , s : slice ) -> Sequence [_T_co ]: pass
95
101
# Mixin methods
96
102
def index (self , x : Any ) -> int : pass
97
103
def count (self , x : Any ) -> int : pass
98
104
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
101
107
102
108
class MutableSequence (Sequence [_T ], Generic [_T ]):
103
109
@abstractmethod
@@ -118,20 +124,20 @@ class MutableSequence(Sequence[_T], Generic[_T]):
118
124
def remove (self , object : _T ) -> None : pass
119
125
def __iadd__ (self , x : Iterable [_T ]) -> MutableSequence [_T ]: pass
120
126
121
- class AbstractSet (Iterable [_T ], Container [_T ], Sized , Generic [_T ]):
127
+ class AbstractSet (Iterable [_KT_co ], Container [_KT_co ], Sized , Generic [_KT_co ]):
122
128
@abstractmethod
123
129
def __contains__ (self , x : object ) -> bool : pass
124
130
# Mixin methods
125
131
def __le__ (self , s : AbstractSet [Any ]) -> bool : pass
126
132
def __lt__ (self , s : AbstractSet [Any ]) -> bool : pass
127
133
def __gt__ (self , s : AbstractSet [Any ]) -> bool : pass
128
134
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
130
136
# In order to support covariance, _T should not be used within an argument
131
137
# 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
135
141
# TODO: Argument can be a more general ABC?
136
142
def isdisjoint (self , s : AbstractSet [Any ]) -> bool : pass
137
143
@@ -152,26 +158,26 @@ class MutableSet(AbstractSet[_T], Generic[_T]):
152
158
class MappingView (Sized ):
153
159
def __len__ (self ) -> int : pass
154
160
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 ]):
156
162
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
158
164
159
- class KeysView (AbstractSet [_T ], MappingView , Generic [_T ]):
165
+ class KeysView (AbstractSet [_KT_co ], MappingView , Generic [_KT_co ]):
160
166
def __contains__ (self , o : object ) -> bool : pass
161
- def __iter__ (self ) -> Iterator [_T ]: pass
167
+ def __iter__ (self ) -> Iterator [_KT_co ]: pass
162
168
163
- class ValuesView (MappingView , Iterable [_T ], Generic [_T ]):
169
+ class ValuesView (MappingView , Iterable [_VT_co ], Generic [_VT_co ]):
164
170
def __contains__ (self , o : object ) -> bool : pass
165
- def __iter__ (self ) -> Iterator [_T ]: pass
171
+ def __iter__ (self ) -> Iterator [_VT_co ]: pass
166
172
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 ]):
168
174
@abstractmethod
169
- def __getitem__ (self , k : _KT ) -> _VT : pass
175
+ def __getitem__ (self , k : _KT_co ) -> _VT_co : pass
170
176
# 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
175
181
def __contains__ (self , o : object ) -> bool : pass
176
182
177
183
class MutableMapping (Mapping [_KT , _VT ], Generic [_KT , _VT ]):
0 commit comments