Skip to content

Commit 90a99ff

Browse files
committed
Sync variance in typing fixtures
1 parent b8ee1f5 commit 90a99ff

File tree

7 files changed

+73
-62
lines changed

7 files changed

+73
-62
lines changed

test-data/unit/fixtures/typing-async.pyi

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ Self = 0
2828

2929
T = TypeVar('T')
3030
T_co = TypeVar('T_co', covariant=True)
31+
R_co = TypeVar('R_co', covariant=True)
3132
T_contra = TypeVar('T_contra', contravariant=True)
3233
U = TypeVar('U')
3334
V = TypeVar('V')
3435
S = TypeVar('S')
36+
S_contra = TypeVar('S_contra', contravariant=True)
3537

3638
# Note: definitions below are different from typeshed, variances are declared
3739
# to silence the protocol variance checks. Maybe it is better to use type: ignore?
@@ -49,9 +51,9 @@ class Iterator(Iterable[T_co], Protocol):
4951
@abstractmethod
5052
def __next__(self) -> T_co: pass
5153

52-
class Generator(Iterator[T], Generic[T, U, V]):
54+
class Generator(Iterator[T_co], Generic[T_co, S_contra, R_co]):
5355
@abstractmethod
54-
def send(self, value: U) -> T: pass
56+
def send(self, value: S_contra) -> T_co: pass
5557

5658
@abstractmethod
5759
def throw(self, typ: Any, val: Any=None, tb: Any=None) -> None: pass
@@ -60,49 +62,54 @@ class Generator(Iterator[T], Generic[T, U, V]):
6062
def close(self) -> None: pass
6163

6264
@abstractmethod
63-
def __iter__(self) -> 'Generator[T, U, V]': pass
65+
def __iter__(self) -> 'Generator[T_co, S_contra, R_co]': pass
6466

65-
class AsyncGenerator(AsyncIterator[T], Generic[T, U]):
67+
class AsyncGenerator(AsyncIterator[T_co], Generic[T_co, S_contra]):
6668
@abstractmethod
67-
def __anext__(self) -> Awaitable[T]: pass
69+
def __anext__(self) -> Awaitable[T_co]: pass
6870

6971
@abstractmethod
70-
def asend(self, value: U) -> Awaitable[T]: pass
72+
def asend(self, value: S_contra) -> Awaitable[T_co]: pass
7173

7274
@abstractmethod
73-
def athrow(self, typ: Any, val: Any=None, tb: Any=None) -> Awaitable[T]: pass
75+
def athrow(self, typ: Any, val: Any=None, tb: Any=None) -> Awaitable[T_co]: pass
7476

7577
@abstractmethod
76-
def aclose(self) -> Awaitable[T]: pass
78+
def aclose(self) -> Awaitable[T_co]: pass
7779

7880
@abstractmethod
79-
def __aiter__(self) -> 'AsyncGenerator[T, U]': pass
81+
def __aiter__(self) -> 'AsyncGenerator[T_co, S_contra]': pass
8082

81-
class Awaitable(Protocol[T]):
83+
class Awaitable(Protocol[T_co]):
8284
@abstractmethod
83-
def __await__(self) -> Generator[Any, Any, T]: pass
85+
def __await__(self) -> Generator[Any, Any, T_co]: pass
8486

85-
class AwaitableGenerator(Generator[T, U, V], Awaitable[V], Generic[T, U, V, S], metaclass=ABCMeta):
87+
class AwaitableGenerator(
88+
Awaitable[R_co],
89+
Generator[T_co, S_contra, R_co],
90+
Generic[T_co, S_contra, R_co, S],
91+
metaclass=ABCMeta
92+
):
8693
pass
8794

88-
class Coroutine(Awaitable[V], Generic[T, U, V]):
95+
class Coroutine(Awaitable[R_co], Generic[T_co, S_contra, R_co]):
8996
@abstractmethod
90-
def send(self, value: U) -> T: pass
97+
def send(self, value: S_contra) -> T_co: pass
9198

