Skip to content

Commit 10aebec

Browse files
committed
Remove python2 specific logic for raise keyword check
1 parent 5718dff commit 10aebec

File tree

3 files changed

+1
-78
lines changed

3 files changed

+1
-78
lines changed

mypy/checker.py

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3698,13 +3698,6 @@ def type_check_raise(self, e: Expression, s: RaiseStmt,
36983698
self.msg.deleted_as_rvalue(typ, e)
36993699
return
37003700

3701-
if self.options.python_version[0] == 2:
3702-
# Since `raise` has very different rule on python2, we use a different helper.
3703-
# https://github.com/python/mypy/pull/11289
3704-
self._type_check_raise_python2(e, s, typ)
3705-
return
3706-
3707-
# Python3 case:
37083701
exc_type = self.named_type('builtins.BaseException')
37093702
expected_type_items = [exc_type, TypeType(exc_type)]
37103703
if optional:
@@ -3721,70 +3714,6 @@ def type_check_raise(self, e: Expression, s: RaiseStmt,
37213714
# https://github.com/python/mypy/issues/11089
37223715
self.expr_checker.check_call(typ, [], [], e)
37233716

3724-
def _type_check_raise_python2(self, e: Expression, s: RaiseStmt, typ: ProperType) -> None:
3725-
# Python2 has two possible major cases:
3726-
# 1. `raise expr`, where `expr` is some expression, it can be:
3727-
# - Exception typ
3728-
# - Exception instance
3729-
# - Old style class (not supported)
3730-
# - Tuple, where 0th item is exception type or instance
3731-
# 2. `raise exc, msg, traceback`, where:
3732-
# - `exc` is exception type (not instance!)
3733-
# - `traceback` is `types.TracebackType | None`
3734-
# Important note: `raise exc, msg` is not the same as `raise (exc, msg)`
3735-
# We call `raise exc, msg, traceback` - legacy mode.
3736-
exc_type = self.named_type('builtins.BaseException')
3737-
exc_inst_or_type = UnionType([exc_type, TypeType(exc_type)])
3738-
3739-
if (not s.legacy_mode and (isinstance(typ, TupleType) and typ.items
3740-
or (isinstance(typ, Instance) and typ.args
3741-
and typ.type.fullname == 'builtins.tuple'))):
3742-
# `raise (exc, ...)` case:
3743-
item = typ.items[0] if isinstance(typ, TupleType) else typ.args[0]
3744-
self.check_subtype(
3745-
item, exc_inst_or_type, s,
3746-
'When raising a tuple, first element must by derived from BaseException',
3747-
)
3748-
return
3749-
elif s.legacy_mode:
3750-
# `raise Exception, msg` case
3751-
# `raise Exception, msg, traceback` case
3752-
# https://docs.python.org/2/reference/simple_stmts.html#the-raise-statement
3753-
assert isinstance(typ, TupleType) # Is set in fastparse2.py
3754-
if (len(typ.items) >= 2
3755-
and isinstance(get_proper_type(typ.items[1]), NoneType)):
3756-
expected_type: Type = exc_inst_or_type
3757-
else:
3758-
expected_type = TypeType(exc_type)
3759-
self.check_subtype(
3760-
typ.items[0], expected_type, s,
3761-
f'Argument 1 must be "{expected_type}" subtype',
3762-
)
3763-
3764-
# Typecheck `traceback` part:
3765-
if len(typ.items) == 3:
3766-
# Now, we typecheck `traceback` argument if it is present.
3767-
# We do this after the main check for better error message
3768-
# and better ordering: first about `BaseException` subtype,
3769-
# then about `traceback` type.
3770-
traceback_type = UnionType.make_union([
3771-
self.named_type('types.TracebackType'),
3772-
NoneType(),
3773-
])
3774-
self.check_subtype(
3775-
typ.items[2], traceback_type, s,
3776-
f'Argument 3 must be "{traceback_type}" subtype',
3777-
)
3778-
else:
3779-
expected_type_items = [
3780-
# `raise Exception` and `raise Exception()` cases:
3781-
exc_type, TypeType(exc_type),
3782-
]
3783-
self.check_subtype(
3784-
typ, UnionType.make_union(expected_type_items),
3785-
s, message_registry.INVALID_EXCEPTION,
3786-
)
3787-
37883717
def visit_try_stmt(self, s: TryStmt) -> None:
37893718
"""Type check a try statement."""
37903719
# Our enclosing frame will get the result if the try/except falls through.

mypy/fastparse2.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,21 +668,18 @@ def visit_With(self, n: ast27.With) -> WithStmt:
668668

669669
# 'raise' [test [',' test [',' test]]]
670670
def visit_Raise(self, n: ast27.Raise) -> RaiseStmt:
671-
legacy_mode = False
672671
if n.type is None:
673672
e = None
674673
else:
675674
if n.inst is None:
676675
e = self.visit(n.type)
677676
else:
678-
legacy_mode = True
679677
if n.tback is None:
680678
e = TupleExpr([self.visit(n.type), self.visit(n.inst)])
681679
else:
682680
e = TupleExpr([self.visit(n.type), self.visit(n.inst), self.visit(n.tback)])
683681

684682
stmt = RaiseStmt(e, None)
685-
stmt.legacy_mode = legacy_mode
686683
return self.set_line(stmt, n)
687684

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

mypy/nodes.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,19 +1335,16 @@ def accept(self, visitor: StatementVisitor[T]) -> T:
13351335

13361336

13371337
class RaiseStmt(Statement):
1338-
__slots__ = ('expr', 'from_expr', 'legacy_mode')
1338+
__slots__ = ('expr', 'from_expr')
13391339

13401340
# Plain 'raise' is a valid statement.
13411341
expr: Optional[Expression]
13421342
from_expr: Optional[Expression]
1343-
# Is set when python2 has `raise exc, msg, traceback`.
1344-
legacy_mode: bool
13451343

13461344
def __init__(self, expr: Optional[Expression], from_expr: Optional[Expression]) -> None:
13471345
super().__init__()
13481346
self.expr = expr
13491347
self.from_expr = from_expr
1350-
self.legacy_mode = False
13511348

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

0 commit comments

Comments
 (0)