File: check-python311.test

package info (click to toggle)
mypy 1.19.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,412 kB
  • sloc: python: 114,754; ansic: 13,343; cpp: 11,380; makefile: 257; sh: 28
file content (323 lines) | stat: -rw-r--r-- 11,212 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
[case testTryStarSimple]
try:
    pass
except* Exception as e:
    reveal_type(e)  # N: Revealed type is "builtins.ExceptionGroup[builtins.Exception]"
[builtins fixtures/exception.pyi]

[case testTryStarMultiple]
try:
    pass
except* Exception as e:
    reveal_type(e)  # N: Revealed type is "builtins.ExceptionGroup[builtins.Exception]"
except* RuntimeError as e:
    reveal_type(e)  # N: Revealed type is "builtins.ExceptionGroup[builtins.RuntimeError]"
[builtins fixtures/exception.pyi]

[case testTryStarBase]
try:
    pass
except* BaseException as e:
    reveal_type(e)  # N: Revealed type is "builtins.BaseExceptionGroup[builtins.BaseException]"
[builtins fixtures/exception.pyi]

[case testTryStarTuple]
class Custom(Exception): ...

try:
    pass
except* (RuntimeError, Custom) as e:
    reveal_type(e)  # N: Revealed type is "builtins.ExceptionGroup[Union[builtins.RuntimeError, __main__.Custom]]"
[builtins fixtures/exception.pyi]

[case testTryStarInvalidType]
class Bad: ...
try:
    pass
except* (RuntimeError, Bad) as e:  # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
    reveal_type(e)  # N: Revealed type is "builtins.ExceptionGroup[Any]"
[builtins fixtures/exception.pyi]

[case testTryStarGroupInvalid]
try:
    pass
except* ExceptionGroup as e:  # E: Exception type in except* cannot derive from BaseExceptionGroup
    reveal_type(e)  # N: Revealed type is "builtins.ExceptionGroup[Any]"
[builtins fixtures/exception.pyi]

[case testTryStarGroupInvalidTuple]
try:
    pass
except* (RuntimeError, ExceptionGroup) as e:  # E: Exception type in except* cannot derive from BaseExceptionGroup
    reveal_type(e)  # N: Revealed type is "builtins.ExceptionGroup[Union[builtins.RuntimeError, Any]]"
[builtins fixtures/exception.pyi]

[case testBasicTypeVarTupleGeneric]
from typing import Generic, TypeVarTuple, Unpack

Ts = TypeVarTuple("Ts")

class Variadic(Generic[Unpack[Ts]]):
    ...

variadic: Variadic[int, str]
reveal_type(variadic)  # N: Revealed type is "__main__.Variadic[builtins.int, builtins.str]"
[builtins fixtures/tuple.pyi]

[case testAsyncGeneratorWithinComprehension]
# flags: --python-version 3.11
from typing import Any, Generator, List

async def asynciter(iterable):
    for x in iterable:
        yield x

async def coro() -> Generator[List[Any], None, None]:
    return ([i async for i in asynciter([0,j])] for j in [3, 5])
reveal_type(coro)  # N: Revealed type is "def () -> typing.Coroutine[Any, Any, typing.Generator[builtins.list[Any], None, None]]"
[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]

[case testTypeVarTupleNewSyntaxAnnotations]
Ints = tuple[int, int, int]
x: tuple[str, *Ints]
reveal_type(x)  # N: Revealed type is "tuple[builtins.str, builtins.int, builtins.int, builtins.int]"
y: tuple[int, *tuple[int, ...]]
reveal_type(y)  # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]]]"
[builtins fixtures/tuple.pyi]

[case testTypeVarTupleNewSyntaxGenerics]
from typing import Generic, TypeVar, TypeVarTuple

T = TypeVar("T")
Ts = TypeVarTuple("Ts")
class C(Generic[T, *Ts]):
    attr: tuple[int, *Ts, str]

    def test(self) -> None:
        reveal_type(self.attr)  # N: Revealed type is "tuple[builtins.int, Unpack[Ts`2], builtins.str]"
        self.attr = ci  # E: Incompatible types in assignment (expression has type "C[*tuple[int, ...]]", variable has type "tuple[int, *Ts, str]")
    def meth(self, *args: *Ts) -> T: ...