9299
@abstractmethod
93100
def throw(self, typ: Any, val: Any=None, tb: Any=None) -> None: pass
94101

95102
@abstractmethod
96103
def close(self) -> None: pass
97104

98-
class AsyncIterable(Protocol[T]):
105+
class AsyncIterable(Protocol[T_co]):
99106
@abstractmethod
100-
def __aiter__(self) -> 'AsyncIterator[T]': pass
107+
def __aiter__(self) -> 'AsyncIterator[T_co]': pass
101108

102-
class AsyncIterator(AsyncIterable[T], Protocol):
103-
def __aiter__(self) -> 'AsyncIterator[T]': return self
109+
class AsyncIterator(AsyncIterable[T_co], Protocol):
110+
def __aiter__(self) -> 'AsyncIterator[T_co]': return self
104111
@abstractmethod
105-
def __anext__(self) -> Awaitable[T]: pass
112+
def __anext__(self) -> Awaitable[T_co]: pass
106113

107114
class Sequence(Iterable[T_co], Container[T_co]):
108115
@abstractmethod
@@ -116,13 +123,13 @@ class Mapping(Iterable[T], Generic[T, T_co], metaclass=ABCMeta):
116123
@overload
117124
def get(self, k: T, default: Union[T_co, V]) -> Union[T_co, V]: pass
118125

119-
class ContextManager(Generic[T]):
120-
def __enter__(self) -> T: pass
126+
class ContextManager(Generic[T_co]):
127+
def __enter__(self) -> T_co: pass
121128
# Use Any because not all the precise types are in the fixtures.
122129
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> Any: pass
123130

124-
class AsyncContextManager(Generic[T]):
125-
def __aenter__(self) -> Awaitable[T]: pass
131+
class AsyncContextManager(Generic[T_co]):
132+
def __aenter__(self) -> Awaitable[T_co]: pass
126133
# Use Any because not all the precise types are in the fixtures.
127134
def __aexit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> Awaitable[Any]: pass
128135

test-data/unit/fixtures/typing-full.pyi

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ Literal: _SpecialForm
4444

4545
T = TypeVar('T')
4646
T_co = TypeVar('T_co', covariant=True)
47-
T_contra = TypeVar('T_contra', contravariant=True)
47+
R_co = TypeVar('R_co', covariant=True)
48+
S_contra = TypeVar('S_contra', contravariant=True)
4849
U = TypeVar('U')
4950
V = TypeVar('V')
5051
S = TypeVar('S')
@@ -82,9 +83,9 @@ class Iterator(Iterable[T_co], Protocol):
8283
@abstractmethod
8384
def __next__(self) -> T_co: pass
8485

85-
class Generator(Iterator[T], Generic[T, U, V]):
86+
class Generator(Iterator[T_co], Generic[T_co, S_contra, R_co]):
8687
@abstractmethod
87-
def send(self, value: U) -> T: pass
88+
def send(self, value: S_contra) -> T_co: pass
8889

8990
@abstractmethod
9091
def throw(self, typ: Any, val: Any=None, tb: Any=None) -> None: pass
@@ -93,35 +94,40 @@ class Generator(Iterator[T], Generic[T, U, V]):
9394
def close(self) -> None: pass
9495

9596
@abstractmethod
96-
def __iter__(self) -> 'Generator[T, U, V]': pass
97+
def __iter__(self) -> 'Generator[T_co, S_contra, R_co]': pass
9798

98-
class AsyncGenerator(AsyncIterator[T], Generic[T, U]):
99+
class AsyncGenerator(AsyncIterator[T_co], Generic[T_co, S_contra]):
99100
@abstractmethod
100-
def __anext__(self) -> Awaitable[T]: pass
101+
def __anext__(self) -> Awaitable[T_co]: pass
101102

102103
@abstractmethod
103-
def asend(self, value: U) -> Awaitable[T]: pass
104+
def asend(self, value: S_contra) -> Awaitable[T_co]: pass
104105

105106
@abstractmethod
106-
def athrow(self, typ: Any, val: Any=None, tb: Any=None) -> Awaitable[T]: pass
107+
def athrow(self, typ: Any, val: Any=None, tb: Any=None) -> Awaitable[T_co]: pass
107108

