Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -2248,15 +2254,15 @@ 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(
[AnyType(TypeOfAny.special_form), self.named_type("builtins.str")],
[nodes.ARG_POS, nodes.ARG_POS],
[None, None],
AnyType(TypeOfAny.special_form),
self.named_type("builtins.function"),
self.named_type("types.FunctionType"),
)
else:
return
Expand All @@ -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__")
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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],
)
Expand Down Expand Up @@ -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="<dict>",
variables=[kt, vt],
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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],
)
Expand Down Expand Up @@ -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="<dictionary-comprehension>",
variables=[ktdef, vtdef],
)
Expand Down
6 changes: 3 additions & 3 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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__":
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
9 changes: 5 additions & 4 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions mypy/plugins/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading
Loading