ci: C[*tuple[int, ...]]
reveal_type(ci)  # N: Revealed type is "__main__.C[Unpack[builtins.tuple[builtins.int, ...]]]"
reveal_type(ci.meth)  # N: Revealed type is "def (*args: builtins.int) -> builtins.int"
c3: C[str, str, str]
reveal_type(c3)  # N: Revealed type is "__main__.C[builtins.str, builtins.str, builtins.str]"

A = C[int, *Ts]
B = tuple[str, *tuple[str, str], str]
z: A[*B]
reveal_type(z)  # N: Revealed type is "__main__.C[builtins.int, builtins.str, builtins.str, builtins.str, builtins.str]"
[builtins fixtures/tuple.pyi]

[case testTypeVarTupleNewSyntaxCallables]
from typing import Generic, overload, TypeVar

T1 = TypeVar("T1")
T2 = TypeVar("T2")
class MyClass(Generic[T1, T2]):
    @overload
    def __init__(self: MyClass[None, None]) -> None: ...

    @overload
    def __init__(self: MyClass[T1, None], *types: *tuple[type[T1]]) -> None: ...

    @overload
    def __init__(self: MyClass[T1, T2], *types: *tuple[type[T1], type[T2]]) -> None: ...

    def __init__(self: MyClass[T1, T2], *types: *tuple[type, ...]) -> None:
        pass

myclass = MyClass()
reveal_type(myclass)  # N: Revealed type is "__main__.MyClass[None, None]"
myclass1 = MyClass(float)
reveal_type(myclass1)  # N: Revealed type is "__main__.MyClass[builtins.float, None]"
myclass2 = MyClass(float, float)
reveal_type(myclass2)  # N: Revealed type is "__main__.MyClass[builtins.float, builtins.float]"
myclass3 = MyClass(float, float, float)  # E: No overload variant of "MyClass" matches argument types "type[float]", "type[float]", "type[float]" \
                                         # N: Possible overload variants: \
                                         # N:     def [T1, T2] __init__(self) -> MyClass[None, None] \
                                         # N:     def [T1, T2] __init__(self, type[T1], /) -> MyClass[T1, None] \
                                         # N:     def [T1, T2] __init__(type[T1], type[T2], /) -> MyClass[T1, T2]
reveal_type(myclass3)  # N: Revealed type is "Any"
[builtins fixtures/tuple.pyi]

[case testUnpackNewSyntaxInvalidCallableAlias]
from typing import Any, Callable, List, Tuple, TypeVar, Unpack

T = TypeVar("T")
Ts = TypeVarTuple("Ts")  # E: Name "TypeVarTuple" is not defined

def good(*x: int) -> int: ...
def bad(*x: int, y: int) -> int: ...

Alias1 = Callable[[*Ts], int]  # E: Variable "__main__.Ts" is not valid as a type \
                               # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
x1: Alias1[int]  # E: Bad number of arguments for type alias, expected 0, given 1
reveal_type(x1)  # N: Revealed type is "def (*Any) -> builtins.int"
x1 = good
x1 = bad  # E: Incompatible types in assignment (expression has type "def bad(*x: int, y: int) -> int", variable has type "def (*Any) -> int")

Alias2 = Callable[[*T], int]  # E: "T" cannot be unpacked (must be tuple or TypeVarTuple)
x2: Alias2[int]
reveal_type(x2)  # N: Revealed type is "def (*Any) -> builtins.int"

Unknown = Any
Alias3 = Callable[[*Unknown], int]
x3: Alias3[int]  # E: Bad number of arguments for type alias, expected 0, given 1
reveal_type(x3)  # N: Revealed type is "def (*Any) -> builtins.int"

IntList = List[int]
Alias4 = Callable[[*IntList], int]  # E: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple)
x4: Alias4[int]  # E: Bad number of arguments for type alias, expected 0, given 1
reveal_type(x4)  # N: Revealed type is "def (*Any) -> builtins.int"
[builtins fixtures/tuple.pyi]

