From 39f00041c94749372183280df3a4f9f44adecd34 Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Sun, 22 Nov 2026 16:30:14 +0100 Subject: [PATCH] Use types.FunctionType instead of builtins.function --- mypy/checker.py | 40 +++++++--- mypy/checkexpr.py | 18 ++--- mypy/checkmember.py | 6 +- mypy/checkpattern.py | 2 +- mypy/join.py | 9 ++- mypy/meet.py | 6 +- mypy/messages.py | 2 +- mypy/plugins/common.py | 4 +- mypy/semanal.py | 78 ++++++++++++++++--- mypy/semanal_infer.py | 6 +- mypy/semanal_main.py | 1 + mypy/semanal_namedtuple.py | 2 +- mypy/semanal_newtype.py | 2 +- mypy/stubtest.py | 6 +- mypy/suggestions.py | 2 +- mypy/test/teststubtest.py | 2 - mypy/test/typefixture.py | 2 +- mypy/typeanal.py | 6 +- mypy/typeops.py | 2 +- mypy/types.py | 4 +- mypyc/test-data/fixtures/ir.py | 1 - test-data/unit/check-dynamic-typing.test | 6 +- test-data/unit/check-flags.test | 14 ++-- test-data/unit/check-functions.test | 6 +- test-data/unit/check-generics.test | 3 +- test-data/unit/check-inference.test | 2 +- test-data/unit/check-literal.test | 4 +- .../unit/check-parameter-specification.test | 2 +- test-data/unit/check-protocols.test | 4 +- test-data/unit/check-tuples.test | 3 +- test-data/unit/exportjson.test | 2 +- test-data/unit/fine-grained-inspect.test | 4 +- test-data/unit/fixtures/__init_subclass__.pyi | 1 - test-data/unit/fixtures/__new__.pyi | 1 - test-data/unit/fixtures/alias.pyi | 1 - test-data/unit/fixtures/args.pyi | 1 - test-data/unit/fixtures/async_await.pyi | 1 - test-data/unit/fixtures/bool.pyi | 1 - test-data/unit/fixtures/callable.pyi | 1 - test-data/unit/fixtures/classmethod.pyi | 2 - test-data/unit/fixtures/complex.pyi | 2 - test-data/unit/fixtures/complex_tuple.pyi | 1 - test-data/unit/fixtures/dataclasses.pyi | 1 - test-data/unit/fixtures/dict-full.pyi | 1 - test-data/unit/fixtures/dict.pyi | 1 - test-data/unit/fixtures/divmod.pyi | 1 - test-data/unit/fixtures/exception.pyi | 1 - test-data/unit/fixtures/f_string.pyi | 1 - test-data/unit/fixtures/fine_grained.pyi | 1 - test-data/unit/fixtures/float.pyi | 1 - test-data/unit/fixtures/floatdict.pyi | 1 - test-data/unit/fixtures/for.pyi | 1 - test-data/unit/fixtures/function.pyi | 1 - test-data/unit/fixtures/isinstance.pyi | 2 - .../unit/fixtures/isinstance_python3_10.pyi | 2 - test-data/unit/fixtures/isinstancelist.pyi | 1 - test-data/unit/fixtures/len.pyi | 2 - test-data/unit/fixtures/list.pyi | 1 - test-data/unit/fixtures/module.pyi | 1 - test-data/unit/fixtures/module_all.pyi | 1 - test-data/unit/fixtures/narrowing.pyi | 1 - test-data/unit/fixtures/notimplemented.pyi | 1 - .../fixtures/object_with_init_subclass.pyi | 1 - test-data/unit/fixtures/ops.pyi | 2 - test-data/unit/fixtures/paramspec.pyi | 1 - test-data/unit/fixtures/plugin_attrs.pyi | 1 - test-data/unit/fixtures/primitives.pyi | 1 - test-data/unit/fixtures/property.pyi | 2 - test-data/unit/fixtures/set.pyi | 1 - test-data/unit/fixtures/slice.pyi | 2 +- test-data/unit/fixtures/staticmethod.pyi | 2 - test-data/unit/fixtures/tuple-simple.pyi | 1 - test-data/unit/fixtures/tuple.pyi | 2 - test-data/unit/fixtures/type.pyi | 1 - test-data/unit/fixtures/union.pyi | 1 - test-data/unit/lib-stub/builtins.pyi | 2 - test-data/unit/lib-stub/types.pyi | 3 + test-data/unit/plugins/type_anal_hook.py | 2 +- test-data/unit/typexport-basic.test | 6 +- 79 files changed, 174 insertions(+), 143 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index ea7adc757d1d3..b2181a8449ab8 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -215,7 +215,13 @@ def __init__(self) -> None: from mypy.plugin import Plugin from mypy.plugins import dataclasses as dataclasses_plugin from mypy.scope import Scope -from mypy.semanal import is_trivial_body, refers_to_fullname, set_callable_name +from mypy.semanal import ( + CORE_TYPES_CLASSES, + is_trivial_body, + make_placeholder_types_info, + refers_to_fullname, + set_callable_name, +) from mypy.semanal_enum import ENUM_BASES, ENUM_SPECIAL_PROPS from mypy.semanal_shared import SemanticAnalyzerCoreInterface from mypy.sharedparse import BINARY_MAGIC_METHODS @@ -882,7 +888,7 @@ def _visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None: arg_kinds=[ARG_POS, ARG_POS], arg_names=[None, None], ret_type=any_type, - fallback=self.named_type("builtins.function"), + fallback=self.named_type("types.FunctionType"), ) if setter_type and not is_subtype(setter_type, fallback_setter_type): self.fail("Invalid property setter signature", defn.setter.func) @@ -951,7 +957,7 @@ def _visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None: arg_kinds=[ARG_POS], arg_names=[None], ret_type=any_type, - fallback=self.named_type("builtins.function"), + fallback=self.named_type("types.FunctionType"), ) defn.type = Overloaded([var_type]) # Check override validity after we analyzed current definition. @@ -2022,7 +2028,7 @@ def check_reverse_op_method( [nodes.ARG_POS, nodes.ARG_POS], [None, None], AnyType(TypeOfAny.special_form), - self.named_type("builtins.function"), + self.named_type("types.FunctionType"), ) if not is_subtype(reverse_type, method_type): self.msg.invalid_signature(reverse_type, context) @@ -2248,7 +2254,7 @@ def check_getattr_method(self, typ: Type, context: Context, name: str) -> None: [nodes.ARG_POS], [None], AnyType(TypeOfAny.special_form), - self.named_type("builtins.function"), + self.named_type("types.FunctionType"), ) elif self.scope.active_class(): method_type = CallableType( @@ -2256,7 +2262,7 @@ def check_getattr_method(self, typ: Type, context: Context, name: str) -> None: [nodes.ARG_POS, nodes.ARG_POS], [None, None], AnyType(TypeOfAny.special_form), - self.named_type("builtins.function"), + self.named_type("types.FunctionType"), ) else: return @@ -2275,7 +2281,7 @@ def check_setattr_method(self, typ: Type, context: Context) -> None: [nodes.ARG_POS, nodes.ARG_POS, nodes.ARG_POS], [None, None, None], NoneType(), - self.named_type("builtins.function"), + self.named_type("types.FunctionType"), ) if not is_subtype(typ, method_type): self.msg.invalid_signature_for_special_method(typ, context, "__setattr__") @@ -6446,7 +6452,7 @@ def intersect_instance_callable(self, typ: Instance, callable_type: CallableType def make_fake_callable(self, typ: Instance) -> Instance: """Produce a new type that makes type Callable with a generic callable type.""" - fallback = self.named_type("builtins.function") + fallback = self.named_type("types.FunctionType") callable_type = CallableType( [AnyType(TypeOfAny.explicit), AnyType(TypeOfAny.explicit)], [nodes.ARG_STAR, nodes.ARG_STAR2], @@ -7977,7 +7983,7 @@ def named_type(self, name: str) -> Instance: if instance_cache.str_type is None: instance_cache.str_type = self._named_type(name) return instance_cache.str_type - if name == "builtins.function": + if name == "types.FunctionType": if instance_cache.function_type is None: instance_cache.function_type = self._named_type(name) return instance_cache.function_type @@ -8085,6 +8091,20 @@ def lookup_qualified(self, name: str) -> SymbolTableNode: return self.lookup(name) else: parts = name.split(".") + if ( + parts[0] == "types" + and parts[-1] in CORE_TYPES_CLASSES + and (parts[0] not in self.modules or parts[-1] not in self.modules[parts[0]].names) + ): + # The 'types' module may not be loaded (e.g. in tests using + # minimal builtins fixtures), but core types classes must be + # resolvable. + return SymbolTableNode( + GDEF, + make_placeholder_types_info( + parts[-1], lambda: self.named_type("builtins.object") + ), + ) n = self.modules[parts[0]] for i in range(1, len(parts) - 1): sym = n.names.get(parts[i]) @@ -8259,7 +8279,7 @@ def iterable_item_type(self, it: ProperType, context: Context) -> Type: return self.analyze_iterable_item_type_without_expression(it, context)[1] def function_type(self, func: FuncBase) -> FunctionLike: - typ = function_type(func, self.named_type("builtins.function")) + typ = function_type(func, self.named_type("types.FunctionType")) if ( isinstance(func, FuncItem) and func.is_coroutine diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index c48f3da51f85e..ed73f6ace944a 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -593,7 +593,7 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> e.callee.arg_kinds, e.callee.arg_names, ret_type=self.object_type(), - fallback=self.named_type("builtins.function"), + fallback=self.named_type("types.FunctionType"), ) callee_type = get_proper_type( self.accept(e.callee, type_context, always_allow_any=True, is_callee=True) @@ -2053,7 +2053,7 @@ def analyze_type_type_callee(self, item: ProperType, context: Context) -> Type: arg_kinds=[], arg_names=[], ret_type=NoneType(), - fallback=self.named_type("builtins.function"), + fallback=self.named_type("types.FunctionType"), name=None, from_type_type=True, ) @@ -3983,7 +3983,7 @@ def visit_comparison_expr(self, e: ComparisonExpr) -> Type: [nodes.ARG_POS], [None], self.bool_type(), - self.named_type("builtins.function"), + self.named_type("types.FunctionType"), ) e.method_types.append(method_type) iterable_types.append(itertype) @@ -5475,7 +5475,7 @@ def check_lst_expr(self, e: ListExpr | SetExpr | TupleExpr, fullname: str, tag: [nodes.ARG_STAR], [None], self.chk.named_generic_type(fullname, [tv]), - self.named_type("builtins.function"), + self.named_type("types.FunctionType"), name=tag, variables=[tv], ) @@ -5767,7 +5767,7 @@ def _visit_dict_expr(self, e: DictExpr) -> Type: [nodes.ARG_POS] * len(expected_types), [None] * len(expected_types), self.chk.named_generic_type("builtins.dict", [kt, vt]), - self.named_type("builtins.function"), + self.named_type("types.FunctionType"), name="", variables=[kt, vt], ) @@ -5842,7 +5842,7 @@ def visit_lambda_expr(self, e: LambdaExpr) -> Type: # Only type check the return expression, not the return statement. # There's no useful type context. ret_type = self.accept(e.expr(), allow_none_return=True) - fallback = self.named_type("builtins.function") + fallback = self.named_type("types.FunctionType") self.chk.return_types.pop() self.in_lambda_expr = old_in_lambda return callable_type(e, fallback, ret_type) @@ -5905,7 +5905,7 @@ def infer_lambda_type_using_context( # The callable_ctx may have a fallback of builtins.type if the context # is a constructor -- but this fallback doesn't make sense for lambdas. - callable_ctx = callable_ctx.copy_modified(fallback=self.named_type("builtins.function")) + callable_ctx = callable_ctx.copy_modified(fallback=self.named_type("types.FunctionType")) if callable_ctx.type_guard is not None or callable_ctx.type_is is not None: # Lambda's return type cannot be treated as a `TypeGuard`, @@ -6185,7 +6185,7 @@ def check_generator_or_comprehension( [nodes.ARG_POS], [None], self.chk.named_generic_type(type_name, tv_list + additional_args), - self.chk.named_type("builtins.function"), + self.chk.named_type("types.FunctionType"), name=id_for_messages, variables=[tv], ) @@ -6240,7 +6240,7 @@ def visit_dictionary_comprehension(self, e: DictionaryComprehension) -> Type: arg_kinds, arg_names, self.chk.named_generic_type("builtins.dict", [ktdef, vtdef]), - self.chk.named_type("builtins.function"), + self.chk.named_type("types.FunctionType"), name="", variables=[ktdef, vtdef], ) diff --git a/mypy/checkmember.py b/mypy/checkmember.py index e01d1e6a85790..768c237193048 100644 --- a/mypy/checkmember.py +++ b/mypy/checkmember.py @@ -522,7 +522,7 @@ def analyze_none_member_access(name: str, typ: NoneType, mx: MemberContext) -> T arg_kinds=[], arg_names=[], ret_type=literal_false, - fallback=mx.named_type("builtins.function"), + fallback=mx.named_type("types.FunctionType"), ) else: return _analyze_member_access(name, mx.named_type("builtins.object"), mx) @@ -1401,7 +1401,7 @@ def analyze_typeddict_access( arg_kinds=[ARG_POS, ARG_POS], arg_names=[None, None], ret_type=NoneType(), - fallback=mx.chk.named_type("builtins.function"), + fallback=mx.chk.named_type("types.FunctionType"), name=name, ) elif name == "__delitem__": @@ -1410,7 +1410,7 @@ def analyze_typeddict_access( arg_kinds=[ARG_POS], arg_names=[None], ret_type=NoneType(), - fallback=mx.chk.named_type("builtins.function"), + fallback=mx.chk.named_type("types.FunctionType"), name=name, ) return _analyze_member_access(name, typ.fallback, mx, override_info) diff --git a/mypy/checkpattern.py b/mypy/checkpattern.py index 0e4786910e116..b1442b56cd25d 100644 --- a/mypy/checkpattern.py +++ b/mypy/checkpattern.py @@ -733,7 +733,7 @@ def get_class_pattern_type_ranges(self, typ: Type, o: ClassPattern) -> list[Type and o.class_ref.node.fullname == "typing.Callable" ): # Create a `Callable[..., Any]` - fallback = self.chk.named_type("builtins.function") + fallback = self.chk.named_type("types.FunctionType") any_type = AnyType(TypeOfAny.unannotated) typ = callable_with_ellipsis(any_type, ret_type=any_type, fallback=fallback) return [TypeRange(typ, is_upper_bound=False)] diff --git a/mypy/join.py b/mypy/join.py index 9d777835fa481..bbb6b1e9c2112 100644 --- a/mypy/join.py +++ b/mypy/join.py @@ -836,11 +836,12 @@ def join_similar_callables(t: CallableType, s: CallableType) -> CallableType: arg_types.append(safe_meet(t.arg_types[i], s.arg_types[i])) # TODO Handle more kinds and different argument names. # TODO what should happen if one fallback is 'type' and the other is a custom metaclass? - # The fallback type can be either 'function', 'type', or some user-provided metaclass. - # The result should always use 'function' as a fallback if either operands are using it. - if t.fallback.type.fullname == "builtins.function": + # The fallback type can be either 'types.FunctionType', 'type', or some user-provided + # metaclass. The result should always use 'types.FunctionType' as a fallback if either + # operand is using it. + if t.fallback.type.fullname == "types.FunctionType": fallback = t.fallback - elif s.fallback.type.fullname == "builtins.function": + elif s.fallback.type.fullname == "types.FunctionType": fallback = s.fallback else: fallback = InstanceJoiner().join_instances(t.fallback, s.fallback) diff --git a/mypy/meet.py b/mypy/meet.py index 0ca4dfabc73cb..f608fc4c91a34 100644 --- a/mypy/meet.py +++ b/mypy/meet.py @@ -1246,9 +1246,9 @@ def meet_similar_callables(t: CallableType, s: CallableType) -> CallableType: for i in range(len(t.arg_types)): arg_types.append(safe_join(t.arg_types[i], s.arg_types[i])) # TODOs in join_similar_callables() also applies here. - # The fallback type can be either 'function' or 'type'. The result should have 'function' as - # fallback only if both operands have it as 'function'. - if t.fallback.type.fullname != "builtins.function": + # The fallback type can be either 'types.FunctionType' or 'type'. The result should have + # 'types.FunctionType' as fallback only if both operands have it as 'types.FunctionType'. + if t.fallback.type.fullname != "types.FunctionType": fallback = t.fallback else: fallback = s.fallback diff --git a/mypy/messages.py b/mypy/messages.py index 1ef5dea68e2b5..6a53086f835ec 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -427,7 +427,7 @@ def has_no_attr( return codes.INDEX elif member == "__call__": if isinstance(original_type, Instance) and ( - original_type.type.fullname == "builtins.function" + original_type.type.fullname == "types.FunctionType" ): # "'function' not callable" is a confusing error message. # Explain that the problem is that the type of the function is not known. diff --git a/mypy/plugins/common.py b/mypy/plugins/common.py index 16ccdb4184299..4ab77f996d9f8 100644 --- a/mypy/plugins/common.py +++ b/mypy/plugins/common.py @@ -337,9 +337,9 @@ def _add_method_by_spec( ), "Can't add a new method that's both staticmethod and classmethod." if isinstance(api, SemanticAnalyzerPluginInterface): - function_type = api.named_type("builtins.function") + function_type = api.named_type("types.FunctionType") else: - function_type = api.named_generic_type("builtins.function", []) + function_type = api.named_generic_type("types.FunctionType", []) if is_classmethod: self_type = self_type or TypeType(fill_typevars(info)) diff --git a/mypy/semanal.py b/mypy/semanal.py index 0cedce64a68b9..1b8059ee8875e 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -346,7 +346,24 @@ # Special cased built-in classes that are needed for basic functionality and need to be # available very early on. -CORE_BUILTIN_CLASSES: Final = ["object", "bool", "function"] +CORE_BUILTIN_CLASSES: Final = ["object", "bool"] +CORE_TYPES_CLASSES: Final = ["FunctionType"] + + +def make_placeholder_types_info( + name: str, obj_type: Callable[[], Instance] | None = None +) -> TypeInfo: + """Create a placeholder TypeInfo for a core class of the 'types' module. + + The placeholders will be replaced by the real definitions if the 'types' + module is loaded, but they are needed to make the core classes resolvable + even when it isn't (e.g. in tests using minimal builtins fixtures). + """ + cdef = ClassDef(name, Block([])) # Dummy ClassDef, will be replaced later + info = TypeInfo(SymbolTable(), cdef, "types") + info._fullname = f"types.{name}" + calculate_mro(info, obj_type) + return info # Python has several different scope/namespace kinds with subtly different semantics. @@ -505,6 +522,9 @@ def __init__( self.plugin = plugin self.recurse_into_functions = True self.scope = Scope() + # Cache of placeholder symbols for core 'types' classes that may not be + # available, so that repeated lookups return the same symbol. + self._placeholder_types_symbols: dict[str, SymbolTableNode] = {} # Trace line numbers for every file where deferral happened during analysis of # current SCC or top-level function. @@ -617,6 +637,8 @@ def prepare_file(self, file_node: MypyFile) -> None: file_node.names["__builtins__"] = SymbolTableNode(GDEF, self.modules["builtins"]) if file_node.fullname == "builtins": self.prepare_builtins_namespace(file_node) + if file_node.fullname == "types": + self.prepare_types_namespace(file_node) if file_node.fullname == "typing": self.prepare_typing_namespace(file_node, type_aliases) if file_node.fullname == "typing_extensions": @@ -691,6 +713,33 @@ def prepare_builtins_namespace(self, file_node: MypyFile) -> None: v._fullname = f"builtins.{name}" file_node.names[name] = SymbolTableNode(GDEF, v) + def prepare_types_namespace(self, file_node: MypyFile) -> None: + """Add certain special-cased definitions to the types module. + + Similar to special definitions from the builtins module. + """ + # Add empty definition for core 'types' classes, since they are required for basic + # operation. These will be completed later on. + for name in CORE_TYPES_CLASSES: + file_node.names[name] = SymbolTableNode( + GDEF, make_placeholder_types_info(name, self.object_type) + ) + + def placeholder_types_symbol(self, name: str) -> SymbolTableNode: + """Return a placeholder symbol for a core class of the 'types' module. + + The 'types' module may not be loaded (e.g. in tests using minimal + builtins fixtures), but core types classes such as 'types.FunctionType' + must be resolvable for basic functionality. The placeholders are not + added to the module table, so they don't interfere with incremental + caching. + """ + if name not in self._placeholder_types_symbols: + self._placeholder_types_symbols[name] = SymbolTableNode( + GDEF, make_placeholder_types_info(name, self.object_type) + ) + return self._placeholder_types_symbols[name] + # # Analyzing a target # @@ -1991,7 +2040,7 @@ def pop_type_args(self, type_args: list[TypeParam] | None) -> None: def analyze_class(self, defn: ClassDef) -> None: fullname = self.qualified_name(defn.name) - if not defn.info and not self.is_core_builtin_class(defn): + if not defn.info and not self.is_core_class(defn): # Add placeholder so that self-references in base classes can be # resolved. We don't want this to cause a deferral, since if there # are no incomplete references, we'll replace this with a TypeInfo @@ -2141,8 +2190,10 @@ def setup_alias_type_vars(self, defn: ClassDef) -> None: else: assert False, f"Unexpected special alias type: {type(target)}" - def is_core_builtin_class(self, defn: ClassDef) -> bool: - return self.cur_mod_id == "builtins" and defn.name in CORE_BUILTIN_CLASSES + def is_core_class(self, defn: ClassDef) -> bool: + return (self.cur_mod_id == "builtins" and defn.name in CORE_BUILTIN_CLASSES) or ( + self.cur_mod_id == "types" and defn.name in CORE_TYPES_CLASSES + ) def analyze_class_body_common(self, defn: ClassDef) -> None: """Parts of class body analysis that are common to all kinds of class defs.""" @@ -2581,12 +2632,8 @@ def prepare_class_def(self, defn: ClassDef, info: TypeInfo | None = None) -> Non self.add_global_symbol(defn.name, defn, defn.info) def make_empty_type_info(self, defn: ClassDef) -> TypeInfo: - if ( - self.is_module_scope() - and self.cur_mod_id == "builtins" - and defn.name in CORE_BUILTIN_CLASSES - ): - # Special case core built-in classes. A TypeInfo was already + if self.is_module_scope() and self.is_core_class(defn): + # Special case core classes. A TypeInfo was already # created for it before semantic analysis, but with a dummy # ClassDef. Patch the real ClassDef object. info = self.globals[defn.name].node @@ -7061,7 +7108,14 @@ def lookup_fully_qualified_or_none(self, fullname: str) -> SymbolTableNode | Non """ # TODO: unify/clean-up/simplify lookup methods, see #4157. module, name = fullname.rsplit(".", maxsplit=1) - + if ( + module == "types" + and name in CORE_TYPES_CLASSES + and (module not in self.modules or name not in self.modules[module].names) + ): + # The 'types' module may not be loaded (e.g. in tests using minimal + # builtins fixtures), but core types classes must be resolvable. + return self.placeholder_types_symbol(name) if module in self.modules: # If the module exists, look up the name in the module. # This is the common case. @@ -7113,7 +7167,7 @@ def str_type(self) -> Instance: def function_type(self) -> Instance: if self._function_type is None: - self._function_type = self.named_type("builtins.function") + self._function_type = self.named_type("types.FunctionType") return self._function_type def named_type(self, fullname: str, args: list[Type] | None = None) -> Instance: diff --git a/mypy/semanal_infer.py b/mypy/semanal_infer.py index abe9bf18c45e9..0fe1d7bb95d78 100644 --- a/mypy/semanal_infer.py +++ b/mypy/semanal_infer.py @@ -39,7 +39,7 @@ def infer_decorator_signature_if_simple( [ARG_POS], [None], AnyType(TypeOfAny.special_form), - analyzer.named_type("builtins.function"), + analyzer.named_type("types.FunctionType"), name=dec.var.name, ) elif isinstance(dec.func.type, CallableType): @@ -59,7 +59,7 @@ def infer_decorator_signature_if_simple( if decorator_preserves_type: # No non-identity decorators left. We can trivially infer the type # of the function here. - sig = function_type(dec.func, analyzer.named_type("builtins.function")) + sig = function_type(dec.func, analyzer.named_type("types.FunctionType")) dec.var.type = set_callable_name(sig, dec.func) if dec.decorators: return_type = calculate_return_type(dec.decorators[0]) @@ -71,7 +71,7 @@ def infer_decorator_signature_if_simple( if sig: # The outermost decorator always returns the same kind of function, # so we know that this is the type of the decorated function. - orig_sig = function_type(dec.func, analyzer.named_type("builtins.function")) + orig_sig = function_type(dec.func, analyzer.named_type("types.FunctionType")) sig.name = orig_sig.items[0].name dec.var.type = sig if isinstance(sig, CallableType): diff --git a/mypy/semanal_main.py b/mypy/semanal_main.py index 77db7925841ae..d1a2d8158e627 100644 --- a/mypy/semanal_main.py +++ b/mypy/semanal_main.py @@ -72,6 +72,7 @@ "typing", "_collections_abc", "builtins", + "types", "abc", "collections", "collections.abc", diff --git a/mypy/semanal_namedtuple.py b/mypy/semanal_namedtuple.py index 6244cb8150907..b4a227facb76d 100644 --- a/mypy/semanal_namedtuple.py +++ b/mypy/semanal_namedtuple.py @@ -473,7 +473,7 @@ def build_namedtuple_typeinfo( # Note: actual signature should accept an invariant version of Iterable[UnionType[types]]. # but it can't be expressed. 'new' and 'len' should be callable types. iterable_type = self.api.named_type_or_none("typing.Iterable", [implicit_any]) - function_type = self.api.named_type("builtins.function") + function_type = self.api.named_type("types.FunctionType") literals: list[Type] = [LiteralType(item, strtype) for item in items] match_args_type = TupleType(literals, basetuple_type) diff --git a/mypy/semanal_newtype.py b/mypy/semanal_newtype.py index 1833cf82582e5..1d617eef6c7c1 100644 --- a/mypy/semanal_newtype.py +++ b/mypy/semanal_newtype.py @@ -244,7 +244,7 @@ def build_newtype_typeinfo( arg_kinds=[arg.kind for arg in args], arg_names=["self", "item"], ret_type=NoneType(), - fallback=self.api.named_type("builtins.function"), + fallback=self.api.named_type("types.FunctionType"), name=name, ) init_func = FuncDef("__init__", args, Block([]), typ=signature) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 3014878614947..4bb993c673c0b 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -2149,9 +2149,9 @@ def anytype() -> mypy.types.AnyType: runtime, (types.FunctionType, types.BuiltinFunctionType, types.MethodType, types.BuiltinMethodType), ): - builtins = get_stub("builtins") - assert builtins is not None - type_info = builtins.names["function"].node + types_mod = get_stub("types") + assert types_mod is not None + type_info = types_mod.names["FunctionType"].node assert isinstance(type_info, nodes.TypeInfo) fallback = mypy.types.Instance(type_info, [anytype()]) signature = safe_inspect_signature(runtime) diff --git a/mypy/suggestions.py b/mypy/suggestions.py index 6a66f8345408d..06ee05991d1f8 100644 --- a/mypy/suggestions.py +++ b/mypy/suggestions.py @@ -333,7 +333,7 @@ def get_trivial_type(self, fdef: FuncDef) -> CallableType: fdef.arg_kinds, fdef.arg_names, AnyType(TypeOfAny.suggestion_engine), - self.named_type("builtins.function"), + self.named_type("types.FunctionType"), ) def get_starting_type(self, fdef: FuncDef) -> CallableType: diff --git a/mypy/test/teststubtest.py b/mypy/test/teststubtest.py index d3a1889567eb1..700dc908b8187 100644 --- a/mypy/test/teststubtest.py +++ b/mypy/test/teststubtest.py @@ -121,8 +121,6 @@ class dict(Mapping[KT, VT]): ... class frozenset(Generic[T]): ... -class function: pass - class sentinel: __name__: str __module__: str diff --git a/mypy/test/typefixture.py b/mypy/test/typefixture.py index f70c8b94f09c5..3c0b103c97a88 100644 --- a/mypy/test/typefixture.py +++ b/mypy/test/typefixture.py @@ -99,7 +99,7 @@ def make_type_var( self.type_typei = self.make_type_info("builtins.type") # class type self.bool_type_info = self.make_type_info("builtins.bool") self.str_type_info = self.make_type_info("builtins.str") - self.functioni = self.make_type_info("builtins.function") # function TODO + self.functioni = self.make_type_info("types.FunctionType") # function TODO self.ai = self.make_type_info("A", mro=[self.oi]) # class A self.bi = self.make_type_info("B", mro=[self.ai, self.oi]) # class B(A) self.ci = self.make_type_info("C", mro=[self.ai, self.oi]) # class C(A) diff --git a/mypy/typeanal.py b/mypy/typeanal.py index bd6f97cc94cef..3574ba04e995d 100644 --- a/mypy/typeanal.py +++ b/mypy/typeanal.py @@ -1277,7 +1277,9 @@ def visit_callable_type( ret_type=self.anal_type(t.ret_type, nested=nested), # If the fallback isn't filled in yet, # its type will be the falsey FakeInfo - fallback=(t.fallback if t.fallback.type else self.named_type("builtins.function")), + fallback=( + t.fallback if t.fallback.type else self.named_type("types.FunctionType") + ), variables=self.anal_var_defs(variables), type_guard=type_guard, type_is=type_is, @@ -1635,7 +1637,7 @@ def analyze_callable_args_for_concatenate( ) def analyze_callable_type(self, t: UnboundType) -> Type: - fallback = self.named_type("builtins.function") + fallback = self.named_type("types.FunctionType") if len(t.args) == 0: # Callable (bare). Treat as Callable[..., Any]. any_type = self.get_omitted_any(t) diff --git a/mypy/typeops.py b/mypy/typeops.py index 8c53d250b576a..116594f27c994 100644 --- a/mypy/typeops.py +++ b/mypy/typeops.py @@ -214,7 +214,7 @@ def type_object_type( any_type = AnyType(TypeOfAny.special_form) if instance_cache.function_type is None: function_typeinfo = lookup_stdlib_typeinfo( - "builtins.function", modules_state.modules + "types.FunctionType", modules_state.modules ) instance_cache.function_type = Instance(function_typeinfo, []) sig = CallableType( diff --git a/mypy/types.py b/mypy/types.py index f46e7b5491611..8181df187c4cc 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -1776,7 +1776,7 @@ def write(self, data: WriteBuffer) -> None: type_ref = self.type.fullname if type_ref == "builtins.str": write_tag(data, INSTANCE_STR) - elif type_ref == "builtins.function": + elif type_ref == "types.FunctionType": write_tag(data, INSTANCE_FUNCTION) elif type_ref == "builtins.int": write_tag(data, INSTANCE_INT) @@ -1811,7 +1811,7 @@ def read(cls, data: ReadBuffer) -> Instance: if tag == INSTANCE_FUNCTION: if instance_cache.function_type is None: instance_cache.function_type = Instance(NOT_READY, []) - instance_cache.function_type.type_ref = "builtins.function" + instance_cache.function_type.type_ref = "types.FunctionType" return instance_cache.function_type if tag == INSTANCE_INT: if instance_cache.int_type is None: diff --git a/mypyc/test-data/fixtures/ir.py b/mypyc/test-data/fixtures/ir.py index 86d5baa1a983a..e0b9dfd117b53 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -270,7 +270,6 @@ def __add__(self, value: Tuple[_T, ...], /) -> Tuple[T_co | _T, ...]: ... def __mul__(self, value: int, /) -> Tuple[T_co, ...]: ... def __rmul__(self, value: int, /) -> Tuple[T_co, ...]: ... -class function: pass class list(Generic[_T], Sequence[_T], Iterable[_T]): def __init__(self, i: Optional[Iterable[_T]] = None) -> None: pass diff --git a/test-data/unit/check-dynamic-typing.test b/test-data/unit/check-dynamic-typing.test index eeaa69affb78a..5b9a3124c2050 100644 --- a/test-data/unit/check-dynamic-typing.test +++ b/test-data/unit/check-dynamic-typing.test @@ -148,9 +148,10 @@ class object: class bool: pass class int: pass class type: pass -class function: pass class str: pass class dict: pass +[file types.py] +class FunctionType: pass [case testBinaryOperationsWithDynamicAsRightOperand] from typing import Any @@ -221,9 +222,10 @@ class object: class bool: pass class int: pass class type: pass -class function: pass class str: pass class dict: pass +[file types.py] +class FunctionType: pass [case testDynamicWithUnaryExpressions] from typing import Any diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index d33acacb2853f..8e9982a873bca 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -160,11 +160,11 @@ from typing import Callable class TypedDecorator: def __call__(self, c: Callable) -> Callable: - return function + return c class UntypedDecorator: def __call__(self, c): - return function + return c @TypedDecorator() def f() -> None: pass @@ -200,29 +200,29 @@ def untyped_decorator(c): class TypedDecorator: @typed_decorator def __call__(self, c: Callable) -> Callable: - return function + return c class UntypedDecorator1: @untyped_decorator def __call__(self, c): - return function + return c class UntypedDecorator2: @untyped_decorator # E: Untyped decorator makes function "__call__" untyped def __call__(self, c: Callable) -> Callable: - return function + return c class UntypedDecorator3: @typed_decorator @untyped_decorator # E: Untyped decorator makes function "__call__" untyped def __call__(self, c: Callable) -> Callable: - return function + return c class UntypedDecorator4: @untyped_decorator # E: Untyped decorator makes function "__call__" untyped @typed_decorator def __call__(self, c: Callable) -> Callable: - return function + return c @TypedDecorator() def f() -> None: pass diff --git a/test-data/unit/check-functions.test b/test-data/unit/check-functions.test index 321a0afebd5ac..e84f50d49a0af 100644 --- a/test-data/unit/check-functions.test +++ b/test-data/unit/check-functions.test @@ -2331,11 +2331,11 @@ T = TypeVar("T") def join(x: T, y: T) -> T: ... h2 = join(f, g) -reveal_type(h2) # N: Revealed type is "builtins.function" +reveal_type(h2) # N: Revealed type is "types.FunctionType" h2(7) # E: Cannot call function of unknown type h3 = join(g, f) -reveal_type(h3) # N: Revealed type is "builtins.function" +reveal_type(h3) # N: Revealed type is "types.FunctionType" h3(7) # E: Cannot call function of unknown type [builtins fixtures/bool.pyi] @@ -3560,7 +3560,7 @@ def f3(i: str = "") -> None: ... reveal_type(join(f1, f2)) # N: Revealed type is "def ()" reveal_type(join(f1, f3)) # N: Revealed type is "def ()" -reveal_type(join(f2, f3)) # N: Revealed type is "builtins.function" # TODO: this could be better +reveal_type(join(f2, f3)) # N: Revealed type is "types.FunctionType" # TODO: this could be better [builtins fixtures/tuple.pyi] [case testCallableJoinWithDefaultsMultiple] diff --git a/test-data/unit/check-generics.test b/test-data/unit/check-generics.test index 63282a979462e..9d34f7427ded8 100644 --- a/test-data/unit/check-generics.test +++ b/test-data/unit/check-generics.test @@ -1343,9 +1343,10 @@ class list(Iterable[T]): class int: pass class type: pass class tuple: pass -class function: pass class str: pass class dict: pass +[file types.py] +class FunctionType: pass [case testMultipleAssignmentWithIterable] from typing import Iterable, TypeVar diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 08a70d1a6efef..3bf2bbf1f430c 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -1110,7 +1110,7 @@ def g(*, y: int) -> int: ... def h(*, x: int) -> int: ... list_1 = [f, g] -reveal_type(list_1) # N: Revealed type is "builtins.list[builtins.function]" +reveal_type(list_1) # N: Revealed type is "builtins.list[types.FunctionType]" list_2 = [f, h] reveal_type(list_2) # N: Revealed type is "builtins.list[def (*, x: builtins.int) -> builtins.int]" [builtins fixtures/list.pyi] diff --git a/test-data/unit/check-literal.test b/test-data/unit/check-literal.test index b54db1f33fa5e..b521b2fc34bc8 100644 --- a/test-data/unit/check-literal.test +++ b/test-data/unit/check-literal.test @@ -1756,7 +1756,7 @@ arr4 = [a, d] arr5 = [a, e] reveal_type(arr1) # N: Revealed type is "builtins.list[def (Literal[1]) -> builtins.int]" -reveal_type(arr2) # N: Revealed type is "builtins.list[builtins.function]" +reveal_type(arr2) # N: Revealed type is "builtins.list[types.FunctionType]" reveal_type(arr3) # N: Revealed type is "builtins.list[def (Literal[1]) -> builtins.object]" reveal_type(arr4) # N: Revealed type is "builtins.list[def (Literal[1]) -> builtins.object]" reveal_type(arr5) # N: Revealed type is "builtins.list[def (Literal[1]) -> builtins.object]" @@ -1794,7 +1794,7 @@ b: Callable[[Literal[2]], str] lit: Literal[1] arr = [a, b] -reveal_type(arr) # N: Revealed type is "builtins.list[builtins.function]" +reveal_type(arr) # N: Revealed type is "builtins.list[types.FunctionType]" reveal_type(arr[0](lit)) # E: Cannot call function of unknown type \ # N: Revealed type is "Any" diff --git a/test-data/unit/check-parameter-specification.test b/test-data/unit/check-parameter-specification.test index 18e16179854fb..793493b174eb8 100644 --- a/test-data/unit/check-parameter-specification.test +++ b/test-data/unit/check-parameter-specification.test @@ -384,7 +384,7 @@ def join(x: T, y: T) -> T: ... class C(Generic[P, P2]): def m(self, f: Callable[P, None], g: Callable[P2, None]) -> None: reveal_type(join(f, f)) # N: Revealed type is "def (*P.args, **P.kwargs)" - reveal_type(join(f, g)) # N: Revealed type is "builtins.function" + reveal_type(join(f, g)) # N: Revealed type is "types.FunctionType" def m2(self, *args: P.args, **kwargs: P.kwargs) -> None: reveal_type(join(args, args)) # N: Revealed type is "P.args`1" diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index 4c0fdd55b1224..b2f7447e70441 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -2708,12 +2708,12 @@ def f() -> str: ... reveal_type(f.__name__) # N: Revealed type is "builtins.str" a: A = f # OK b1: B1 = f # E: Incompatible types in assignment (expression has type "Callable[[], str]", variable has type "B1") \ - # N: Following member(s) of "function" have conflicts: \ + # N: Following member(s) of "FunctionType" have conflicts: \ # N: __name__: expected "int", got "str" b2: B2 = f # E: Incompatible types in assignment (expression has type "Callable[[], str]", variable has type "B2") \ # N: "B2.__call__" has type "Callable[[], int]" b3: B3 = f # E: Incompatible types in assignment (expression has type "Callable[[], str]", variable has type "B3") \ - # N: "function" is missing following "B3" protocol member: \ + # N: "FunctionType" is missing following "B3" protocol member: \ # N: extra_stuff [case testCallbackProtocolFunctionAttributesInference] diff --git a/test-data/unit/check-tuples.test b/test-data/unit/check-tuples.test index 6d523d375dd2a..ede9185e9b1a6 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -966,8 +966,9 @@ class int: pass class str: pass class bool: pass class type: pass -class function: pass class dict: pass +[file types.py] +class FunctionType: pass -- For loop over tuple diff --git a/test-data/unit/exportjson.test b/test-data/unit/exportjson.test index f1f977c6ea2ca..82ffe5bbc5ea6 100644 --- a/test-data/unit/exportjson.test +++ b/test-data/unit/exportjson.test @@ -164,7 +164,7 @@ def foo(a: int) -> None: ... "ret_type": { ".class": "NoneType" }, - "fallback": "builtins.function", + "fallback": "types.FunctionType", "name": "foo", "variables": [], "is_ellipsis_args": false, diff --git a/test-data/unit/fine-grained-inspect.test b/test-data/unit/fine-grained-inspect.test index 5caa1a94387b1..f90b4ae21f954 100644 --- a/test-data/unit/fine-grained-inspect.test +++ b/test-data/unit/fine-grained-inspect.test @@ -52,8 +52,8 @@ class Meta(type): == {"C": ["meth", "x"]} {"C": ["meth", "x"], "Meta": ["y"], "type": ["__init__"]} -{"function": ["__name__"]} -{"function": ["__name__"], "object": ["__init__"]} +{"FunctionType": ["__name__"]} +{"FunctionType": ["__name__"], "object": ["__init__"]} [case testInspectDefBasic] # inspect2: --show=definition tmp/foo.py:5:5 diff --git a/test-data/unit/fixtures/__init_subclass__.pyi b/test-data/unit/fixtures/__init_subclass__.pyi index b4618c28249e6..96e9509bf0110 100644 --- a/test-data/unit/fixtures/__init_subclass__.pyi +++ b/test-data/unit/fixtures/__init_subclass__.pyi @@ -10,5 +10,4 @@ class type: pass class int: pass class bool: pass class str: pass -class function: pass class dict: pass diff --git a/test-data/unit/fixtures/__new__.pyi b/test-data/unit/fixtures/__new__.pyi index 57d3624ce92cf..53138d6633acf 100644 --- a/test-data/unit/fixtures/__new__.pyi +++ b/test-data/unit/fixtures/__new__.pyi @@ -16,5 +16,4 @@ class float: pass class int: pass class bool: pass class str: pass -class function: pass class dict: pass diff --git a/test-data/unit/fixtures/alias.pyi b/test-data/unit/fixtures/alias.pyi index 2ec7703f00c4d..39dc8e468f305 100644 --- a/test-data/unit/fixtures/alias.pyi +++ b/test-data/unit/fixtures/alias.pyi @@ -9,7 +9,6 @@ class type: class int: pass class str: pass -class function: pass bytes = str diff --git a/test-data/unit/fixtures/args.pyi b/test-data/unit/fixtures/args.pyi index 724a9f530062c..a1fbfddd6cba5 100644 --- a/test-data/unit/fixtures/args.pyi +++ b/test-data/unit/fixtures/args.pyi @@ -32,4 +32,3 @@ class float: pass class str: pass class bytes: pass class bool: pass -class function: pass diff --git a/test-data/unit/fixtures/async_await.pyi b/test-data/unit/fixtures/async_await.pyi index 5076dcaee4157..ebf3934a93b7f 100644 --- a/test-data/unit/fixtures/async_await.pyi +++ b/test-data/unit/fixtures/async_await.pyi @@ -11,7 +11,6 @@ class list(typing.Sequence[T]): class object: def __init__(self) -> None: pass class type: pass -class function: pass class int: pass class float: pass class str: pass diff --git a/test-data/unit/fixtures/bool.pyi b/test-data/unit/fixtures/bool.pyi index 80ec071f99eb3..66da47540935b 100644 --- a/test-data/unit/fixtures/bool.pyi +++ b/test-data/unit/fixtures/bool.pyi @@ -10,7 +10,6 @@ class object: class type: pass class tuple(Generic[T]): pass -class function: pass class int: pass class bool(int): pass class float: pass diff --git a/test-data/unit/fixtures/callable.pyi b/test-data/unit/fixtures/callable.pyi index 2b1a5a155e754..632205f625b9f 100644 --- a/test-data/unit/fixtures/callable.pyi +++ b/test-data/unit/fixtures/callable.pyi @@ -13,7 +13,6 @@ class tuple(Generic[T]): pass class classmethod: pass class staticmethod: pass -class function: pass def isinstance(x: object, t: Union[type, Tuple[type, ...]]) -> bool: pass diff --git a/test-data/unit/fixtures/classmethod.pyi b/test-data/unit/fixtures/classmethod.pyi index 7f72574bc70c9..b8f06d89aba65 100644 --- a/test-data/unit/fixtures/classmethod.pyi +++ b/test-data/unit/fixtures/classmethod.pyi @@ -10,8 +10,6 @@ class type: def __init__(self, x) -> None: pass def mro(self) -> typing.Any: pass -class function: pass - # Dummy definitions. class classmethod: pass class staticmethod: pass diff --git a/test-data/unit/fixtures/complex.pyi b/test-data/unit/fixtures/complex.pyi index 880ec3dd4d9d0..0c30abfcf1d79 100644 --- a/test-data/unit/fixtures/complex.pyi +++ b/test-data/unit/fixtures/complex.pyi @@ -1,11 +1,9 @@ # Builtins stub used for some float/complex test cases. # Please don't add tuple to this file, it is used to test incomplete fixtures. - class object: def __init__(self): pass class type: pass -class function: pass class int: pass class float: pass class complex: pass diff --git a/test-data/unit/fixtures/complex_tuple.pyi b/test-data/unit/fixtures/complex_tuple.pyi index 8e3358df0adfd..ab7ec1dc3311e 100644 --- a/test-data/unit/fixtures/complex_tuple.pyi +++ b/test-data/unit/fixtures/complex_tuple.pyi @@ -8,7 +8,6 @@ class object: class tuple(Generic[_T]): pass class type: pass -class function: pass class int: pass class float: pass class complex: pass diff --git a/test-data/unit/fixtures/dataclasses.pyi b/test-data/unit/fixtures/dataclasses.pyi index 3f570b410ddfa..2c5841ce43f6e 100644 --- a/test-data/unit/fixtures/dataclasses.pyi +++ b/test-data/unit/fixtures/dataclasses.pyi @@ -52,7 +52,6 @@ class list(Generic[_T], Sequence[_T]): @override def __iter__(self) -> Iterator[_T]: pass -class function: pass class classmethod: pass class staticmethod: pass property = object() diff --git a/test-data/unit/fixtures/dict-full.pyi b/test-data/unit/fixtures/dict-full.pyi index 6cc32055fb87f..8d6fba77c311e 100644 --- a/test-data/unit/fixtures/dict-full.pyi +++ b/test-data/unit/fixtures/dict-full.pyi @@ -74,7 +74,6 @@ class list(Sequence[T]): # needed by some test cases def append(self, item: T) -> None: pass class tuple(Generic[T]): pass -class function: pass class float: pass class complex: pass class bool(int): pass diff --git a/test-data/unit/fixtures/dict.pyi b/test-data/unit/fixtures/dict.pyi index 63698a89281c3..bf38ee8493288 100644 --- a/test-data/unit/fixtures/dict.pyi +++ b/test-data/unit/fixtures/dict.pyi @@ -56,7 +56,6 @@ class list(Sequence[T]): # needed by some test cases def append(self, item: T) -> None: pass class tuple(Generic[T]): pass -class function: pass class float: pass class complex: pass class bool(int): pass diff --git a/test-data/unit/fixtures/divmod.pyi b/test-data/unit/fixtures/divmod.pyi index 124e318b7f1e2..0ff33dedd7599 100644 --- a/test-data/unit/fixtures/divmod.pyi +++ b/test-data/unit/fixtures/divmod.pyi @@ -13,7 +13,6 @@ class float(SupportsInt): class tuple: pass -class function: pass class str: pass class type: pass diff --git a/test-data/unit/fixtures/exception.pyi b/test-data/unit/fixtures/exception.pyi index ebc1e632b64c4..a6902a807c39d 100644 --- a/test-data/unit/fixtures/exception.pyi +++ b/test-data/unit/fixtures/exception.pyi @@ -11,7 +11,6 @@ class tuple(Generic[T]): def __ge__(self, other: object) -> bool: ... class list: pass class dict: pass -class function: pass class int: pass class float: pass class str: pass diff --git a/test-data/unit/fixtures/f_string.pyi b/test-data/unit/fixtures/f_string.pyi index 1fb9c1b2d1355..082bafa8058ee 100644 --- a/test-data/unit/fixtures/f_string.pyi +++ b/test-data/unit/fixtures/f_string.pyi @@ -22,7 +22,6 @@ class list(Iterable[T], Generic[T]): class tuple(Generic[T]): pass -class function: pass class int: def __add__(self, i: int) -> int: pass diff --git a/test-data/unit/fixtures/fine_grained.pyi b/test-data/unit/fixtures/fine_grained.pyi index 46701f469786f..5401a881b1cb8 100644 --- a/test-data/unit/fixtures/fine_grained.pyi +++ b/test-data/unit/fixtures/fine_grained.pyi @@ -24,6 +24,5 @@ class str: class float: pass class bytes: pass class tuple(Generic[T]): pass -class function: pass class list(Generic[T]): pass class dict: pass diff --git a/test-data/unit/fixtures/float.pyi b/test-data/unit/fixtures/float.pyi index 7231b61fdbfc1..424a7afd30ddb 100644 --- a/test-data/unit/fixtures/float.pyi +++ b/test-data/unit/fixtures/float.pyi @@ -15,7 +15,6 @@ class str: class bytes: pass class tuple(Generic[T]): pass -class function: pass class int: def __abs__(self) -> int: ... diff --git a/test-data/unit/fixtures/floatdict.pyi b/test-data/unit/fixtures/floatdict.pyi index b635ee5fed8a2..cac61b33ef33e 100644 --- a/test-data/unit/fixtures/floatdict.pyi +++ b/test-data/unit/fixtures/floatdict.pyi @@ -19,7 +19,6 @@ class bytes: pass class tuple(Generic[T]): pass class slice: pass -class function: pass class list(Iterable[T], Generic[T]): diff --git a/test-data/unit/fixtures/for.pyi b/test-data/unit/fixtures/for.pyi index e4410ee3129e9..55e0950b2d10d 100644 --- a/test-data/unit/fixtures/for.pyi +++ b/test-data/unit/fixtures/for.pyi @@ -12,7 +12,6 @@ class object: class type: pass class tuple(Generic[t]): def __iter__(self) -> Iterator[t]: pass -class function: pass class bool: pass class int: pass # for convenience class float: pass # for convenience diff --git a/test-data/unit/fixtures/function.pyi b/test-data/unit/fixtures/function.pyi index 697d0d919d982..2d95fd4e0812b 100644 --- a/test-data/unit/fixtures/function.pyi +++ b/test-data/unit/fixtures/function.pyi @@ -2,7 +2,6 @@ class object: def __init__(self): pass class type: pass -class function: pass class int: pass class str: pass class dict: pass diff --git a/test-data/unit/fixtures/isinstance.pyi b/test-data/unit/fixtures/isinstance.pyi index 0f55a1ca0bc2b..ac4472d0e7221 100644 --- a/test-data/unit/fixtures/isinstance.pyi +++ b/test-data/unit/fixtures/isinstance.pyi @@ -12,8 +12,6 @@ class type: class tuple(Generic[T]): pass -class function: pass - def isinstance(x: object, t: Union[Type[object], Tuple[Type[object], ...]]) -> bool: pass def issubclass(x: object, t: Union[Type[object], Tuple[Type[object], ...]]) -> bool: pass def hasattr(x: object, name: str) -> bool: pass diff --git a/test-data/unit/fixtures/isinstance_python3_10.pyi b/test-data/unit/fixtures/isinstance_python3_10.pyi index de8e933ce884d..68d46bcc09543 100644 --- a/test-data/unit/fixtures/isinstance_python3_10.pyi +++ b/test-data/unit/fixtures/isinstance_python3_10.pyi @@ -13,8 +13,6 @@ class type: class tuple(Generic[T]): pass -class function: pass - def isinstance(x: object, t: Union[Type[object], Tuple[Type[object], ...], types.UnionType]) -> bool: pass def issubclass(x: object, t: Union[Type[object], Tuple[Type[object], ...]]) -> bool: pass diff --git a/test-data/unit/fixtures/isinstancelist.pyi b/test-data/unit/fixtures/isinstancelist.pyi index 9577bdd3119f9..51ecfc6d3985e 100644 --- a/test-data/unit/fixtures/isinstancelist.pyi +++ b/test-data/unit/fixtures/isinstancelist.pyi @@ -10,7 +10,6 @@ class object: class type: def __init__(self, x) -> None: pass -class function: pass class classmethod: pass Ellipsis: types.EllipsisType diff --git a/test-data/unit/fixtures/len.pyi b/test-data/unit/fixtures/len.pyi index 805cd877444d9..b66d5c20ff0b5 100644 --- a/test-data/unit/fixtures/len.pyi +++ b/test-data/unit/fixtures/len.pyi @@ -17,8 +17,6 @@ class tuple(Sequence[T]): class list(Sequence[T]): pass class dict(Mapping[T, V]): pass -class function: pass - class Sized(Protocol): def __len__(self) -> int: pass diff --git a/test-data/unit/fixtures/list.pyi b/test-data/unit/fixtures/list.pyi index 10eb7e29fe89e..46005eca335da 100644 --- a/test-data/unit/fixtures/list.pyi +++ b/test-data/unit/fixtures/list.pyi @@ -27,7 +27,6 @@ class list(Sequence[T]): def extend(self, x: Iterable[T]) -> None: pass class tuple(Generic[T]): pass -class function: pass class int: def __bool__(self) -> bool: pass class float: diff --git a/test-data/unit/fixtures/module.pyi b/test-data/unit/fixtures/module.pyi index a50aedac54b4e..2ef42486d3d45 100644 --- a/test-data/unit/fixtures/module.pyi +++ b/test-data/unit/fixtures/module.pyi @@ -9,7 +9,6 @@ class list(Generic[T], Sequence[T]): pass # type: ignore class object: def __init__(self) -> None: pass class type: pass -class function: pass class int: pass class float: pass class str: pass diff --git a/test-data/unit/fixtures/module_all.pyi b/test-data/unit/fixtures/module_all.pyi index fb96f31c912d8..76401dcdc20c1 100644 --- a/test-data/unit/fixtures/module_all.pyi +++ b/test-data/unit/fixtures/module_all.pyi @@ -6,7 +6,6 @@ _T = TypeVar('_T') class object: def __init__(self) -> None: pass class type: pass -class function: pass class int: pass class str: pass class bool: pass diff --git a/test-data/unit/fixtures/narrowing.pyi b/test-data/unit/fixtures/narrowing.pyi index 5f0c46663358f..75eb22ae99d9e 100644 --- a/test-data/unit/fixtures/narrowing.pyi +++ b/test-data/unit/fixtures/narrowing.pyi @@ -12,7 +12,6 @@ class object: class type: pass class tuple(Sequence[Tco], Generic[Tco]): pass -class function: pass class int: pass class str: pass class float: pass diff --git a/test-data/unit/fixtures/notimplemented.pyi b/test-data/unit/fixtures/notimplemented.pyi index 5f5f6d2033761..904cfd056d387 100644 --- a/test-data/unit/fixtures/notimplemented.pyi +++ b/test-data/unit/fixtures/notimplemented.pyi @@ -4,7 +4,6 @@ class object: def __init__(self) -> None: pass class type: pass -class function: pass class bool: pass class int: pass class str: pass diff --git a/test-data/unit/fixtures/object_with_init_subclass.pyi b/test-data/unit/fixtures/object_with_init_subclass.pyi index c30b0883e8d6c..daa8e99f2e193 100644 --- a/test-data/unit/fixtures/object_with_init_subclass.pyi +++ b/test-data/unit/fixtures/object_with_init_subclass.pyi @@ -33,7 +33,6 @@ class bytes(Sequence[int]): def __getitem__(self, item: int) -> int: pass class bytearray: pass class tuple(Generic[T]): pass -class function: pass # copy-pasted from list.pyi class list(Sequence[T]): diff --git a/test-data/unit/fixtures/ops.pyi b/test-data/unit/fixtures/ops.pyi index ec38ae740b277..c8a327570efd0 100644 --- a/test-data/unit/fixtures/ops.pyi +++ b/test-data/unit/fixtures/ops.pyi @@ -23,8 +23,6 @@ class tuple(Sequence[Tco]): def __gt__(self, x: Tuple[Tco, ...]) -> bool: pass def __ge__(self, x: Tuple[Tco, ...]) -> bool: pass -class function: pass - class str: def __init__(self, x: 'int' = ...) -> None: pass def __add__(self, x: 'str') -> 'str': pass diff --git a/test-data/unit/fixtures/paramspec.pyi b/test-data/unit/fixtures/paramspec.pyi index a21f1ce1bda3e..1b5a83d2af468 100644 --- a/test-data/unit/fixtures/paramspec.pyi +++ b/test-data/unit/fixtures/paramspec.pyi @@ -15,7 +15,6 @@ VT = TypeVar("VT") class object: def __init__(self) -> None: ... -class function: ... class classmethod: ... class type: diff --git a/test-data/unit/fixtures/plugin_attrs.pyi b/test-data/unit/fixtures/plugin_attrs.pyi index 169738a720ff0..1d67ca2a5159e 100644 --- a/test-data/unit/fixtures/plugin_attrs.pyi +++ b/test-data/unit/fixtures/plugin_attrs.pyi @@ -10,7 +10,6 @@ class object: class type: pass class bytes: pass -class function: pass class float: pass class int: @overload diff --git a/test-data/unit/fixtures/primitives.pyi b/test-data/unit/fixtures/primitives.pyi index a6c94b96bb591..af50d08d38cc2 100644 --- a/test-data/unit/fixtures/primitives.pyi +++ b/test-data/unit/fixtures/primitives.pyi @@ -69,7 +69,6 @@ class set(Iterable[T]): def __contains__(self, o: object, /) -> bool: pass class frozenset(Iterable[T]): def __iter__(self) -> Iterator[T]: pass -class function: pass class range(Sequence[int]): def __init__(self, __x: int, __y: int = ..., __z: int = ...) -> None: pass diff --git a/test-data/unit/fixtures/property.pyi b/test-data/unit/fixtures/property.pyi index 78cccc566c60f..c3ead9e04d232 100644 --- a/test-data/unit/fixtures/property.pyi +++ b/test-data/unit/fixtures/property.pyi @@ -9,8 +9,6 @@ class object: class type: def __init__(self, x: typing.Any) -> None: pass -class function: pass - property = object() # Dummy definition class classmethod: pass diff --git a/test-data/unit/fixtures/set.pyi b/test-data/unit/fixtures/set.pyi index 50b5afe488ce1..f70c776c7685e 100644 --- a/test-data/unit/fixtures/set.pyi +++ b/test-data/unit/fixtures/set.pyi @@ -11,7 +11,6 @@ class object: class type: pass class tuple(Generic[T]): pass -class function: pass class int: pass class float: pass diff --git a/test-data/unit/fixtures/slice.pyi b/test-data/unit/fixtures/slice.pyi index aa376cec1a876..95d7b1a15b15b 100644 --- a/test-data/unit/fixtures/slice.pyi +++ b/test-data/unit/fixtures/slice.pyi @@ -1,6 +1,7 @@ # Builtins stub used in slicing test cases. import types from typing import Generic, TypeVar, Protocol + T = TypeVar('T') _Tco = TypeVar('_Tco', covariant=True) @@ -12,7 +13,6 @@ class object: class type: pass class tuple(Generic[T]): pass -class function: pass class int: pass class str: pass diff --git a/test-data/unit/fixtures/staticmethod.pyi b/test-data/unit/fixtures/staticmethod.pyi index d1bfc7f03ebbd..cd01d64d3f6fd 100644 --- a/test-data/unit/fixtures/staticmethod.pyi +++ b/test-data/unit/fixtures/staticmethod.pyi @@ -7,8 +7,6 @@ class object: class type: def __init__(self, x) -> None: pass -class function: pass - staticmethod = object() # Dummy definition. property = object() # Dummy definition diff --git a/test-data/unit/fixtures/tuple-simple.pyi b/test-data/unit/fixtures/tuple-simple.pyi index 07f9edf63cdd0..83eed94ede433 100644 --- a/test-data/unit/fixtures/tuple-simple.pyi +++ b/test-data/unit/fixtures/tuple-simple.pyi @@ -13,7 +13,6 @@ class object: class type: pass class tuple(Generic[T]): def __getitem__(self, x: int) -> T: pass -class function: pass # We need int for indexing tuples. class int: pass diff --git a/test-data/unit/fixtures/tuple.pyi b/test-data/unit/fixtures/tuple.pyi index e2bd03b736bca..920808d5fcffb 100644 --- a/test-data/unit/fixtures/tuple.pyi +++ b/test-data/unit/fixtures/tuple.pyi @@ -28,8 +28,6 @@ class tuple(Sequence[_Tco], Generic[_Tco]): def __rmul__(self, n: int) -> Tuple[_Tco, ...]: pass def __add__(self, x: Tuple[_Tco, ...]) -> Tuple[_Tco, ...]: pass def count(self, obj: object) -> int: pass -class function: - __name__: str class classmethod: pass # We need int and slice for indexing tuples. diff --git a/test-data/unit/fixtures/type.pyi b/test-data/unit/fixtures/type.pyi index 752cfda484511..b00b0dc0b95cd 100644 --- a/test-data/unit/fixtures/type.pyi +++ b/test-data/unit/fixtures/type.pyi @@ -22,7 +22,6 @@ class type: class tuple(Generic[T]): pass class dict(Generic[T, S]): pass -class function: pass class bool: pass class int: pass class str: pass diff --git a/test-data/unit/fixtures/union.pyi b/test-data/unit/fixtures/union.pyi index 350e145a6f8f9..80c756e51aa29 100644 --- a/test-data/unit/fixtures/union.pyi +++ b/test-data/unit/fixtures/union.pyi @@ -8,7 +8,6 @@ class object: def __init__(self): pass class type: pass -class function: pass class tuple(Generic[T]): pass diff --git a/test-data/unit/lib-stub/builtins.pyi b/test-data/unit/lib-stub/builtins.pyi index eea54878568f8..49b1f1fec2565 100644 --- a/test-data/unit/lib-stub/builtins.pyi +++ b/test-data/unit/lib-stub/builtins.pyi @@ -20,8 +20,6 @@ class float: pass class str: pass class bytes: pass -class function: - __name__: str from typing import Generic, Iterator, Sequence, TypeVar _T = TypeVar('_T') diff --git a/test-data/unit/lib-stub/types.pyi b/test-data/unit/lib-stub/types.pyi index 02f7785f827bc..f5fa88949faa1 100644 --- a/test-data/unit/lib-stub/types.pyi +++ b/test-data/unit/lib-stub/types.pyi @@ -21,3 +21,6 @@ class UnionType: class NotImplementedType: ... class EllipsisType: pass + +class FunctionType: + __name__: str diff --git a/test-data/unit/plugins/type_anal_hook.py b/test-data/unit/plugins/type_anal_hook.py index c380bbe873fe1..6cbc8d4845bd4 100644 --- a/test-data/unit/plugins/type_anal_hook.py +++ b/test-data/unit/plugins/type_anal_hook.py @@ -29,7 +29,7 @@ def signal_type_analyze_callback(ctx: AnalyzeTypeContext) -> Type: arg_types, arg_kinds, arg_names = analyzed arg_types = [ctx.api.analyze_type(arg) for arg in arg_types] type_arg = CallableType( - arg_types, arg_kinds, arg_names, NoneTyp(), ctx.api.named_type("builtins.function", []) + arg_types, arg_kinds, arg_names, NoneTyp(), ctx.api.named_type("types.FunctionType", []) ) return ctx.api.named_type("m.Signal", [type_arg]) diff --git a/test-data/unit/typexport-basic.test b/test-data/unit/typexport-basic.test index 0852f3a39f4c2..9f2b99bd79ede 100644 --- a/test-data/unit/typexport-basic.test +++ b/test-data/unit/typexport-basic.test @@ -130,7 +130,6 @@ a = 1 + 2 [file builtins.py] class object: def __init__(self) -> None: pass -class function: pass class int: def __add__(self, x: int) -> int: pass def __truediv__(self, x: int) -> float: pass @@ -141,6 +140,8 @@ class type: pass class str: pass class list: pass class dict: pass +[file types.py] +class FunctionType: pass [out] OpExpr(3) : builtins.int OpExpr(4) : builtins.float @@ -165,7 +166,6 @@ class int: def __gt__(self, x: int) -> int: pass class bool: pass class type: pass -class function: pass class str: pass class list: pass class dict: pass @@ -176,6 +176,8 @@ ComparisonExpr(5) : builtins.bool ComparisonExpr(6) : builtins.bool ComparisonExpr(7) : builtins.int ComparisonExpr(8) : builtins.object +[file types.py] +class FunctionType: pass [case testBooleanOps] ## OpExpr|UnaryExpr