Skip to content

Commit 408f19e

Browse files
committed
Standardize Type[Union...]] -> Union[Type[...]]
* Add more tests for Type[Union[...]] * Force TypeType.make() to be used everywhere instead of TypeType() * Accidentally fixed testTypeUsingTypeCClassMethodUnion
1 parent aca05c7 commit 408f19e

15 files changed

+98
-35
lines changed

mypy/checker.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ def is_implicit_any(t: Type) -> bool:
663663
and typ.arg_kinds[0] not in [nodes.ARG_STAR, nodes.ARG_STAR2]):
664664
isclass = defn.is_class or defn.name() in ('__new__', '__init_subclass__')
665665
if isclass:
666-
ref_type = mypy.types.TypeType(ref_type)
666+
ref_type = mypy.types.TypeType.make(ref_type)
667667
erased = erase_to_bound(arg_type)
668668
if not is_subtype_ignoring_tvars(ref_type, erased):
669669
note = None
@@ -2680,13 +2680,10 @@ def convert_to_typetype(type_map: TypeMap) -> TypeMap:
26802680
if type_map is None:
26812681
return None
26822682
for expr, typ in type_map.items():
2683-
if isinstance(typ, UnionType):
2684-
converted_type_map[expr] = UnionType([TypeType(t) for t in typ.items])
2685-
elif isinstance(typ, Instance):
2686-
converted_type_map[expr] = TypeType(typ)
2687-
else:
2683+
if not isinstance(typ, (UnionType, Instance)):
26882684
# unknown type; error was likely reported earlier
26892685
return {}
2686+
converted_type_map[expr] = TypeType.make(typ)
26902687
return converted_type_map
26912688

26922689

mypy/checkexpr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def check_call(self, callee: Type, args: List[Expression],
401401

402402
if (callee.is_type_obj() and (len(arg_types) == 1)
403403
and is_equivalent(callee.ret_type, self.named_type('builtins.type'))):
404-
callee = callee.copy_modified(ret_type=TypeType(arg_types[0]))
404+
callee = callee.copy_modified(ret_type=TypeType.make(arg_types[0]))
405405

406406
if callable_node:
407407
# Store the inferred callable type.
@@ -1071,7 +1071,7 @@ def analyze_descriptor_access(self, instance_type: Type, descriptor_type: Type,
10711071
owner_type = instance_type
10721072

10731073
_, inferred_dunder_get_type = self.check_call(
1074-
dunder_get_type, [TempNode(instance_type), TempNode(TypeType(owner_type))],
1074+
dunder_get_type, [TempNode(instance_type), TempNode(TypeType.make(owner_type))],
10751075
[nodes.ARG_POS, nodes.ARG_POS], context)
10761076

10771077
if isinstance(inferred_dunder_get_type, AnyType):

mypy/checkmember.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ def expand(target: Type) -> Type:
633633
ret_type = func.ret_type
634634
variables = func.variables
635635
if isinstance(original_type, CallableType) and original_type.is_type_obj():
636-
original_type = TypeType(original_type.ret_type)
636+
original_type = TypeType.make(original_type.ret_type)
637637
res = func.copy_modified(arg_types=arg_types,
638638
arg_kinds=func.arg_kinds[1:],
639639
arg_names=func.arg_names[1:],
@@ -648,5 +648,5 @@ def erase_to_bound(t: Type) -> Type:
648648
return t.upper_bound
649649
if isinstance(t, TypeType):
650650
if isinstance(t.item, TypeVarType):
651-
return TypeType(t.item.upper_bound)
651+
return TypeType.make(t.item.upper_bound)
652652
return t

mypy/erasetype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def visit_union_type(self, t: UnionType) -> Type:
7777
return UnionType.make_simplified_union(erased_items)
7878

7979
def visit_type_type(self, t: TypeType) -> Type:
80-
return TypeType(t.item.accept(self), line=t.line)
80+
return TypeType.make(t.item.accept(self), line=t.line)
8181

8282

8383
def erase_typevars(t: Type, ids_to_erase: Optional[Container[TypeVarId]] = None) -> Type:

mypy/expandtype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def visit_type_type(self, t: TypeType) -> Type:
127127
# union of instances or Any). Sadly we can't report errors
128128
# here yet.
129129
item = t.item.accept(self)
130-
return TypeType(item)
130+
return TypeType.make(item)
131131

132132
def expand_types(self, types: Iterable[Type]) -> List[Type]:
133133
a = [] # type: List[Type]

mypy/join.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def visit_partial_type(self, t: PartialType) -> Type:
248248

249249
def visit_type_type(self, t: TypeType) -> Type:
250250
if isinstance(self.s, TypeType):
251-
return TypeType(self.join(t.item, self.s.item), line=t.line)
251+
return TypeType.make(self.join(t.item, self.s.item), line=t.line)
252252
elif isinstance(self.s, Instance) and self.s.type.fullname() == 'builtins.type':
253253
return self.s
254254
else:

mypy/meet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def visit_type_type(self, t: TypeType) -> Type:
276276
if isinstance(self.s, TypeType):
277277
typ = self.meet(t.item, self.s.item)
278278
if not isinstance(typ, NoneTyp):
279-
typ = TypeType(typ, line=t.line)
279+
typ = TypeType.make(typ, line=t.line)
280280
return typ
281281
elif isinstance(self.s, Instance) and self.s.type.fullname() == 'builtins.type':
282282
return t

mypy/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2212,7 +2212,7 @@ def add_method(funcname: str,
22122212
is_classmethod: bool = False,
22132213
) -> None:
22142214
if is_classmethod:
2215-
first = [Argument(Var('cls'), TypeType(selftype), None, ARG_POS)]
2215+
first = [Argument(Var('cls'), TypeType.make(selftype), None, ARG_POS)]
22162216
else:
22172217
first = [Argument(Var('self'), selftype, None, ARG_POS)]
22182218
args = first + args

mypy/subtypes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def is_subtype(left: Type, right: Type,
7070
# otherwise, fall through
7171
# Treat builtins.type the same as Type[Any]
7272
elif is_named_instance(left, 'builtins.type'):
73-
return is_subtype(TypeType(AnyType()), right)
73+
return is_subtype(TypeType.make(AnyType()), right)
7474
elif is_named_instance(right, 'builtins.type'):
75-
return is_subtype(left, TypeType(AnyType()))
75+
return is_subtype(left, TypeType.make(AnyType()))
7676
return left.accept(SubtypeVisitor(right, type_parameter_checker,
7777
ignore_pos_arg_names=ignore_pos_arg_names))
7878

mypy/test/testtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ def test_type_type(self) -> None:
580580
self.assert_join(self.fx.type_b, self.fx.type_any, self.fx.type_any)
581581
self.assert_join(self.fx.type_b, self.fx.type_type, self.fx.type_type)
582582
self.assert_join(self.fx.type_b, self.fx.type_c, self.fx.type_a)
583-
self.assert_join(self.fx.type_c, self.fx.type_d, TypeType(self.fx.o))
583+
self.assert_join(self.fx.type_c, self.fx.type_d, TypeType.make(self.fx.o))
584584
self.assert_join(self.fx.type_type, self.fx.type_any, self.fx.type_type)
585585
self.assert_join(self.fx.type_b, self.fx.anyt, self.fx.anyt)
586586

0 commit comments

Comments
 (0)