8000 Foundations for non-linear solver and polymorphic application by ilevkivskyi · Pull Request #15287 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Foundations for non-linear solver and polymorphic application #15287

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 19 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Some tweaks
  • Loading branch information
ilevkivskyi committed May 22, 2023
commit 4c41c67bfc17baf5cc9cfdd0c8a25a3de4850eb4
11 changes: 6 additions & 5 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5387,7 +5387,7 @@ def replace_callable_return_type(c: CallableType, new_ret_type: Type) -> Callabl
def apply_poly(tp: CallableType, poly_tvars: Sequence[TypeVarLikeType]) -> Optional[CallableType]:
"""Make free type variables generic in the type if possible.

This will analyze the type `tp` while trying to create valid bindings for
This will translate the type `tp` while trying to create valid bindings for
type variables `poly_tvars` while traversing the type. This follows the same rules
as we do during semantic analysis phase, examples:
* Callable[Callable[[T], T], T] -> def [T] (def (T) -> T) -> T
Expand All @@ -5413,6 +5413,7 @@ class PolyTranslator(TypeTranslator):

See docstring for apply_poly() for details.
"""

def __init__(self, poly_tvars: Sequence[TypeVarLikeType]) -> None:
self.poly_tvars = set(poly_tvars)
# This is a simplified version of TypeVarScope used during semantic analysis.
Expand All @@ -5422,14 +5423,14 @@ def __init__(self, poly_tvars: Sequence[TypeVarLikeType]) -> None:
def visit_callable_type(self, t: CallableType) -> Type:
found_vars = set()
for arg in t.arg_types:
found_vars |= set(get_type_vars(arg))
found_vars &= self.poly_tvars
found_vars |= set(get_type_vars(arg)) & self.poly_tvars

found_vars -= self.bound_tvars
self.bound_tvars |= found_vars
result = super().visit_callable_type(t)
self.bound_tvars -= found_vars
assert isinstance(result, ProperType)
assert isinstance(result, CallableType)

assert isinstance(result, ProperType) and isinstance(result, CallableType)
result.variables = list(result.variables) + list(found_vars)
return result

Expand Down
4 changes: 2 additions & 2 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,9 +888,9 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
# FIX verify argument counts
# TODO: Erase template variables if it is generic?
if cactual.variables and cactual.param_spec() is None:
# If template is generic, unify it with template. Note: this is
# If actual is generic, unify it with template. Note: this is
# not an ideal solution (which would be adding the generic variables
# to the constraint inference set), but is a good first approximation,
# to the constraint inference set), but it's a good first approximation,
# and this will prevent leaking these variables in the solutions.
# Note: this may infer constraints like T <: S or T <: List[S]
# that contain variables in the target.
Expand Down
10 changes: 6 additions & 4 deletions mypy/solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ def solve_iteratively(
c.target = expand_type(
c.target, {k: v for (k, v) in tmap.items() if v is not None}
)
# TODO: support backtracking lower/upper bound choices
# (will require switching this function from iterative to recursive).
solutions.update(tmap)
return solutions

Expand Down Expand Up @@ -213,10 +215,10 @@ def normalize_constraints(
) -> list[Constraint]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would nice to have some unit tests for this (not need to do this in this PR).

Copy link
Collaborator

Choose a reason for hiding this comment

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

On reflection, I'm not sure if it's worth having unit tests for all of these functions. It may make more sense to have some unit tests for top-level functions. @jhance has recently written some unit tests that target type inference in mypy/test/testconstraints.py, and I think that they are helpful, even if we are not trying to cover all edge cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

FWIW I think it is a good idea. Of course not all of these really needs to be tested, but for most of them it is a good thing. Especially for e.g. transitive_closure() since logic there is really easy to get wrong. I will however add them in a separate PR.

"""Normalize list of constraints (to simplify life for the non-linear solver).

This includes two things currently:
* Complement T :> S by S <: T
* Remove strict duplicates
"""
This includes two things currently:
* Complement T :> S by S <: T
* Remove strict duplicates
"""
res = constraints.copy()
for c in constraints:
if isinstance(c.target, TypeVarType):
Expand Down
0