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