108109
@abstractmethod
109-
def aclose(self) -> Awaitable[T]: pass
110+
def aclose(self) -> Awaitable[T_co]: pass
110111

111112
@abstractmethod
112-
def __aiter__(self) -> 'AsyncGenerator[T, U]': pass
113+
def __aiter__(self) -> 'AsyncGenerator[T_co, S_contra]': pass
113114

114115
@runtime_checkable
115-
class Awaitable(Protocol[T]):
116+
class Awaitable(Protocol[T_co]):
116117
@abstractmethod
117-
def __await__(self) -> Generator[Any, Any, T]: pass
118+
def __await__(self) -> Generator[Any, Any, T_co]: pass
118119

119-
class AwaitableGenerator(Generator[T, U, V], Awaitable[V], Generic[T, U, V, S], metaclass=ABCMeta):
120+
class AwaitableGenerator(
121+
Awaitable[R_co],
122+
Generator[T_co, S_contra, R_co],
123+
Generic[T_co, S_contra, R_co, S],
124+
metaclass=ABCMeta
125+
):
120126
pass
121127

122-
class Coroutine(Awaitable[V], Generic[T, U, V]):
128+
class Coroutine(Awaitable[R_co], Generic[T_co, S_contra, R_co]):
123129
@abstractmethod
124-
def send(self, value: U) -> T: pass
130+
def send(self, value: S_contra) -> T_co: pass
125131

126132
@abstractmethod
127133
def throw(self, typ: Any, val: Any=None, tb: Any=None) -> None: pass
@@ -130,15 +136,15 @@ class Coroutine(Awaitable[V], Generic[T, U, V]):
130136
def close(self) -> None: pass
131137

132138
@runtime_checkable
133-
class AsyncIterable(Protocol[T]):
139+
class AsyncIterable(Protocol[T_co]):
134140
@abstractmethod
135-
def __aiter__(self) -> 'AsyncIterator[T]': pass
141+
def __aiter__(self) -> 'AsyncIterator[T_co]': pass
136142

137143
@runtime_checkable
138-
class AsyncIterator(AsyncIterable[T], Protocol):
139-
def __aiter__(self) -> 'AsyncIterator[T]': return self
144+
class AsyncIterator(AsyncIterable[T_co], Protocol):
145+
def __aiter__(self) -> 'AsyncIterator[T_co]': return self
140146
@abstractmethod
141-
def __anext__(self) -> Awaitable[T]: pass
147+
def __anext__(self) -> Awaitable[T_co]: pass
142148

143149
class Sequence(Iterable[T_co], Container[T_co]):
144150
@abstractmethod

test-data/unit/fixtures/typing-medium.pyi

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ Self = 0
3232

3333
T = TypeVar('T')
3434
T_co = TypeVar('T_co', covariant=True)
35-
T_contra = TypeVar('T_contra', contravariant=True)
36-
U = TypeVar('U')
37-
V = TypeVar('V')
38-
S = TypeVar('S')
35+
R_co = TypeVar('R_co', covariant=True)
36+
S_contra = TypeVar('S_contra', contravariant=True)
3937

4038
# Note: definitions below are different from typeshed, variances are declared
4139
# to silence the protocol variance checks. Maybe it is better to use type: ignore?
@@ -49,8 +47,8 @@ class Iterable(Protocol[T_co]):
4947
class Iterator(Iterable[T_co], Protocol):
5048
def __next__(self) -> T_co: pass
5149

52-
class Generator(Iterator[T], Generic[T, U, V]):
53-
def __iter__(self) -> 'Generator[T, U, V]': pass
50+
class Generator(Iterator[T_co], Generic[T_co, S_contra, R_co]):
51+
def __iter__(self) -> 'Generator[T_co, S_contra, R_co]': pass
5452

5553
class Sequence(Iterable[T_co]):
5654
def __getitem__(self, n: Any) -> T_co: pass
@@ -65,8 +63,8 @@ class SupportsInt(Protocol):
6563
class SupportsFloat(Protocol):
6664
def __float__(self) -> float: pass
6765

