Skip to content
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
4 changes: 4 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2357,6 +2357,10 @@ def visit_raise_stmt(self, s: RaiseStmt) -> None:
def type_check_raise(self, e: Expression, s: RaiseStmt,
optional: bool = False) -> None:
typ = self.expr_checker.accept(e)
if isinstance(typ, TypeType):
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't (for consistency) the same case be added to check_except_handler_test (with test cases)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if you mean that this should work:

[case testExceptWithTypeType]
import typing
E = BaseException  # type: typing.Type[BaseException]

try:
    pass
except E:
    pass
[builtins fixtures/exception.pyi]

it already does, though I can certainly add that test.

if isinstance(typ.item, AnyType):
return
typ = typ.item
if isinstance(typ, FunctionLike):
if typ.is_type_obj():
# Cases like "raise/from ExceptionClass".
Expand Down
24 changes: 22 additions & 2 deletions test-data/unit/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,19 @@ raise object # E: Exception must be derived from BaseException
raise f # E: Exception must be derived from BaseException
[builtins fixtures/exception.pyi]

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

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

[case testRaiseFromStatement]
e = None # type: BaseException
f = None # type: MyError
a = None # type: A
Expand Down Expand Up @@ -628,6 +639,16 @@ except ((E1, E2)): pass
except (((E1, E2))): pass
[builtins fixtures/exception.pyi]

[case testExceptWithTypeType]
import typing
E = BaseException # type: typing.Type[BaseException]

try:
pass
except E:
pass
[builtins fixtures/exception.pyi]

[case testExceptWithMultipleTypes2]
import typing
class E1(BaseException): pass
Expand Down Expand Up @@ -1615,4 +1636,3 @@ N = TypedDict('N', {'x': int})
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]
[out]