[case testReturnInExceptStarBlock1]
# flags: --python-version 3.11
def foo() -> None:
    try:
        pass
    except* Exception:
        return   # E: "return" not allowed in except* block
    finally:
        return
[builtins fixtures/exception.pyi]

[case testReturnInExceptStarBlock2]
# flags: --python-version 3.11
def foo():
    while True:
        try:
            pass
        except* Exception:
            while True:
                return  # E: "return" not allowed in except* block
[builtins fixtures/exception.pyi]

[case testContinueInExceptBlockNestedInExceptStarBlock]
# flags: --python-version 3.11
while True:
    try:
        ...
    except* Exception:
        try:
            ...
        except Exception:
            continue  # E: "continue" not allowed in except* block
        continue  # E: "continue" not allowed in except* block
[builtins fixtures/exception.pyi]

[case testReturnInExceptBlockNestedInExceptStarBlock]
# flags: --python-version 3.11
def foo():
    try:
        ...
    except* Exception:
        try:
            ...
        except Exception:
            return  # E: "return" not allowed in except* block
        return  # E: "return" not allowed in except* block
[builtins fixtures/exception.pyi]

[case testBreakContinueReturnInExceptStarBlock1]
# flags: --python-version 3.11
from typing import Iterable
def foo(x: Iterable[int]) -> None:
    for _ in x:
        try:
            pass
        except* Exception:
            continue   # E: "continue" not allowed in except* block
        except* Exception:
            for _ in x:
                continue
            break  # E: "break" not allowed in except* block
        except* Exception:
            return  # E: "return" not allowed in except* block
[builtins fixtures/exception.pyi]

[case testBreakContinueReturnInExceptStarBlock2]
# flags: --python-version 3.11
def foo():
    while True:
        try:
            pass
        except* Exception:
            def inner():
                while True:
                    if 1 < 1:
                        continue
                    else:
                        break
                return
            if 1 < 2:
                break  # E: "break" not allowed in except* block
            if 1 < 2:
                continue  # E: "continue" not allowed in except* block
            return  # E: "return" not allowed in except* block
[builtins fixtures/exception.pyi]

[case testLambdaInExceptStarBlock]
# flags: --python-version 3.11
def foo():
    try:
        pass
    except* Exception:
        x = lambda: 0
        return lambda: 0  # E: "return" not allowed in except* block

def loop():
    while True:
        try:
            pass
        except* Exception:
            x = lambda: 0
            return lambda: 0  # E: "return" not allowed in except* block
[builtins fixtures/exception.pyi]

[case testRedefineLocalWithinExceptStarTryClauses]
# flags: --allow-redefinition
def fn_str(_: str) -> int: ...
def fn_int(_: int) -> None: ...
def fn_exc(_: Exception) -> str: ...

def in_block() -> None:
    try:
        a = ""
        a = fn_str(a)  # E: Incompatible types in assignment (expression has type "int", variable has type "str")
        fn_int(a)  # E: Argument 1 to "fn_int" has incompatible type "str"; expected "int"
    except* Exception:
        b = ""
        b = fn_str(b)
        fn_int(b)
    else:
        c = ""
        c = fn_str(c)
        fn_int(c)
    finally:
        d = ""
        d = fn_str(d)
        fn_int(d)
    reveal_type(a)  # N: Revealed type is "builtins.str"
    reveal_type(b)  # N: Revealed type is "builtins.int"
    reveal_type(c)  # N: Revealed type is "builtins.int"
    reveal_type(d)  # N: Revealed type is "builtins.int"

def across_blocks() -> None:
    try:
        a = ""
    except* Exception:
        a = fn_str(a)  # E: Incompatible types in assignment (expression has type "int", variable has type "str")
    else:
        a = fn_str(a)  # E: Incompatible types in assignment (expression has type "int", variable has type "str")
    reveal_type(a)  # N: Revealed type is "builtins.str"

def exc_name() -> None:
    try:
        pass
    except* RuntimeError as e:
        e = fn_exc(e)
[builtins fixtures/exception.pyi]