Skip to content

Commit

Permalink
Allow Any types for tuple except handler tests (#1639)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pdmccormick authored and ddfisher committed Jun 3, 2016
1 parent 99e9670 commit 89ffeb2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
6 changes: 4 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1827,13 +1827,15 @@ def visit_try_stmt(self, s: TryStmt) -> Type:
def visit_except_handler_test(self, n: Node) -> Type:
"""Type check an exception handler test clause."""
type = self.accept(n)
if isinstance(type, AnyType):
return type

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

for ttype in test_types:
if isinstance(ttype, AnyType):
all_types.append(ttype)
continue

if not isinstance(ttype, FunctionLike):
self.fail(messages.INVALID_EXCEPTION_TYPE, n)
return AnyType()
Expand Down
35 changes: 35 additions & 0 deletions mypy/test/data/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,41 @@ except (E1_1, E1_2) as e2:
c = e2 # type: E1_2 # E: Incompatible types in assignment (expression has type "Union[E1_1, E1_2]", variable has type "E1_2")
[builtins fixtures/exception.py]

[case testExceptWithAnyTypes]
from typing import Any

E1 = None # type: Any
class E2(BaseException): pass
class NotBaseDerived: pass

try:
pass
except BaseException as e1:
reveal_type(e1) # E: Revealed type is 'builtins.BaseException'
except (E1, BaseException) as e2:
reveal_type(e2) # E: Revealed type is 'Any'
except (E1, E2) as e3:
reveal_type(e3) # E: Revealed type is 'Any'
except (E1, E2, BaseException) as e4:
reveal_type(e4) # E: Revealed type is 'Any'

try: pass
except E1 as e1:
reveal_type(e1) # E: Revealed type is 'Any'
except E2 as e2:
reveal_type(e2) # E: Revealed type is '__main__.E2'
except NotBaseDerived as e3: # E: Exception type must be derived from BaseException
pass
except (NotBaseDerived, E1) as e4: # E: Exception type must be derived from BaseException
pass
except (NotBaseDerived, E2) as e5: # E: Exception type must be derived from BaseException
pass
except (NotBaseDerived, E1, E2) as e6: # E: Exception type must be derived from BaseException
pass
except (E1, E2, NotBaseDerived) as e6: # E: Exception type must be derived from BaseException
pass
[builtins fixtures/exception.py]

[case testReuseTryExceptionVariable]
import typing
class E1(BaseException): pass
Expand Down

0 comments on commit 89ffeb2

Please sign in to comment.