diff --git a/mypy/checkmember.py b/mypy/checkmember.py index 3579fda2e7f44..3efc39753627e 100644 --- a/mypy/checkmember.py +++ b/mypy/checkmember.py @@ -438,7 +438,7 @@ def check_final_member(name: str, info: TypeInfo, msg: MessageBuilder, ctx: Cont def analyze_descriptor_access(instance_type: Type, descriptor_type: Type, - builtin_type: Callable[[str], Instance], + named_type: Callable[[str], Instance], msg: MessageBuilder, context: Context, *, chk: 'mypy.checker.TypeChecker') -> Type: @@ -460,7 +460,7 @@ def analyze_descriptor_access(instance_type: Type, if isinstance(descriptor_type, UnionType): # Map the access over union types return make_simplified_union([ - analyze_descriptor_access(instance_type, typ, builtin_type, + analyze_descriptor_access(instance_type, typ, named_type, msg, context, chk=chk) for typ in descriptor_type.items ]) @@ -476,7 +476,7 @@ def analyze_descriptor_access(instance_type: Type, msg.fail(message_registry.DESCRIPTOR_GET_NOT_CALLABLE.format(descriptor_type), context) return AnyType(TypeOfAny.from_error) - function = function_type(dunder_get, builtin_type('builtins.function')) + function = function_type(dunder_get, named_type('builtins.function')) bound_method = bind_self(function, descriptor_type) typ = map_instance_to_supertype(descriptor_type, dunder_get.info) dunder_get_type = expand_type_by_instance(bound_method, typ) @@ -518,7 +518,7 @@ def analyze_descriptor_access(instance_type: Type, def instance_alias_type(alias: TypeAlias, - builtin_type: Callable[[str], Instance]) -> Type: + named_type: Callable[[str], Instance]) -> Type: """Type of a type alias node targeting an instance, when appears in runtime context. As usual, we first erase any unbound type variables to Any. @@ -528,7 +528,7 @@ def instance_alias_type(alias: TypeAlias, Instance), "Must be called only with aliases to classes" target = get_proper_type(set_any_tvars(alias, alias.line, alias.column)) assert isinstance(target, Instance) - tp = type_object_type(target.type, builtin_type) + tp = type_object_type(target.type, named_type) return expand_type_by_instance(tp, target) diff --git a/mypy/plugin.py b/mypy/plugin.py index 6f6d4a8df5b98..c88fba4eb1884 100644 --- a/mypy/plugin.py +++ b/mypy/plugin.py @@ -297,11 +297,6 @@ def class_type(self, self_type: Type) -> Type: """Generate type of first argument of class methods from type of self.""" raise NotImplementedError - @abstractmethod - def builtin_type(self, fully_qualified_name: str) -> Instance: - """Deprecated: use named_type instead.""" - raise NotImplementedError - @abstractmethod def lookup_fully_qualified(self, name: str) -> SymbolTableNode: """Lookup a symbol by its fully qualified name. diff --git a/mypy/plugins/attrs.py b/mypy/plugins/attrs.py index f6de73f14f982..2bbcd8d2243b8 100644 --- a/mypy/plugins/attrs.py +++ b/mypy/plugins/attrs.py @@ -97,7 +97,7 @@ def argument(self, ctx: 'mypy.plugin.ClassDefContext') -> Argument: converter_type: Optional[Type] = None if converter and isinstance(converter.node, TypeInfo): from mypy.checkmember import type_object_type # To avoid import cycle. - converter_type = type_object_type(converter.node, ctx.api.builtin_type) + converter_type = type_object_type(converter.node, ctx.api.named_type) elif converter and isinstance(converter.node, OverloadedFuncDef): converter_type = converter.node.type elif converter and converter.type: diff --git a/mypy/semanal.py b/mypy/semanal.py index 1c87d9721f429..76985d4ab4583 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -732,7 +732,7 @@ def analyze_overloaded_func_def(self, defn: OverloadedFuncDef) -> None: # This is a property. first_item.func.is_overload = True self.analyze_property_with_multi_part_definition(defn) - typ = function_type(first_item.func, self.builtin_type('builtins.function')) + typ = function_type(first_item.func, self.named_type('builtins.function')) assert isinstance(typ, CallableType) types = [typ] else: @@ -789,7 +789,7 @@ def analyze_overload_sigs_and_impl( item.accept(self) # TODO: support decorated overloaded functions properly if isinstance(item, Decorator): - callable = function_type(item.func, self.builtin_type('builtins.function')) + callable = function_type(item.func, self.named_type('builtins.function')) assert isinstance(callable, CallableType) if not any(refers_to_fullname(dec, 'typing.overload') for dec in item.decorators): @@ -4421,12 +4421,6 @@ def lookup_fully_qualified_or_none(self, fullname: str) -> Optional[SymbolTableN self.record_incomplete_ref() return result - def builtin_type(self, fully_qualified_name: str) -> Instance: - sym = self.lookup_fully_qualified(fully_qualified_name) - node = sym.node - assert isinstance(node, TypeInfo) - return Instance(node, [AnyType(TypeOfAny.special_form)] * len(node.defn.type_vars)) - def object_type(self) -> Instance: return self.named_type('builtins.object') diff --git a/mypy/suggestions.py b/mypy/suggestions.py index 08063cc05451e..87b54814c6373 100644 --- a/mypy/suggestions.py +++ b/mypy/suggestions.py @@ -288,7 +288,7 @@ def get_trivial_type(self, fdef: FuncDef) -> CallableType: fdef.arg_kinds, fdef.arg_names, AnyType(TypeOfAny.suggestion_engine), - self.builtin_type('builtins.function')) + self.named_type('builtins.function')) def get_starting_type(self, fdef: FuncDef) -> CallableType: if isinstance(fdef.type, CallableType): @@ -351,7 +351,7 @@ def get_default_arg_types(self, state: State, fdef: FuncDef) -> List[Optional[Ty def add_adjustments(self, typs: List[Type]) -> List[Type]: if not self.try_text or self.manager.options.python_version[0] != 2: return typs - translator = StrToText(self.builtin_type) + translator = StrToText(self.named_type) return dedup(typs + [tp.accept(translator) for tp in typs]) def get_guesses(self, is_method: bool, base: CallableType, defaults: List[Optional[Type]], @@ -648,8 +648,8 @@ def ensure_loaded(self, state: State, force: bool = False) -> MypyFile: assert state.tree is not None return state.tree - def builtin_type(self, s: str) -> Instance: - return self.manager.semantic_analyzer.builtin_type(s) + def named_type(self, s: str) -> Instance: + return self.manager.semantic_analyzer.named_type(s) def json_suggestion(self, mod: str, func_name: str, node: FuncDef, suggestion: PyAnnotateSignature) -> str: @@ -850,8 +850,8 @@ def visit_callable_type(self, t: CallableType) -> str: class StrToText(TypeTranslator): - def __init__(self, builtin_type: Callable[[str], Instance]) -> None: - self.text_type = builtin_type('builtins.unicode') + def __init__(self, named_type: Callable[[str], Instance]) -> None: + self.text_type = named_type('builtins.unicode') def visit_type_alias_type(self, t: TypeAliasType) -> Type: exp_t = get_proper_type(t) diff --git a/test-data/unit/plugins/common_api_incremental.py b/test-data/unit/plugins/common_api_incremental.py index 070bc61ceb3ff..2dcd559777ec4 100644 --- a/test-data/unit/plugins/common_api_incremental.py +++ b/test-data/unit/plugins/common_api_incremental.py @@ -24,7 +24,7 @@ def add_info_hook(ctx) -> None: info = TypeInfo(SymbolTable(), class_def, ctx.api.cur_mod_id) class_def.info = info - obj = ctx.api.builtin_type('builtins.object') + obj = ctx.api.named_type('builtins.object') info.mro = [info, obj.type] info.bases = [obj] ctx.api.add_symbol_table_node(ctx.name, SymbolTableNode(GDEF, info)) diff --git a/test-data/unit/plugins/dyn_class.py b/test-data/unit/plugins/dyn_class.py index 56ef89e17869e..54bf377aa8ef2 100644 --- a/test-data/unit/plugins/dyn_class.py +++ b/test-data/unit/plugins/dyn_class.py @@ -23,7 +23,7 @@ def add_info_hook(ctx): info = TypeInfo(SymbolTable(), class_def, ctx.api.cur_mod_id) class_def.info = info - obj = ctx.api.builtin_type('builtins.object') + obj = ctx.api.named_type('builtins.object') info.mro = [info, obj.type] info.bases = [obj] ctx.api.add_symbol_table_node(ctx.name, SymbolTableNode(GDEF, info)) diff --git a/test-data/unit/plugins/dyn_class_from_method.py b/test-data/unit/plugins/dyn_class_from_method.py index 8a18f7f1e8e1f..4c3904907750d 100644 --- a/test-data/unit/plugins/dyn_class_from_method.py +++ b/test-data/unit/plugins/dyn_class_from_method.py @@ -18,7 +18,7 @@ def add_info_hook(ctx: DynamicClassDefContext): class_def.info = info queryset_type_fullname = ctx.call.args[0].fullname queryset_info = ctx.api.lookup_fully_qualified_or_none(queryset_type_fullname).node # type: TypeInfo - obj = ctx.api.builtin_type('builtins.object') + obj = ctx.api.named_type('builtins.object') info.mro = [info, queryset_info, obj.type] info.bases = [Instance(queryset_info, [])] ctx.api.add_symbol_table_node(ctx.name, SymbolTableNode(GDEF, info))