Skip to content

Optimize bind_self() and deprecation checks #19556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 2, 2025
Merged
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
30 changes: 3 additions & 27 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ def _visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
assert isinstance(item, Decorator)
item_type = self.extract_callable_type(item.var.type, item)
if item_type is not None:
item_type.definition = item
item_types.append(item_type)
if item_types:
defn.type = Overloaded(item_types)
Expand Down Expand Up @@ -4927,17 +4928,7 @@ def visit_operator_assignment_stmt(self, s: OperatorAssignmentStmt) -> None:
inplace, method = infer_operator_assignment_method(lvalue_type, s.op)
if inplace:
# There is __ifoo__, treat as x = x.__ifoo__(y)
rvalue_type, method_type = self.expr_checker.check_op(method, lvalue_type, s.rvalue, s)
if isinstance(inst := get_proper_type(lvalue_type), Instance) and isinstance(
defn := inst.type.get_method(method), OverloadedFuncDef
):
for item in defn.items:
if (
isinstance(item, Decorator)
and isinstance(typ := item.func.type, CallableType)
and (bind_self(typ) == method_type)
):
self.warn_deprecated(item.func, s)
rvalue_type, _ = self.expr_checker.check_op(method, lvalue_type, s.rvalue, s)
if not is_subtype(rvalue_type, lvalue_type):
self.msg.incompatible_operator_assignment(s.op, s)
else:
Expand Down Expand Up @@ -7962,7 +7953,7 @@ def warn_deprecated(self, node: Node | None, context: Context) -> None:
node = node.func
if (
isinstance(node, (FuncDef, OverloadedFuncDef, TypeInfo))
and ((deprecated := node.deprecated) is not None)
and (deprecated := node.deprecated) is not None
and not self.is_typeshed_stub
and not any(
node.fullname == p or node.fullname.startswith(f"{p}.")
Expand All @@ -7972,21 +7963,6 @@ def warn_deprecated(self, node: Node | None, context: Context) -> None:
warn = self.msg.note if self.options.report_deprecated_as_note else self.msg.fail
warn(deprecated, context, code=codes.DEPRECATED)

def warn_deprecated_overload_item(
self, node: Node | None, context: Context, *, target: Type, selftype: Type | None = None
) -> None:
"""Warn if the overload item corresponding to the given callable is deprecated."""
target = get_proper_type(target)
if isinstance(node, OverloadedFuncDef) and isinstance(target, CallableType):
for item in node.items:
if isinstance(item, Decorator) and isinstance(
candidate := item.func.type, CallableType
):
if selftype is not None and not node.is_static:
candidate = bind_self(candidate, selftype)
if candidate == target:
self.warn_deprecated(item.func, context)

# leafs

def visit_pass_stmt(self, o: PassStmt, /) -> None:
Expand Down
6 changes: 0 additions & 6 deletions mypy/checker_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,6 @@ def check_deprecated(self, node: Node | None, context: Context) -> None:
def warn_deprecated(self, node: Node | None, context: Context) -> None:
raise NotImplementedError

@abstractmethod
def warn_deprecated_overload_item(
self, node: Node | None, context: Context, *, target: Type, selftype: Type | None = None
) -> None:
raise NotImplementedError

@abstractmethod
def type_is_iterable(self, type: Type) -> bool:
raise NotImplementedError
Expand Down
27 changes: 7 additions & 20 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@
validate_instance,
)
from mypy.typeops import (
bind_self,
callable_type,
custom_special_method,
erase_to_union_or_bound,
Expand Down Expand Up @@ -1517,15 +1516,6 @@ def check_call_expr_with_callee_type(
object_type=object_type,
)
proper_callee = get_proper_type(callee_type)
if isinstance(e.callee, (NameExpr, MemberExpr)):
node = e.callee.node
if node is None and member is not None and isinstance(object_type, Instance):
if (symbol := object_type.type.get(member)) is not None:
node = symbol.node
self.chk.check_deprecated(node, e)
self.chk.warn_deprecated_overload_item(
node, e, target=callee_type, selftype=object_type
)
if isinstance(e.callee, RefExpr) and isinstance(proper_callee, CallableType):
# Cache it for find_isinstance_check()
if proper_callee.type_guard is not None:
Expand Down Expand Up @@ -2943,6 +2933,8 @@ def infer_overload_return_type(
# check for ambiguity due to 'Any' below.
if not args_contain_any:
self.chk.store_types(m)
if isinstance(infer_type, ProperType) and isinstance(infer_type, CallableType):
self.chk.check_deprecated(infer_type.definition, context)
return ret_type, infer_type
p_infer_type = get_proper_type(infer_type)
if isinstance(p_infer_type, CallableType):
Expand Down Expand Up @@ -2979,6 +2971,11 @@ def infer_overload_return_type(
else:
# Success! No ambiguity; return the first match.
self.chk.store_types(type_maps[0])
inferred_callable = inferred_types[0]
if isinstance(inferred_callable, ProperType) and isinstance(
inferred_callable, CallableType
):
self.chk.check_deprecated(inferred_callable.definition, context)
return return_types[0], inferred_types[0]

def overload_erased_call_targets(
Expand Down Expand Up @@ -4103,16 +4100,6 @@ def lookup_definer(typ: Instance, attr_name: str) -> str | None:
errors.append(local_errors.filtered_errors())
results.append(result)
else:
if isinstance(obj, Instance) and isinstance(
defn := obj.type.get_method(name), OverloadedFuncDef
):
for item in defn.items:
if (
isinstance(item, Decorator)
and isinstance(typ := item.func.type, CallableType)
and bind_self(typ) == result[1]
):
self.chk.check_deprecated(item.func, context)
return result

# We finish invoking above operators and no early return happens. Therefore,
Expand Down
74 changes: 43 additions & 31 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Sequence
from typing import Callable, TypeVar, cast

from mypy import message_registry, state, subtypes
from mypy import message_registry, state
from mypy.checker_shared import TypeCheckerSharedApi
from mypy.erasetype import erase_typevars
from mypy.expandtype import (
Expand All @@ -14,6 +14,7 @@
freshen_all_functions_type_vars,
)
from mypy.maptype import map_instance_to_supertype
from mypy.meet import is_overlapping_types
from mypy.messages import MessageBuilder
from mypy.nodes import (
ARG_POS,
Expand All @@ -39,6 +40,7 @@
is_final_node,
)
from mypy.plugin import AttributeContext
from mypy.subtypes import is_subtype
from mypy.typeops import (
bind_self,
erase_to_bound,
Expand Down Expand Up @@ -745,10 +747,8 @@ def analyze_descriptor_access(descriptor_type: Type, mx: MemberContext) -> Type:
callable_name=callable_name,
)

mx.chk.check_deprecated(dunder_get, mx.context)
mx.chk.warn_deprecated_overload_item(
dunder_get, mx.context, target=inferred_dunder_get_type, selftype=descriptor_type
)
# Search for possible deprecations:
mx.chk.warn_deprecated(dunder_get, mx.context)

inferred_dunder_get_type = get_proper_type(inferred_dunder_get_type)
if isinstance(inferred_dunder_get_type, AnyType):
Expand Down Expand Up @@ -825,10 +825,7 @@ def analyze_descriptor_assign(descriptor_type: Instance, mx: MemberContext) -> T
)

# Search for possible deprecations:
mx.chk.check_deprecated(dunder_set, mx.context)
mx.chk.warn_deprecated_overload_item(
dunder_set, mx.context, target=inferred_dunder_set_type, selftype=descriptor_type
)
mx.chk.warn_deprecated(dunder_set, mx.context)

# In the following cases, a message already will have been recorded in check_call.
if (not isinstance(inferred_dunder_set_type, CallableType)) or (
Expand Down Expand Up @@ -1053,6 +1050,7 @@ def f(self: S) -> T: ...
new_items = []
if is_classmethod:
dispatched_arg_type = TypeType.make_normalized(dispatched_arg_type)
p_dispatched_arg_type = get_proper_type(dispatched_arg_type)

for item in items:
if not item.arg_types or item.arg_kinds[0] not in (ARG_POS, ARG_STAR):
Expand All @@ -1061,28 +1059,42 @@ def f(self: S) -> T: ...
# This is pretty bad, so just return the original signature if
# there is at least one such error.
return functype
else:
selfarg = get_proper_type(item.arg_types[0])
# This matches similar special-casing in bind_self(), see more details there.
self_callable = name == "__call__" and isinstance(selfarg, CallableType)
if self_callable or subtypes.is_subtype(
dispatched_arg_type,
# This level of erasure matches the one in checker.check_func_def(),
# better keep these two checks consistent.
erase_typevars(erase_to_bound(selfarg)),
# This is to work around the fact that erased ParamSpec and TypeVarTuple
# callables are not always compatible with non-erased ones both ways.
always_covariant=any(
not isinstance(tv, TypeVarType) for tv in get_all_type_vars(selfarg)
),
ignore_pos_arg_names=True,
):
new_items.append(item)
elif isinstance(selfarg, ParamSpecType):
# TODO: This is not always right. What's the most reasonable thing to do here?
new_items.append(item)
elif isinstance(selfarg, TypeVarTupleType):
raise NotImplementedError
selfarg = get_proper_type(item.arg_types[0])
if isinstance(selfarg, Instance) and isinstance(p_dispatched_arg_type, Instance):
if selfarg.type is p_dispatched_arg_type.type and selfarg.args:
if not is_overlapping_types(p_dispatched_arg_type, selfarg):
# This special casing is needed since `actual <: erased(template)`
# logic below doesn't always work, and a more correct approach may
# be tricky.
continue
new_items.append(item)

if new_items:
items = new_items
new_items = []

for item in items:
selfarg = get_proper_type(item.arg_types[0])
# This matches similar special-casing in bind_self(), see more details there.
self_callable = name == "__call__" and isinstance(selfarg, CallableType)
if self_callable or is_subtype(
dispatched_arg_type,
# This level of erasure matches the one in checker.check_func_def(),
# better keep these two checks consistent.
erase_typevars(erase_to_bound(selfarg)),
# This is to work around the fact that erased ParamSpec and TypeVarTuple
# callables are not always compatible with non-erased ones both ways.
always_covariant=any(
not isinstance(tv, TypeVarType) for tv in get_all_type_vars(selfarg)
),
ignore_pos_arg_names=True,
):
new_items.append(item)
elif isinstance(selfarg, ParamSpecType):
# TODO: This is not always right. What's the most reasonable thing to do here?
new_items.append(item)
elif isinstance(selfarg, TypeVarTupleType):
raise NotImplementedError
if not new_items:
# Choose first item for the message (it may be not very helpful for overloads).
msg.incompatible_self_argument(
Expand Down
19 changes: 12 additions & 7 deletions mypy/erasetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,29 +145,34 @@ def erase_typevars(t: Type, ids_to_erase: Container[TypeVarId] | None = None) ->
or just the ones in the provided collection.
"""

if ids_to_erase is None:
return t.accept(TypeVarEraser(None, AnyType(TypeOfAny.special_form)))

def erase_id(id: TypeVarId) -> bool:
if ids_to_erase is None:
return True
return id in ids_to_erase

return t.accept(TypeVarEraser(erase_id, AnyType(TypeOfAny.special_form)))


def erase_meta_id(id: TypeVarId) -> bool:
return id.is_meta_var()


def replace_meta_vars(t: Type, target_type: Type) -> Type:
"""Replace unification variables in a type with the target type."""
return t.accept(TypeVarEraser(lambda id: id.is_meta_var(), target_type))
return t.accept(TypeVarEraser(erase_meta_id, target_type))


class TypeVarEraser(TypeTranslator):
"""Implementation of type erasure"""

def __init__(self, erase_id: Callable[[TypeVarId], bool], replacement: Type) -> None:
def __init__(self, erase_id: Callable[[TypeVarId], bool] | None, replacement: Type) -> None:
super().__init__()
self.erase_id = erase_id
self.replacement = replacement

def visit_type_var(self, t: TypeVarType) -> Type:
if self.erase_id(t.id):
if self.erase_id is None or self.erase_id(t.id):
return self.replacement
return t

Expand Down Expand Up @@ -212,12 +217,12 @@ def visit_callable_type(self, t: CallableType) -> Type:
return result

def visit_type_var_tuple(self, t: TypeVarTupleType) -> Type:
if self.erase_id(t.id):
if self.erase_id is None or self.erase_id(t.id):
return t.tuple_fallback.copy_modified(args=[self.replacement])
return t

def visit_param_spec(self, t: ParamSpecType) -> Type:
if self.erase_id(t.id):
if self.erase_id is None or self.erase_id(t.id):
return self.replacement
return t

Expand Down
2 changes: 2 additions & 0 deletions mypy/fixup.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ def visit_func_def(self, func: FuncDef) -> None:
func.info = self.current_info
if func.type is not None:
func.type.accept(self.type_fixer)
if isinstance(func.type, CallableType):
func.type.definition = func

def visit_overloaded_func_def(self, o: OverloadedFuncDef) -> None:
if self.current_info is not None:
Expand Down
24 changes: 12 additions & 12 deletions mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,18 @@ def is_object(t: ProperType) -> bool:
return isinstance(t, Instance) and t.type.fullname == "builtins.object"


def is_none_typevarlike_overlap(t1: ProperType, t2: ProperType) -> bool:
return isinstance(t1, NoneType) and isinstance(t2, TypeVarLikeType)


def is_none_object_overlap(t1: ProperType, t2: ProperType) -> bool:
return (
isinstance(t1, NoneType)
and isinstance(t2, Instance)
and t2.type.fullname == "builtins.object"
)


def is_overlapping_types(
left: Type,
right: Type,
Expand Down Expand Up @@ -382,14 +394,6 @@ def _is_overlapping_types(left: Type, right: Type) -> bool:
):
return True

def is_none_object_overlap(t1: Type, t2: Type) -> bool:
t1, t2 = get_proper_types((t1, t2))
return (
isinstance(t1, NoneType)
and isinstance(t2, Instance)
and t2.type.fullname == "builtins.object"
)

if overlap_for_overloads:
if is_none_object_overlap(left, right) or is_none_object_overlap(right, left):
return False
Expand Down Expand Up @@ -419,10 +423,6 @@ def _is_subtype(left: Type, right: Type) -> bool:
# If both types are singleton variants (and are not TypeVarLikes), we've hit the base case:
# we skip these checks to avoid infinitely recursing.

def is_none_typevarlike_overlap(t1: Type, t2: Type) -> bool:
t1, t2 = get_proper_types((t1, t2))
return isinstance(t1, NoneType) and isinstance(t2, TypeVarLikeType)

if prohibit_none_typevar_overlap:
if is_none_typevarlike_overlap(left, right) or is_none_typevarlike_overlap(right, left):
return False
Expand Down
6 changes: 4 additions & 2 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,14 @@ def is_trivial_self(self) -> bool:
"""
if self._is_trivial_self is not None:
return self._is_trivial_self
for item in self.items:
for i, item in enumerate(self.items):
# Note: bare @property is removed in visit_decorator().
trivial = 1 if i > 0 or not self.is_property else 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be called before SemanticAnalyzer.visit_decorator processes the node?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, there is currently no protection against this (and other typeops-related things) being accessed during semantic analysis (except semanal_typeargs, where it can be, and likely is, accessed). That said, I hope docstring already makes it clear this property should be used for type-checking purposes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, well, this shouldn't be problematic anyway? Even if this check falsely rejects the fast path, the only consequence (not taking potential bugs in fast/slow bind_self into account) is just inefficient check that takes a bit longer, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But maybe it's better to check that explicitly? If there is exactly one decorator and it refers to property fullname, do not reject. (not sure if it's worth the cycles if you are confident enough that is_trivial_self isn't currently accessed prior to removing @property)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if it's worth the cycles if you are confident enough that is_trivial_self isn't currently accessed prior to removing @property

Oh yes, there is no way it is called that soon now. I was thinking more about the future, but you are right, worst case (if someone does something really weird like calling bind_self() during semantic analysis) we will mark something as non-trivial self, while it is actually trivial.

if isinstance(item, FuncDef):
if not item.is_trivial_self:
self._is_trivial_self = False
return False
elif item.decorators or not item.func.is_trivial_self:
elif len(item.decorators) > trivial or not item.func.is_trivial_self:
self._is_trivial_self = False
return False
self._is_trivial_self = True
Expand Down
Loading