Skip to content

Commit ae03faf

Browse files
committed
Rename variables and combine checks in semanal
1 parent ef16189 commit ae03faf

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
@@ -1297,33 +1297,28 @@ def process_newtype_declaration(self, s: AssignmentStmt) -> None:
12971297
self.fail("Cannot redefine '%s' as a NewType" % name, s)
12981298
return
12991299

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:
13021302
return
13031303

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)
13091310
else:
13101311
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)
13121313
return
13131314

1314-
# Create the corresponding class def...
1315-
newtype_class_info = self.build_newtype_typeinfo(name, underlying_type, base_type)
1316-
13171315
# ...and add it to the symbol table.
13181316
node = self.lookup(name, s)
13191317
node.kind = GDEF # TODO: locally defined newtype
13201318
call.analyzed = NewTypeExpr(newtype_class_info).set_line(call.line)
13211319
node.node = newtype_class_info
13221320

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:
13271322
class_def = ClassDef(name, Block([]))
13281323
class_def.fullname = self.qualified_name(name)
13291324

@@ -1332,17 +1327,15 @@ def build_newtype_typeinfo(self,
13321327
info.mro = [info] + base_type.type.mro
13331328
info.bases = [base_type]
13341329
info.is_newtype = True
1335-
if isinstance(underlying_type, TupleType):
1336-
info.tuple_type = underlying_type
13371330

13381331
# Add __init__ method
13391332
args = [Argument(Var('cls'), NoneTyp(), None, ARG_POS),
1340-
self.make_argument('item', underlying_type)]
1333+
self.make_argument('item', old_type)]
13411334
signature = CallableType(
1342-
arg_types = [cast(Type, None), underlying_type],
1335+
arg_types = [cast(Type, None), old_type],
13431336
arg_kinds = [arg.kind for arg in args],
13441337
arg_names = ['self', 'item'],
1345-
ret_type = underlying_type,
1338+
ret_type = old_type,
13461339
fallback = self.named_type('__builtins__.function'),
13471340
name = name)
13481341
init_func = FuncDef('__init__', args, Block([]), typ=signature)

0 commit comments

Comments
 (0)