diff --git a/mypy/checker.py b/mypy/checker.py index 7d0b5dbde09d..9a63edeef5fc 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -5393,6 +5393,8 @@ def check_except_handler_test(self, n: Expression, is_star: bool) -> Type: if isinstance(ttype, AnyType): all_types.append(ttype) continue + if isinstance(ttype, UninhabitedType): + continue if isinstance(ttype, FunctionLike): item = ttype.items[0] diff --git a/test-data/unit/check-statements.test b/test-data/unit/check-statements.test index 0b768908a448..242a60be50ff 100644 --- a/test-data/unit/check-statements.test +++ b/test-data/unit/check-statements.test @@ -802,7 +802,7 @@ def error_in_variadic(exc: Tuple[int, ...]) -> None: [builtins fixtures/tuple.pyi] [case testExceptWithMultipleTypes5] -from typing import Tuple, Type, Union +from typing import Tuple, Type, Union, Never class E1(BaseException): pass class E2(BaseException): pass @@ -849,7 +849,38 @@ def error_in_tuple_union(exc: Tuple[Union[Type[E1], Type[E2]], Union[Type[E3], i pass except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes) pass +[builtins fixtures/tuple.pyi] + +[case testExceptWithMultipleTypes6_no_parallel] +# For no_parallel, see: +# https://github.com/mypyc/ast_serialize/issues/50 +from typing import Never + +def random() -> bool: ... + +def error_in_empty_tuple() -> None: + try: + pass + except () as e: # E: Need type annotation for "e" + pass +def error_in_empty_tuple_annotated(exc: tuple[()]) -> None: + try: + pass + except exc as e: # E: Need type annotation for "e" + pass + +def error_in_conditional_empty_tuple() -> None: + try: + pass + except (BaseException if random() else ()) as e: + pass + +def error_in_never(exc: Never) -> None: + try: + pass + except exc as e: # E: Need type annotation for "e" + pass [builtins fixtures/tuple.pyi] [case testExceptWithAnyTypes]