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