8000 Only consider meta variables in ambiguous "any of" constraints by sterliakov · Pull Request #18986 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Only consider meta variables in ambiguous "any of" constraints #18986

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

Prev Previous commit
Next Next commit
Move this check to a separate state, see anyio failure
  • Loading branch information
sterliakov committed Apr 28, 2025
commit 71e449647c6aaad9bf58885954b137b26c3b3da8
19 changes: 15 additions & 4 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,11 @@ def any_constraints(options: list[list[Constraint] | None], *, eager: bool) -> l
if filtered_options != options:
return any_constraints(filtered_options, eager=eager)

# Try harder: if that didn't work, try to strip typevars not owned by current function.
filtered_options = [filter_own(o) for o in options]
if filtered_options != options:
return any_constraints(filtered_options, eager=eager)

# Otherwise, there are either no valid options or multiple, inconsistent valid
# options. Give up and deduce nothing.
return []
Expand All @@ -574,12 +579,8 @@ def filter_satisfiable(option: list[Constraint] | None) -> list[Constraint] | No
if not option:
return option

own = type_state.constraints_targets[-1] if type_state.constraints_targets else None

satisfiable = []
for c in option:
if own is not None and c.type_var not in own:
continue
if isinstance(c.origin_type_var, TypeVarType) and c.origin_type_var.values:
if any(
mypy.subtypes.is_subtype(c.target, value) for value in c.origin_type_var.values
Expand All @@ -592,6 +593,16 @@ def filter_satisfiable(option: list[Constraint] | None) -> list[Constraint] | No
return satisfiable


def filter_own(option: list[Constraint] | None) -> list[Constraint] | None:
"""Keep only constraints that reference type vars local to current function, if any."""

if not option or not type_state.constraints_targets:
return option
own_vars = type_state.constraints_targets[-1]

return [c for c in option if c.type_var in own_vars] or None


def is_same_constraints(x: list[Constraint], y: list[Constraint]) -> bool:
for c1 in x:
if not any(is_same_constraint(c1, c2) for c2 in y):
Expand Down
0