Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Any types for tuple except handler tests #1639

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just return AnyType() here instead. UnionType.make_simplified_union returns AnyType if any of the types passed to it are AnyType (as it should).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking at your tests, I take it back. The way you're doing it here correctly catches additional non-Exception-derived types that come after an Any.

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