@@ -1297,33 +1297,28 @@ def process_newtype_declaration(self, s: AssignmentStmt) -> None:
1297
1297
self .fail ("Cannot redefine '%s' as a NewType" % name , s )
1298
1298
return
1299
1299
1300
- underlying_type = self .check_newtype_args (call , name , s )
1301
- if underlying_type is None :
1300
+ old_type = self .check_newtype_args (call , name , s )
1301
+ if old_type is None :
1302
1302
return
1303
1303
1304
- # Check if class is subtypeable
1305
- if isinstance (underlying_type , TupleType ):
1306
- base_type = underlying_type .fallback
1307
- elif isinstance (underlying_type , Instance ):
1308
- base_type = underlying_type
1304
+ # Create the corresponding class def if it's subtypeable...
1305
+ if isinstance (old_type , TupleType ):
1306
+ newtype_class_info = self .build_newtype_typeinfo (name , old_type , old_type .fallback )
1307
+ newtype_class_info .tuple_type = old_type
1308
+ elif isinstance (old_type , Instance ):
1309
+ newtype_class_info = self .build_newtype_typeinfo (name , old_type , old_type )
1309
1310
else :
1310
1311
message = "Argument 2 to NewType(...) must be subclassable (got {})"
1311
- self .fail (message .format (underlying_type ), s )
1312
+ self .fail (message .format (old_type ), s )
1312
1313
return
1313
1314
1314
- # Create the corresponding class def...
1315
- newtype_class_info = self .build_newtype_typeinfo (name , underlying_type , base_type )
1316
-
1317
1315
# ...and add it to the symbol table.
1318
1316
node = self .lookup (name , s )
1319
1317
node .kind = GDEF # TODO: locally defined newtype
1320
1318
call .analyzed = NewTypeExpr (newtype_class_info ).set_line (call .line )
1321
1319
node .node = newtype_class_info
1322
1320
1323
- def build_newtype_typeinfo (self ,
1324
- name : str ,
1325
- underlying_type : Type ,
1326
- base_type : Instance ) -> TypeInfo :
1321
+ def build_newtype_typeinfo (self ,name : str , old_type : Type , base_type : Instance ) -> TypeInfo :
1327
1322
class_def = ClassDef (name , Block ([]))
1328
1323
class_def .fullname = self .qualified_name (name )
1329
1324
@@ -1332,17 +1327,15 @@ def build_newtype_typeinfo(self,
1332
1327
info .mro = [info ] + base_type .type .mro
1333
1328
info .bases = [base_type ]
1334
1329
info .is_newtype = True
1335
- if isinstance (underlying_type , TupleType ):
1336
- info .tuple_type = underlying_type
1337
1330
1338
1331
# Add __init__ method
1339
1332
args = [Argument (Var ('cls' ), NoneTyp (), None , ARG_POS ),
1340
- self .make_argument ('item' , underlying_type )]
1333
+ self .make_argument ('item' , old_type )]
1341
1334
signature = CallableType (
1342
- arg_types = [cast (Type , None ), underlying_type ],
1335
+ arg_types = [cast (Type , None ), old_type ],
1343
1336
arg_kinds = [arg .kind for arg in args ],
1344
1337
arg_names = ['self' , 'item' ],
1345
- ret_type = underlying_type ,
1338
+ ret_type = old_type ,
1346
1339
fallback = self .named_type ('__builtins__.function' ),
1347
1340
name = name )
1348
1341
init_func = FuncDef ('__init__' , args , Block ([]), typ = signature )
0 commit comments