Skip to content

Bugfix 7514 #7515

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

Merged
merged 3 commits into from
Oct 3, 2019
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
31 changes: 9 additions & 22 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3047,31 +3047,18 @@ def visit_raise_stmt(self, s: RaiseStmt) -> None:
def type_check_raise(self, e: Expression, s: RaiseStmt,
optional: bool = False) -> None:
typ = get_proper_type(self.expr_checker.accept(e))
if isinstance(typ, TypeType):
if isinstance(typ.item, AnyType):
return
typ = typ.item
if isinstance(typ, FunctionLike):
if typ.is_type_obj():
# Cases like "raise/from ExceptionClass".
typeinfo = typ.type_object()
base = self.lookup_typeinfo('builtins.BaseException')
if base in typeinfo.mro or typeinfo.fallback_to_any:
# Good!
return
# Else fall back to the checks below (which will fail).
if isinstance(typ, TupleType) and self.options.python_version[0] == 2:
exc_type = self.named_type('builtins.BaseException')
expected_type = UnionType([exc_type, TypeType(exc_type)])
if optional:
expected_type.items.append(NoneType())
if self.options.python_version[0] == 2:
# allow `raise type, value, traceback`
# https://docs.python.org/2/reference/simple_stmts.html#the-raise-statement
# TODO: Also check tuple item types.
if len(typ.items) in (2, 3):
return
if isinstance(typ, Instance) and typ.type.fallback_to_any:
# OK!
return
expected_type = self.named_type('builtins.BaseException') # type: Type
if optional:
expected_type = UnionType([expected_type, NoneType()])
any_type = AnyType(TypeOfAny.implementation_artifact)
tuple_type = self.named_type('builtins.tuple')
expected_type.items.append(TupleType([any_type, any_type], tuple_type))
expected_type.items.append(TupleType([any_type, any_type, any_type], tuple_type))
self.check_subtype(typ, expected_type, s, message_registry.INVALID_EXCEPTION)

def visit_try_stmt(self, s: TryStmt) -> None:
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-python2.test
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ raise BaseException, "a"
raise BaseException, "a", None
[builtins_py2 fixtures/exception.pyi]

[case testRaiseTupleTypeFail]
import typing
x = None # type: typing.Type[typing.Tuple[typing.Any, typing.Any, typing.Any]]
raise x # E: Exception must be derived from BaseException
[builtins_py2 fixtures/exception.pyi]

[case testTryExceptWithTuple]
try:
None
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,18 @@ x = int # type: typing.Type[int]
raise x # E: Exception must be derived from BaseException
[builtins fixtures/exception.pyi]

[case testRaiseUnion]
import typing
x = None # type: typing.Union[BaseException, typing.Type[BaseException]]
raise x
[builtins fixtures/exception.pyi]

[case testRaiseNonExceptionUnionFails]
import typing
x = None # type: typing.Union[BaseException, int]
raise x # E: Exception must be derived from BaseException
[builtins fixtures/exception.pyi]

[case testRaiseFromStatement]
e = None # type: BaseException
f = None # type: MyError
Expand Down