68-
class ContextManager(Generic[T]):
69-
def __enter__(self) -> T: pass
66+
class ContextManager(Generic[T_co]):
67+
def __enter__(self) -> T_co: pass
7068
# Use Any because not all the precise types are in the fixtures.
7169
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> Any: pass
7270

test-data/unit/fixtures/typing-namedtuple.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class Iterable(Generic[T_co]): pass
1818
class Iterator(Iterable[T_co]): pass
1919
class Sequence(Iterable[T_co]): pass
2020
class Mapping(Iterable[KT], Generic[KT, T_co]):
21-
def keys(self) -> Iterable[T]: pass # Approximate return type
22-
def __getitem__(self, key: T) -> T_co: pass
21+
def keys(self) -> Iterable[KT]: pass # Approximate return type
22+
def __getitem__(self, key: KT) -> T_co: pass
2323

2424
class NamedTuple(tuple[Any, ...]):
2525
_fields: ClassVar[tuple[str, ...]]

test-data/unit/fixtures/typing-override.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class Iterable(Generic[T_co]): pass
1818
class Iterator(Iterable[T_co]): pass
1919
class Sequence(Iterable[T_co]): pass
2020
class Mapping(Iterable[KT], Generic[KT, T_co]):
21-
def keys(self) -> Iterable[T]: pass # Approximate return type
22-
def __getitem__(self, key: T) -> T_co: pass
21+
def keys(self) -> Iterable[KT]: pass # Approximate return type
22+
def __getitem__(self, key: KT) -> T_co: pass
2323

2424
def override(__arg: T) -> T: ...
2525

test-data/unit/fixtures/typing-typeddict.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Mapping(Iterable[T], Generic[T, T_co], metaclass=ABCMeta):
6161
def __len__(self) -> int: ...
6262
def __contains__(self, arg: object) -> int: pass
6363

64-
class MutableMapping(Mapping[T, T_co], Generic[T, T_co], metaclass=ABCMeta):
64+
class MutableMapping(Mapping[T, V], Generic[T, V], metaclass=ABCMeta):
6565
# Other methods are not used in tests.
6666
def clear(self) -> None: ...
6767

test-data/unit/lib-stub/typing.pyi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ TYPE_CHECKING = 0
3535

3636
T = TypeVar('T')
3737
T_co = TypeVar('T_co', covariant=True)
38-
U = TypeVar('U')
39-
V = TypeVar('V')
38+
S_contra = TypeVar('S_contra', contravariant=True)
39+
R_co = TypeVar('R_co', covariant=True)
4040

4141
class Iterable(Protocol[T_co]):
4242
def __iter__(self) -> Iterator[T_co]: pass
4343

4444
class Iterator(Iterable[T_co], Protocol):
4545
def __next__(self) -> T_co: pass
4646

47-
class Generator(Iterator[T], Generic[T, U, V]):
48-
def __iter__(self) -> Generator[T, U, V]: pass
47+
class Generator(Iterator[T_co], Generic[T_co, S_contra, R_co]):
48+
def __iter__(self) -> Generator[T_co, S_contra, R_co]: pass
4949

5050
class Sequence(Iterable[T_co]):
5151
def __getitem__(self, n: Any) -> T_co: pass
@@ -56,10 +56,10 @@ class Mapping(Iterable[T], Generic[T, T_co]):
5656
def keys(self) -> Iterable[T]: pass # Approximate return type
5757
def __getitem__(self, key: T) -> T_co: pass
5858

59-
class Awaitable(Protocol[T]):
59+
class Awaitable(Protocol[T_co]):
6060
def __await__(self) -> Generator[Any, Any, T]: pass
6161

62-
class Coroutine(Awaitable[V], Generic[T, U, V]): pass
62+
class Coroutine(Awaitable[R_co], Generic[T_co, S_contra, R_co]): pass
6363

6464
def final(meth: T) -> T: pass
6565

0 commit comments

Comments
 (0)