Skip to content

Commit e70166a

Browse files
authored
Fix regression for catching empty tuple in except (#21153)
Fixes #21152
1 parent bdb0dd4 commit e70166a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

mypy/checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5393,6 +5393,8 @@ def check_except_handler_test(self, n: Expression, is_star: bool) -> Type:
53935393
if isinstance(ttype, AnyType):
53945394
all_types.append(ttype)
53955395
continue
5396+
if isinstance(ttype, UninhabitedType):
5397+
continue
53965398

53975399
if isinstance(ttype, FunctionLike):
53985400
item = ttype.items[0]

test-data/unit/check-statements.test

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ def error_in_variadic(exc: Tuple[int, ...]) -> None:
802802
[builtins fixtures/tuple.pyi]
803803

804804
[case testExceptWithMultipleTypes5]
805-
from typing import Tuple, Type, Union
805+
from typing import Tuple, Type, Union, Never
806806

807807
class E1(BaseException): pass
808808
class E2(BaseException): pass
@@ -849,7 +849,38 @@ def error_in_tuple_union(exc: Tuple[Union[Type[E1], Type[E2]], Union[Type[E3], i
849849
pass
850850
except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
851851
pass
852+
[builtins fixtures/tuple.pyi]
853+
854+
[case testExceptWithMultipleTypes6_no_parallel]
855+
# For no_parallel, see:
856+
# https://github.com/mypyc/ast_serialize/issues/50
857+
from typing import Never
858+
859+
def random() -> bool: ...
860+
861+
def error_in_empty_tuple() -> None:
862+
try:
863+
pass
864+
except () as e: # E: Need type annotation for "e"
865+
pass
852866

867+
def error_in_empty_tuple_annotated(exc: tuple[()]) -> None:
868+
try:
869+
pass
870+
except exc as e: # E: Need type annotation for "e"
871+
pass
872+
873+
def error_in_conditional_empty_tuple() -> None:
874+
try:
875+
pass
876+
except (BaseException if random() else ()) as e:
877+
pass
878+
879+
def error_in_never(exc: Never) -> None:
880+
try:
881+
pass
882+
except exc as e: # E: Need type annotation for "e"
883+
pass
853884
[builtins fixtures/tuple.pyi]
854885

855886
[case testExceptWithAnyTypes]

0 commit comments

Comments
 (0)