Skip to content

Commit bffeb2e

Browse files
Rename builtin_type to named_type (#11224)
Currently TypeChecker.named_type is been called inside MemberContext.builtin_type thus this PR renames MemberContext.builtin_type with named_type for consistency. Another reason for this change is that we sometimes call this function for non-builtins.
1 parent 1d04059 commit bffeb2e

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

mypy/checkmember.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __init__(self,
6060
self.chk = chk
6161
self.module_symbol_table = module_symbol_table
6262

63-
def builtin_type(self, name: str) -> Instance:
63+
def named_type(self, name: str) -> Instance:
6464
return self.chk.named_type(name)
6565

6666
def not_ready_callback(self, name: str, context: Context) -> None:
@@ -202,7 +202,7 @@ def analyze_instance_member_access(name: str,
202202
return analyze_var(name, first_item.var, typ, info, mx)
203203
if mx.is_lvalue:
204204
mx.msg.cant_assign_to_method(mx.context)
205-
signature = function_type(method, mx.builtin_type('builtins.function'))
205+
signature = function_type(method, mx.named_type('builtins.function'))
206206
signature = freshen_function_type_vars(signature)
207207
if name == '__new__':
208208
# __new__ is special and behaves like a static method -- don't strip
@@ -266,7 +266,7 @@ def analyze_type_type_member_access(name: str,
266266
override_info: Optional[TypeInfo]) -> Type:
267267
# Similar to analyze_type_callable_attribute_access.
268268
item = None
269-
fallback = mx.builtin_type('builtins.type')
269+
fallback = mx.named_type('builtins.type')
270270
ignore_messages = mx.msg.copy()
271271
ignore_messages.disable_errors().__enter__()
272272
if isinstance(typ.item, Instance):
@@ -326,10 +326,10 @@ def analyze_none_member_access(name: str, typ: NoneType, mx: MemberContext) -> T
326326
return CallableType(arg_types=[],
327327
arg_kinds=[],
328328
arg_names=[],
329-
ret_type=mx.builtin_type('builtins.bool'),
330-
fallback=mx.builtin_type('builtins.function'))
329+
ret_type=mx.named_type('builtins.bool'),
330+
fallback=mx.named_type('builtins.function'))
331331
else:
332-
return _analyze_member_access(name, mx.builtin_type('builtins.object'), mx)
332+
return _analyze_member_access(name, mx.named_type('builtins.object'), mx)
333333

334334

335335
def analyze_member_var_access(name: str,
@@ -354,7 +354,7 @@ def analyze_member_var_access(name: str,
354354
# If the associated variable is a TypeInfo synthesize a Var node for
355355
# the purposes of type checking. This enables us to type check things
356356
# like accessing class attributes on an inner class.
357-
v = Var(name, type=type_object_type(vv, mx.builtin_type))
357+
v = Var(name, type=type_object_type(vv, mx.named_type))
358358
v.info = info
359359

360360
if isinstance(vv, TypeAlias) and isinstance(get_proper_type(vv.target), Instance):
@@ -364,7 +364,7 @@ def analyze_member_var_access(name: str,
364364
# class C:
365365
# A = List[int]
366366
# x = C.A() <- this is OK
367-
typ = instance_alias_type(vv, mx.builtin_type)
367+
typ = instance_alias_type(vv, mx.named_type)
368368
v = Var(name, type=typ)
369369
v.info = info
370370

@@ -388,7 +388,7 @@ def analyze_member_var_access(name: str,
388388
# the guard this search will always find object.__getattribute__ and conclude
389389
# that the attribute exists
390390
if method and method.info.fullname != 'builtins.object':
391-
function = function_type(method, mx.builtin_type('builtins.function'))
391+
function = function_type(method, mx.named_type('builtins.function'))
392392
bound_method = bind_self(function, mx.self_type)
393393
typ = map_instance_to_supertype(itype, method.info)
394394
getattr_type = get_proper_type(expand_type_by_instance(bound_method, typ))
@@ -405,7 +405,7 @@ def analyze_member_var_access(name: str,
405405
else:
406406
setattr_meth = info.get_method('__setattr__')
407407
if setattr_meth and setattr_meth.info.fullname != 'builtins.object':
408-
setattr_func = function_type(setattr_meth, mx.builtin_type('builtins.function'))
408+
setattr_func = function_type(setattr_meth, mx.named_type('builtins.function'))
409409
bound_type = bind_self(setattr_func, mx.self_type)
410410
typ = map_instance_to_supertype(itype, setattr_meth.info)
411411
setattr_type = get_proper_type(expand_type_by_instance(bound_type, typ))
@@ -615,7 +615,7 @@ def analyze_var(name: str,
615615
fullname = '{}.{}'.format(var.info.fullname, name)
616616
hook = mx.chk.plugin.get_attribute_hook(fullname)
617617
if result and not mx.is_lvalue and not implicit:
618-
result = analyze_descriptor_access(mx.original_type, result, mx.builtin_type,
618+
result = analyze_descriptor_access(mx.original_type, result, mx.named_type,
619619
mx.msg, mx.context, chk=mx.chk)
620620
if hook:
621621
result = hook(AttributeContext(get_proper_type(mx.original_type),
@@ -792,7 +792,7 @@ def analyze_class_attribute_access(itype: Instance,
792792
result = add_class_tvars(t, isuper, is_classmethod,
793793
mx.self_type, original_vars=original_vars)
794794
if not mx.is_lvalue:
795-
result = analyze_descriptor_access(mx.original_type, result, mx.builtin_type,
795+
result = analyze_descriptor_access(mx.original_type, result, mx.named_type,
796796
mx.msg, mx.context, chk=mx.chk)
797797
return result
798798
elif isinstance(node.node, Var):
@@ -805,15 +805,15 @@ def analyze_class_attribute_access(itype: Instance,
805805
return AnyType(TypeOfAny.from_error)
806806

807807
if isinstance(node.node, TypeInfo):
808-
return type_object_type(node.node, mx.builtin_type)
808+
return type_object_type(node.node, mx.named_type)
809809

810810
if isinstance(node.node, MypyFile):
811811
# Reference to a module object.
812-
return mx.builtin_type('types.ModuleType')
812+
return mx.named_type('types.ModuleType')
813813

814814
if (isinstance(node.node, TypeAlias) and
815815
isinstance(get_proper_type(node.node.target), Instance)):
816-
return instance_alias_type(node.node, mx.builtin_type)
816+
return instance_alias_type(node.node, mx.named_type)
817817

818818
if is_decorated:
819819
assert isinstance(node.node, Decorator)
@@ -824,7 +824,7 @@ def analyze_class_attribute_access(itype: Instance,
824824
return AnyType(TypeOfAny.from_error)
825825
else:
826826
assert isinstance(node.node, FuncBase)
827-
typ = function_type(node.node, mx.builtin_type('builtins.function'))
827+
typ = function_type(node.node, mx.named_type('builtins.function'))
828828
# Note: if we are accessing class method on class object, the cls argument is bound.
829829
# Annotated and/or explicit class methods go through other code paths above, for
830830
# unannotated implicit class methods we do this here.
@@ -913,7 +913,7 @@ class B(A[str]): pass
913913
return t
914914

915915

916-
def type_object_type(info: TypeInfo, builtin_type: Callable[[str], Instance]) -> ProperType:
916+
def type_object_type(info: TypeInfo, named_type: Callable[[str], Instance]) -> ProperType:
917917
"""Return the type of a type object.
918918
919919
For a generic type G with type variables T and S the type is generally of form
@@ -945,7 +945,7 @@ def type_object_type(info: TypeInfo, builtin_type: Callable[[str], Instance]) ->
945945
init_index = info.mro.index(init_method.node.info)
946946
new_index = info.mro.index(new_method.node.info)
947947

948-
fallback = info.metaclass_type or builtin_type('builtins.type')
948+
fallback = info.metaclass_type or named_type('builtins.type')
949949
if init_index < new_index:
950950
method: Union[FuncBase, Decorator] = init_method.node
951951
is_new = False
@@ -963,7 +963,7 @@ def type_object_type(info: TypeInfo, builtin_type: Callable[[str], Instance]) ->
963963
arg_kinds=[ARG_STAR, ARG_STAR2],
964964
arg_names=["_args", "_kwds"],
965965
ret_type=any_type,
966-
fallback=builtin_type('builtins.function'))
966+
fallback=named_type('builtins.function'))
967967
return class_callable(sig, info, fallback, None, is_new=False)
968968

969969
# Otherwise prefer __init__ in a tie. It isn't clear that this

0 commit comments

Comments
 (0)