@@ -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.
0 commit comments