-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Introduce a helper function to map types to protocols + fix for dict kwargs false positive #20419
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
Open
randolf-scholz
wants to merge
3
commits into
python:master
Choose a base branch
from
randolf-scholz:fix_good_kw
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+102
−32
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,61 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from mypy.expandtype import expand_type_by_instance | ||
| from typing import cast | ||
|
|
||
| from mypy.expandtype import expand_type, expand_type_by_instance | ||
| from mypy.nodes import TypeInfo | ||
| from mypy.types import AnyType, Instance, TupleType, TypeOfAny, has_type_vars | ||
| from mypy.types import AnyType, Instance, TupleType, Type, TypeOfAny, has_type_vars | ||
|
|
||
|
|
||
| def as_type(typ: Type, direction: int, target: Type) -> Type | None: | ||
| """Attempts to solve type variables in `target` so that `typ` is a subtype/supertype of | ||
| the resulting type. | ||
| Args: | ||
| typ: The type to map from. | ||
| direction: One of SUBTYPE_OF or SUPERTYPE_OF | ||
| target: The target instance type to map to. | ||
| Returns: | ||
| None: if the mapping is not possible. | ||
| Type: the mapped type if the mapping is possible. | ||
| Examples: | ||
| (list[int], Iterable[T]) -> Iterable[int] | ||
| (list[list[int]], Iterable[list[T]]) -> Iterable[list[int]] | ||
| (dict[str, int], Mapping[K, int]) -> Mapping[str, int] | ||
| (list[int], Mapping[K, V]) -> None | ||
| """ | ||
| from mypy.subtypes import is_subtype | ||
| from mypy.typeops import get_all_type_vars | ||
|
|
||
| # 1. get type vars of target | ||
| tvars = get_all_type_vars(target) | ||
|
|
||
| # fast path: if no type vars, just check subtype | ||
| if not tvars: | ||
| return target if is_subtype(typ, target) else None | ||
|
|
||
| from mypy.constraints import SUBTYPE_OF, Constraint, infer_constraints | ||
| from mypy.solve import solve_constraints | ||
|
|
||
| # 2. determine constraints | ||
| constraints: list[Constraint] = infer_constraints(target, typ, not direction) | ||
| for tvar in tvars: | ||
| # need to manually include these because solve_constraints ignores them | ||
| # apparently | ||
| constraints.append(Constraint(tvar, SUBTYPE_OF, tvar.upper_bound)) | ||
|
Comment on lines
+45
to
+47
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it intentional that |
||
|
|
||
| # 3. solve constraints | ||
| solution, _ = solve_constraints(tvars, constraints) | ||
|
|
||
| if None in solution: | ||
| return None | ||
|
|
||
| # 4. build resulting Type by substituting type vars with solution | ||
| env = {tvar.id: s for tvar, s in zip(tvars, cast("list[Type]", solution))} | ||
| target = expand_type(target, env) | ||
| return target if is_subtype(typ, target) else None | ||
|
|
||
|
|
||
| def map_instance_to_supertype(instance: Instance, superclass: TypeInfo) -> Instance: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
had to put these there due to circular import issues
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should definitely not be in this module (ideally we shouldn't even have the existing typeops import below, but that is a separate story).
Also the scope of this function is misleadingly broad. It should probably accept a
TypeInfoastarget(which probably must be a protocol), and then usefill_typevars(...)as a constraint inference target.Finally, it is worth doing some performance measurements, we don't want any visible slow-down for something that is only needed for rare edge cases.