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

Now typecheckes traceback in raise e, msg, traceback on py2 #11289

Merged
merged 6 commits into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 41 additions & 7 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3471,24 +3471,58 @@ def type_check_raise(self, e: Expression, s: RaiseStmt,
if isinstance(typ, DeletedType):
self.msg.deleted_as_rvalue(typ, e)
return

exc_type = self.named_type('builtins.BaseException')
expected_type = UnionType([exc_type, TypeType(exc_type)])
expected_type_items = [exc_type, TypeType(exc_type)]
if optional:
expected_type.items.append(NoneType())
if self.options.python_version[0] == 2:
expected_type_items.append(NoneType())

if (self.options.python_version[0] == 2
and isinstance(typ, TupleType)
and len(typ.items) >= 2):
# allow `raise type, value, traceback`
# https://docs.python.org/2/reference/simple_stmts.html#the-raise-statement
# TODO: Also check tuple item types.
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)

# Right now we push all types as `Any`,
# because we will type-check `traceback` later.
# `msg` is always `Any`.
expected_type_items.extend([
# `raise Exception, msg` case:
TupleType([TypeType(exc_type), any_type], tuple_type),
# `raise Exception, msg, traceback` case:
TupleType([TypeType(exc_type), any_type, any_type], tuple_type),
])

self.check_subtype(
typ, UnionType.make_union(expected_type_items),
s, message_registry.INVALID_EXCEPTION,
)

if isinstance(typ, FunctionLike):
# https://github.com/python/mypy/issues/11089
self.expr_checker.check_call(typ, [], [], e)

# `raise Exception, msg, traceback` case
if (self.options.python_version[0] == 2
and isinstance(typ, TupleType)
and len(typ.items) == 3):
# Now, we typecheck `traceback` argument it is present.
# We do this after the main check for better error message
# and better ordering: first about `BaseException` subtype,
# then about `traceback` type.

traceback_type = UnionType.make_union([
self.named_type('types.TracebackType'),
NoneType(),
])
self.check_subtype(
typ.items[2],
traceback_type, s,
'Third argument to raise must have "{}" type'.format(traceback_type),
)

def visit_try_stmt(self, s: TryStmt) -> None:
"""Type check a try statement."""
# Our enclosing frame will get the result if the try/except falls through.
Expand Down
17 changes: 17 additions & 0 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1531,3 +1531,20 @@ _testNoPython3StubAvailable.py:1: note: See https://mypy.readthedocs.io/en/stabl
_testNoPython3StubAvailable.py:3: error: Library stubs not installed for "maxminddb" (or incompatible with Python 3.6)
_testNoPython3StubAvailable.py:3: note: Hint: "python3 -m pip install types-maxminddb"
_testNoPython3StubAvailable.py:3: note: (or run "mypy --install-types" to install all missing stub packages)

[case testRaiseTupleOfThreeOnPython2]
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
from types import TracebackType
from typing import Optional

raise Exception # ok
raise Exception(1) # ok
raise Exception, 1 # ok

e = None # type: Optional[TracebackType]
raise Exception, 1, e # ok
raise Exception, 1, None

raise int, 1 # E: Exception must be derived from BaseException
raise Exception, 1, 1 # E: Third argument to raise must have "Union[types.TracebackType, None]" type
raise int, 1, 1 # E: Exception must be derived from BaseException \
# E: Third argument to raise must have "Union[types.TracebackType, None]" type