Skip to content

Commit 9090192

Browse files
committed
Remove shared state, is_meta_var is sufficient
1 parent 71e4496 commit 9090192

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

mypy/constraints.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@ def infer_constraints_for_callable(
127127
param_spec_arg_names = []
128128
param_spec_arg_kinds = []
129129

130-
own_vars = {t.id for t in callee.variables}
131-
type_state.constraints_targets.append(own_vars)
132-
133130
incomplete_star_mapping = False
134131
for i, actuals in enumerate(formal_to_actual): # TODO: isn't this `enumerate(arg_types)`?
135132
for actual in actuals:
@@ -276,7 +273,6 @@ def infer_constraints_for_callable(
276273
if any(isinstance(v, ParamSpecType) for v in callee.variables):
277274
# As a perf optimization filter imprecise constraints only when we can have them.
278275
constraints = filter_imprecise_kinds(constraints)
279-
type_state.constraints_targets.pop()
280276
return constraints
281277

282278

@@ -559,8 +555,8 @@ def any_constraints(options: list[list[Constraint] | None], *, eager: bool) -> l
559555
if filtered_options != options:
560556
return any_constraints(filtered_options, eager=eager)
561557

562-
# Try harder: if that didn't work, try to strip typevars not owned by current function.
563-
filtered_options = [filter_own(o) for o in options]
558+
# Try harder: if that didn't work, try to strip typevars that aren't meta vars.
559+
filtered_options = [exclude_non_meta_vars(o) for o in options]
564560
if filtered_options != options:
565561
return any_constraints(filtered_options, eager=eager)
566562

@@ -593,14 +589,13 @@ def filter_satisfiable(option: list[Constraint] | None) -> list[Constraint] | No
593589
return satisfiable
594590

595591

596-
def filter_own(option: list[Constraint] | None) -> list[Constraint] | None:
597-
"""Keep only constraints that reference type vars local to current function, if any."""
598-
599-
if not option or not type_state.constraints_targets:
592+
def exclude_non_meta_vars(option: list[Constraint] | None) -> list[Constraint] | None:
593+
# If we had an empty list, keep it intact
594+
if not option:
600595
return option
601-
own_vars = type_state.constraints_targets[-1]
602-
603-
return [c for c in option if c.type_var in own_vars] or None
596+
# However, if none of the options actually references meta vars, better remove
597+
# this constraint entirely.
598+
return [c for c in option if c.type_var.is_meta_var()] or None
604599

605600

606601
def is_same_constraints(x: list[Constraint], y: list[Constraint]) -> bool:

mypy/typestate.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ class TypeState:
9191
_assuming_proper: Final[list[tuple[Type, Type]]]
9292
# Ditto for inference of generic constraints against recursive type aliases.
9393
inferring: Final[list[tuple[Type, Type]]]
94-
# When building constraints for a callable, prefer these variables when we encounter
95-
# ambiguous set in `any_constraints`
96-
constraints_targets: Final[list[set[TypeVarId]]]
9794
# Whether to use joins or unions when solving constraints, see checkexpr.py for details.
9895
infer_unions: bool
9996
# Whether to use new type inference algorithm that can infer polymorphic types.
@@ -115,7 +112,6 @@ def __init__(self) -> None:
115112
self._assuming = []
116113
self._assuming_proper = []
117114
self.inferring = []
118-
self.constraints_targets = []
119115
self.infer_unions = False
120116
self.infer_polymorphic = False
121117

0 commit comments

Comments
 (0)