Skip to content

Commit c81477d

Browse files
committed
Rename variables and combine checks in semanal
1 parent d76b8da commit c81477d

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

mypy/semanal.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,33 +1307,28 @@ def process_newtype_declaration(self, s: AssignmentStmt) -> None:
13071307
self.fail("Cannot redefine '%s' as a NewType" % name, s)
13081308
return
13091309

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:
13121312
return
13131313

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)
13191320
else:
13201321
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)
13221323
return
13231324

1324-
# Create the corresponding class def...
1325-
newtype_class_info = self.build_newtype_typeinfo(name, underlying_type, base_type)
1326-
13271325
# ...and add it to the symbol table.
13281326
node = self.lookup(name, s)
13291327
node.kind = GDEF # TODO: locally defined newtype
13301328
call.analyzed = NewTypeExpr(newtype_class_info).set_line(call.line)
13311329
node.node = newtype_class_info
13321330

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:
13371332
class_def = ClassDef(name, Block([]))
13381333
class_def.fullname = self.qualified_name(name)
13391334

@@ -1342,17 +1337,15 @@ def build_newtype_typeinfo(self,
13421337
info.mro = [info] + base_type.type.mro
13431338
info.bases = [base_type]
13441339
info.is_newtype = True
1345-
if isinstance(underlying_type, TupleType):
1346-
info.tuple_type = underlying_type
13471340

13481341
# Add __init__ method
13491342
args = [Argument(Var('cls'), NoneTyp(), None, ARG_POS),
1350-
self.make_argument('item', underlying_type)]
1343+
self.make_argument('item', old_type)]
13511344
signature = CallableType(
1352-
arg_types = [cast(Type, None), underlying_type],
1345+
arg_types = [cast(Type, None), old_type],
13531346
arg_kinds = [arg.kind for arg in args],
13541347
arg_names = ['self', 'item'],
1355-
ret_type = underlying_type,
1348+
ret_type = old_type,
13561349
fallback = self.named_type('__builtins__.function'),
13571350
name = name)
13581351
init_func = FuncDef('__init__', args, Block([]), typ=signature)

0 commit comments

Comments
 (0)