[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[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[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] MyClass() -> MyClass[None, None] \ # N: def [T1, T2] MyClass(type[T1], /) -> MyClass[T1, None] \ # N: def [T1, T2] MyClass(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-old 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] [case testGenericTypeAliasArgOfTypeAliasIsUnpackedTuple] from typing_extensions import TypeAlias from typing import TypeVarTuple, Unpack Ts = TypeVarTuple('Ts') TA: TypeAlias = tuple[Unpack[Ts]] v1: TA[*list[int]] # E: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple) v2: TA[Unpack[list[int]]] # E: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple) [builtins fixtures/tuple.pyi]