Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit efd62f5

Browse files
committed
CR: Rename .make -> .make_normalized
1 parent 2a19a2b commit efd62f5

14 files changed

+31
-29
lines changed

mypy/checker.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ def is_implicit_any(t: Type) -> bool:
667667
and typ.arg_kinds[0] not in [nodes.ARG_STAR, nodes.ARG_STAR2]):
668668
isclass = defn.is_class or defn.name() in ('__new__', '__init_subclass__')
669669
if isclass:
670-
ref_type = mypy.types.TypeType.make(ref_type)
670+
ref_type = mypy.types.TypeType.make_normalized(ref_type)
671671
erased = erase_to_bound(arg_type)
672672
if not is_subtype_ignoring_tvars(ref_type, erased):
673673
note = None
@@ -2719,7 +2719,7 @@ def convert_to_typetype(type_map: TypeMap) -> TypeMap:
27192719
if not isinstance(typ, (UnionType, Instance)):
27202720
# unknown type; error was likely reported earlier
27212721
return {}
2722-
converted_type_map[expr] = TypeType.make(typ)
2722+
converted_type_map[expr] = TypeType.make_normalized(typ)
27232723
return converted_type_map
27242724

27252725

mypy/checkexpr.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def check_call(self, callee: Type, args: List[Expression],
432432

433433
if (callee.is_type_obj() and (len(arg_types) == 1)
434434
and is_equivalent(callee.ret_type, self.named_type('builtins.type'))):
435-
callee = callee.copy_modified(ret_type=TypeType.make(arg_types[0]))
435+
callee = callee.copy_modified(ret_type=TypeType.make_normalized(arg_types[0]))
436436

437437
if callable_node:
438438
# Store the inferred callable type.
@@ -1108,7 +1108,7 @@ def analyze_descriptor_access(self, instance_type: Type, descriptor_type: Type,
11081108
owner_type = instance_type
11091109

11101110
_, inferred_dunder_get_type = self.check_call(
1111-
dunder_get_type, [TempNode(instance_type), TempNode(TypeType.make(owner_type))],
1111+
dunder_get_type, [TempNode(instance_type), TempNode(TypeType.make_normalized(owner_type))],
11121112
[nodes.ARG_POS, nodes.ARG_POS], context)
11131113

11141114
if isinstance(inferred_dunder_get_type, AnyType):

mypy/checkmember.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ def expand(target: Type) -> Type:
650650
ret_type = func.ret_type
651651
variables = func.variables
652652
if isinstance(original_type, CallableType) and original_type.is_type_obj():
653-
original_type = TypeType.make(original_type.ret_type)
653+
original_type = TypeType.make_normalized(original_type.ret_type)
654654
res = func.copy_modified(arg_types=arg_types,
655655
arg_kinds=func.arg_kinds[1:],
656656
arg_names=func.arg_names[1:],
@@ -665,5 +665,5 @@ def erase_to_bound(t: Type) -> Type:
665665
return t.upper_bound
666666
if isinstance(t, TypeType):
667667
if isinstance(t.item, TypeVarType):
668-
return TypeType.make(t.item.upper_bound)
668+
return TypeType.make_normalized(t.item.upper_bound)
669669
return t

mypy/erasetype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def visit_union_type(self, t: UnionType) -> Type:
7575
return UnionType.make_simplified_union(erased_items)
7676

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

8080

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

mypy/expandtype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def visit_type_type(self, t: TypeType) -> Type:
124124
# union of instances or Any). Sadly we can't report errors
125125
# here yet.
126126
item = t.item.accept(self)
127-
return TypeType.make(item)
127+
return TypeType.make_normalized(item)
128128

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

mypy/join.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def visit_partial_type(self, t: PartialType) -> Type:
245245

246246
def visit_type_type(self, t: TypeType) -> Type:
247247
if isinstance(self.s, TypeType):
248-
return TypeType.make(self.join(t.item, self.s.item), line=t.line)
248+
return TypeType.make_normalized(self.join(t.item, self.s.item), line=t.line)
249249
elif isinstance(self.s, Instance) and self.s.type.fullname() == 'builtins.type':
250250
return self.s
251251
else:

mypy/meet.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def visit_type_type(self, t: TypeType) -> Type:
278278
if isinstance(self.s, TypeType):
279279
typ = self.meet(t.item, self.s.item)
280280
if not isinstance(typ, NoneTyp):
281-
typ = TypeType.make(typ, line=t.line)
281+
typ = TypeType.make_normalized(typ, line=t.line)
282282
return typ
283283
elif isinstance(self.s, Instance) and self.s.type.fullname() == 'builtins.type':
284284
return t

mypy/messages.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def format(self, typ: Type, verbosity: int = 0) -> str:
192192
if func.is_type_obj():
193193
# The type of a type object type can be derived from the
194194
# return type (this always works).
195-
return self.format(TypeType.make(erase_type(func.items()[0].ret_type)), verbosity)
195+
return self.format(TypeType.make_normalized(erase_type(func.items()[0].ret_type)), verbosity)
196196
elif isinstance(func, CallableType):
197197
return_type = strip_quotes(self.format(func.ret_type))
198198
if func.is_ellipsis_args:

mypy/semanal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2168,7 +2168,7 @@ def add_method(funcname: str,
21682168
is_classmethod: bool = False,
21692169
) -> None:
21702170
if is_classmethod:
2171-
first = [Argument(Var('cls'), TypeType.make(selftype), None, ARG_POS)]
2171+
first = [Argument(Var('cls'), TypeType.make_normalized(selftype), None, ARG_POS)]
21722172
else:
21732173
first = [Argument(Var('self'), selftype, None, ARG_POS)]
21742174
args = first + args

mypy/subtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def visit_instance(self, left: Instance) -> bool:
154154
if isinstance(item, TupleType):
155155
item = item.fallback
156156
if is_named_instance(left, 'builtins.type'):
157-
return is_subtype(TypeType.make(AnyType()), right)
157+
return is_subtype(TypeType(AnyType()), right)
158158
if left.type.is_metaclass():
159159
if isinstance(item, AnyType):
160160
return True

mypy/test/testtypes.py

+1-1
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.make(self.fx.o))
583+
self.assert_join(self.fx.type_c, self.fx.type_d, TypeType.make_normalized(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

mypy/typeanal.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
185185
return self.analyze_callable_type(t)
186186
elif fullname == 'typing.Type':
187187
if len(t.args) == 0:
188-
return TypeType.make(AnyType(), line=t.line)
188+
return TypeType(AnyType(), line=t.line)
189189
if len(t.args) != 1:
190190
self.fail('Type[...] must have exactly one type argument', t)
191191
item = self.anal_type(t.args[0])
192-
return TypeType.make(item, line=t.line)
192+
return TypeType.make_normalized(item, line=t.line)
193193
elif fullname == 'typing.ClassVar':
194194
if self.nesting_level > 0:
195195
self.fail('Invalid type: ClassVar nested inside other type', t)
@@ -384,7 +384,7 @@ def visit_ellipsis_type(self, t: EllipsisType) -> Type:
384384
return AnyType()
385385

386386
def visit_type_type(self, t: TypeType) -> Type:
387-
return TypeType.make(self.anal_type(t.item), line=t.line)
387+
return TypeType.make_normalized(self.anal_type(t.item), line=t.line)
388388

389389
def analyze_callable_type(self, t: UnboundType) -> Type:
390390
fallback = self.builtin_type('builtins.function')

mypy/typefixture.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ def make_type_var(name: str, id: int, values: List[Type], upper_bound: Type,
145145
self.lsta = Instance(self.std_listi, [self.a]) # List[A]
146146
self.lstb = Instance(self.std_listi, [self.b]) # List[B]
147147

148-
self.type_a = TypeType.make(self.a)
149-
self.type_b = TypeType.make(self.b)
150-
self.type_c = TypeType.make(self.c)
151-
self.type_d = TypeType.make(self.d)
152-
self.type_t = TypeType.make(self.t)
153-
self.type_any = TypeType.make(self.anyt)
148+
self.type_a = TypeType.make_normalized(self.a)
149+
self.type_b = TypeType.make_normalized(self.b)
150+
self.type_c = TypeType.make_normalized(self.c)
151+
self.type_d = TypeType.make_normalized(self.d)
152+
self.type_t = TypeType.make_normalized(self.t)
153+
self.type_any = TypeType.make_normalized(self.anyt)
154154

155155
# Helper methods
156156

mypy/types.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1183,20 +1183,22 @@ class TypeType(Type):
11831183
# a generic class instance, a union, Any, a type variable...
11841184
item = None # type: Type
11851185

1186-
# This class must be created using make method which may return either TypeType or UnionType.
1187-
# This is to ensure Type[Union[A, B]] is always represented as Union[Type[A], Type[B]].
11881186
def __init__(self, item: Union[Instance, AnyType, TypeVarType, TupleType, NoneTyp,
11891187
CallableType], *, line: int = -1, column: int = -1) -> None:
1188+
"""To ensure Type[Union[A, B]] is always represented as Union[Type[A], Type[B]], item of
1189+
type UnionType must be handled through make_normalized static method.
1190+
"""
1191+
11901192
super().__init__(line, column)
11911193
if isinstance(item, CallableType) and item.is_type_obj():
11921194
self.item = item.fallback
11931195
else:
11941196
self.item = item
11951197

11961198
@staticmethod
1197-
def make(item: Type, *, line: int = -1, column: int = -1) -> Type:
1199+
def make_normalized(item: Type, *, line: int = -1, column: int = -1) -> Type:
11981200
if isinstance(item, UnionType):
1199-
return UnionType.make_union([TypeType.make(union_item) for union_item in item.items],
1201+
return UnionType.make_union([TypeType.make_normalized(union_item) for union_item in item.items],
12001202
line=line, column=column)
12011203
elif isinstance(item, (Instance, AnyType, TypeVarType, TupleType, NoneTyp, CallableType)):
12021204
return TypeType(item, line=line, column=column)
@@ -1212,7 +1214,7 @@ def serialize(self) -> JsonDict:
12121214
@classmethod
12131215
def deserialize(cls, data: JsonDict) -> Type:
12141216
assert data['.class'] == 'TypeType'
1215-
return TypeType.make(deserialize_type(data['item']))
1217+
return TypeType.make_normalized(deserialize_type(data['item']))
12161218

12171219

12181220
#
@@ -1389,7 +1391,7 @@ def visit_overloaded(self, t: Overloaded) -> Type:
13891391
return Overloaded(items=items)
13901392

13911393
def visit_type_type(self, t: TypeType) -> Type:
1392-
return TypeType.make(t.item.accept(self), line=t.line, column=t.column)
1394+
return TypeType.make_normalized(t.item.accept(self), line=t.line, column=t.column)
13931395

13941396

13951397
class TypeStrVisitor(SyntheticTypeVisitor[str]):

0 commit comments

Comments
 (0)