Skip to content

Commit 937fe14

Browse files
committed
Remove python2 specific logic for raise keyword check
1 parent 5f85898 commit 937fe14

File tree

3 files changed

+1
-84
lines changed

3 files changed

+1
-84
lines changed

mypy/checker.py

-77
Original file line numberDiff line numberDiff line change
@@ -4118,13 +4118,6 @@ def type_check_raise(self, e: Expression, s: RaiseStmt, optional: bool = False)
41184118
self.msg.deleted_as_rvalue(typ, e)
41194119
return
41204120

4121-
if self.options.python_version[0] == 2:
4122-
# Since `raise` has very different rule on python2, we use a different helper.
4123-
# https://github.com/python/mypy/pull/11289
4124-
self._type_check_raise_python2(e, s, typ)
4125-
return
4126-
4127-
# Python3 case:
41284121
exc_type = self.named_type("builtins.BaseException")
41294122
expected_type_items = [exc_type, TypeType(exc_type)]
41304123
if optional:
@@ -4140,76 +4133,6 @@ def type_check_raise(self, e: Expression, s: RaiseStmt, optional: bool = False)
41404133
# https://github.com/python/mypy/issues/11089
41414134
self.expr_checker.check_call(typ, [], [], e)
41424135

4143-
def _type_check_raise_python2(self, e: Expression, s: RaiseStmt, typ: ProperType) -> None:
4144-
# Python2 has two possible major cases:
4145-
# 1. `raise expr`, where `expr` is some expression, it can be:
4146-
# - Exception typ
4147-
# - Exception instance
4148-
# - Old style class (not supported)
4149-
# - Tuple, where 0th item is exception type or instance
4150-
# 2. `raise exc, msg, traceback`, where:
4151-
# - `exc` is exception type (not instance!)
4152-
# - `traceback` is `types.TracebackType | None`
4153-
# Important note: `raise exc, msg` is not the same as `raise (exc, msg)`
4154-
# We call `raise exc, msg, traceback` - legacy mode.
4155-
exc_type = self.named_type("builtins.BaseException")
4156-
exc_inst_or_type = UnionType([exc_type, TypeType(exc_type)])
4157-
4158-
if not s.legacy_mode and (
4159-
isinstance(typ, TupleType)
4160-
and typ.items
4161-
or (isinstance(typ, Instance) and typ.args and typ.type.fullname == "builtins.tuple")
4162-
):
4163-
# `raise (exc, ...)` case:
4164-
item = typ.items[0] if isinstance(typ, TupleType) else typ.args[0]
4165-
self.check_subtype(
4166-
item,
4167-
exc_inst_or_type,
4168-
s,
4169-
"When raising a tuple, first element must by derived from BaseException",
4170-
)
4171-
return
4172-
elif s.legacy_mode:
4173-
# `raise Exception, msg` case
4174-
# `raise Exception, msg, traceback` case
4175-
# https://docs.python.org/2/reference/simple_stmts.html#the-raise-statement
4176-
assert isinstance(typ, TupleType) # Is set in fastparse2.py
4177-
if len(typ.items) >= 2 and isinstance(get_proper_type(typ.items[1]), NoneType):
4178-
expected_type: Type = exc_inst_or_type
4179-
else:
4180-
expected_type = TypeType(exc_type)
4181-
self.check_subtype(
4182-
typ.items[0], expected_type, s, f'Argument 1 must be "{expected_type}" subtype'
4183-
)
4184-
4185-
# Typecheck `traceback` part:
4186-
if len(typ.items) == 3:
4187-
# Now, we typecheck `traceback` argument if it is present.
4188-
# We do this after the main check for better error message
4189-
# and better ordering: first about `BaseException` subtype,
4190-
# then about `traceback` type.
4191-
traceback_type = UnionType.make_union(
4192-
[self.named_type("types.TracebackType"), NoneType()]
4193-
)
4194-
self.check_subtype(
4195-
typ.items[2],
4196-
traceback_type,
4197-
s,
4198-
f'Argument 3 must be "{traceback_type}" subtype',
4199-
)
4200-
else:
4201-
expected_type_items = [
4202-
# `raise Exception` and `raise Exception()` cases:
4203-
exc_type,
4204-
TypeType(exc_type),
4205-
]
4206-
self.check_subtype(
4207-
typ,
4208-
UnionType.make_union(expected_type_items),
4209-
s,
4210-
message_registry.INVALID_EXCEPTION,
4211-
)
4212-
42134136
def visit_try_stmt(self, s: TryStmt) -> None:
42144137
"""Type check a try statement."""
42154138
# Our enclosing frame will get the result if the try/except falls through.

mypy/fastparse2.py

-3
Original file line numberDiff line numberDiff line change
@@ -767,21 +767,18 @@ def visit_With(self, n: ast27.With) -> WithStmt:
767767

768768
# 'raise' [test [',' test [',' test]]]
769769
def visit_Raise(self, n: ast27.Raise) -> RaiseStmt:
770-
legacy_mode = False
771770
if n.type is None:
772771
e = None
773772
else:
774773
if n.inst is None:
775774
e = self.visit(n.type)
776775
else:
777-
legacy_mode = True
778776
if n.tback is None:
779777
e = TupleExpr([self.visit(n.type), self.visit(n.inst)])
780778
else:
781779
e = TupleExpr([self.visit(n.type), self.visit(n.inst), self.visit(n.tback)])
782780

783781
stmt = RaiseStmt(e, None)
784-
stmt.legacy_mode = legacy_mode
785782
return self.set_line(stmt, n)
786783

787784
# TryExcept(stmt* body, excepthandler* handlers, stmt* orelse)

mypy/nodes.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1424,19 +1424,16 @@ def accept(self, visitor: StatementVisitor[T]) -> T:
14241424

14251425

14261426
class RaiseStmt(Statement):
1427-
__slots__ = ("expr", "from_expr", "legacy_mode")
1427+
__slots__ = ("expr", "from_expr")
14281428

14291429
# Plain 'raise' is a valid statement.
14301430
expr: Optional[Expression]
14311431
from_expr: Optional[Expression]
1432-
# Is set when python2 has `raise exc, msg, traceback`.
1433-
legacy_mode: bool
14341432

14351433
def __init__(self, expr: Optional[Expression], from_expr: Optional[Expression]) -> None:
14361434
super().__init__()
14371435
self.expr = expr
14381436
self.from_expr = from_expr
1439-
self.legacy_mode = False
14401437

14411438
def accept(self, visitor: StatementVisitor[T]) -> T:
14421439
return visitor.visit_raise_stmt(self)

0 commit comments

Comments
 (0)