Skip to content

Commit 89ffeb2

Browse files
pdmccormickddfisher
authored andcommitted
Allow Any types for tuple except handler tests (#1639)
* Allow `Any` types for tuple except handler tests Fixes #1635. * If even just one test has `Any` type then the whole expression will be `Any` * Revert "If even just one test has `Any` type then the whole expression will be `Any`" This reverts commit 00faff4. * Ensure all errors are reported, regardless of where the `Any` appears
1 parent 99e9670 commit 89ffeb2

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎mypy/checker.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,13 +1827,15 @@ def visit_try_stmt(self, s: TryStmt) -> Type:
18271827
def visit_except_handler_test(self, n: Node) -> Type:
18281828
"""Type check an exception handler test clause."""
18291829
type = self.accept(n)
1830-
if isinstance(type, AnyType):
1831-
return type
18321830

18331831
all_types = [] # type: List[Type]
18341832
test_types = type.items if isinstance(type, TupleType) else [type]
18351833

18361834
for ttype in test_types:
1835+
if isinstance(ttype, AnyType):
1836+
all_types.append(ttype)
1837+
continue
1838+
18371839
if not isinstance(ttype, FunctionLike):
18381840
self.fail(messages.INVALID_EXCEPTION_TYPE, n)
18391841
return AnyType()

‎mypy/test/data/check-statements.test‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,41 @@ except (E1_1, E1_2) as e2:
598598
c = e2 # type: E1_2 # E: Incompatible types in assignment (expression has type "Union[E1_1, E1_2]", variable has type "E1_2")
599599
[builtins fixtures/exception.py]
600600

601+
[case testExceptWithAnyTypes]
602+
from typing import Any
603+
604+
E1 = None # type: Any
605+
class E2(BaseException): pass
606+
class NotBaseDerived: pass
607+
608+
try:
609+
pass
610+
except BaseException as e1:
611+
reveal_type(e1) # E: Revealed type is 'builtins.BaseException'
612+
except (E1, BaseException) as e2:
613+
reveal_type(e2) # E: Revealed type is 'Any'
614+
except (E1, E2) as e3:
615+
reveal_type(e3) # E: Revealed type is 'Any'
616+
except (E1, E2, BaseException) as e4:
617+
reveal_type(e4) # E: Revealed type is 'Any'
618+
619+
try: pass
620+
except E1 as e1:
621+
reveal_type(e1) # E: Revealed type is 'Any'
622+
except E2 as e2:
623+
reveal_type(e2) # E: Revealed type is '__main__.E2'
624+
except NotBaseDerived as e3: # E: Exception type must be derived from BaseException
625+
pass
626+
except (NotBaseDerived, E1) as e4: # E: Exception type must be derived from BaseException
627+
pass
628+
except (NotBaseDerived, E2) as e5: # E: Exception type must be derived from BaseException
629+
pass
630+
except (NotBaseDerived, E1, E2) as e6: # E: Exception type must be derived from BaseException
631+
pass
632+
except (E1, E2, NotBaseDerived) as e6: # E: Exception type must be derived from BaseException
633+
pass
634+
[builtins fixtures/exception.py]
635+
601636
[case testReuseTryExceptionVariable]
602637
import typing
603638
class E1(BaseException): pass

0 commit comments

Comments
 (0)