8000 Use type variable bound when it appears as actual during inference by ilevkivskyi · Pull Request #16178 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Use type variable bound when it appears as actual during inference #16178

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 5 commits into from
Sep 27, 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
Next Next commit
Use type variable bound when it appears as actual during inference
  • Loading branch information
ilevkivskyi committed Sep 25, 2023
commit 1f1074d0c73c6c1215a59b73cbde4c6c47bb0148
5 changes: 5 additions & 0 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ def _infer_constraints(
return handle_recursive_union(template, actual, direction)
return []

if isinstance(actual, TypeVarType) and not actual.id.is_meta_var():
# Unless template is also a type variable (that is handled above), using the upper
# bound for inference will usually give better result for actual that is a type variable.
actual = get_proper_type(actual.upper_bound)

# Remaining cases are handled by ConstraintBuilderVisitor.
return template.accept(ConstraintBuilderVisitor(actual, direction, skip_neg_op))

Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -3686,3 +3686,15 @@ def g(*args: str) -> None: pass
reveal_type(f(g)) # N: Revealed type is "Tuple[Never, Never]" \
# E: Argument 1 to "f" has incompatible type "Callable[[VarArg(str)], None]"; expected "Call[Never]"
[builtins fixtures/list.pyi]

[case testInferenceAgainstTypeVarActualBound]
from typing import Callable, TypeVar

T = TypeVar("T")
S = TypeVar("S")
def test(f: Callable[[T], S]) -> Callable[[T], S]: ...

F = TypeVar("F", bound=Callable[..., object])
def dec(f: F) -> F:
reveal_type(test(f)) # N: Revealed type is "def (Any) -> builtins.object"
return f
0