Skip to content
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

Use upper bound as inference fallback more consistently #16344

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,9 @@ def infer_function_type_arguments_using_context(
# in this case external context is almost everything we have.
if not is_generic_instance(ctx) and not is_literal_type_like(ctx):
return callable.copy_modified()
args = infer_type_arguments(callable.variables, ret_type, erased_ctx)
args = infer_type_arguments(
callable.variables, ret_type, erased_ctx, skip_unsatisfied=True
)
# Only substitute non-Uninhabited and non-erased types.
new_args: list[Type | None] = []
for arg in args:
Expand Down
8 changes: 6 additions & 2 deletions mypy/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ def infer_function_type_arguments(


def infer_type_arguments(
type_vars: Sequence[TypeVarLikeType], template: Type, actual: Type, is_supertype: bool = False
type_vars: Sequence[TypeVarLikeType],
template: Type,
actual: Type,
is_supertype: bool = False,
skip_unsatisfied: bool = False,
) -> list[Type | None]:
# Like infer_function_type_arguments, but only match a single type
# against a generic type.
constraints = infer_constraints(template, actual, SUPERTYPE_OF if is_supertype else SUBTYPE_OF)
return solve_constraints(type_vars, constraints)[0]
return solve_constraints(type_vars, constraints, skip_unsatisfied=skip_unsatisfied)[0]
5 changes: 4 additions & 1 deletion mypy/solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def solve_constraints(
constraints: list[Constraint],
strict: bool = True,
allow_polymorphic: bool = False,
skip_unsatisfied: bool = False,
) -> tuple[list[Type | None], list[TypeVarLikeType]]:
"""Solve type constraints.

Expand All @@ -54,6 +55,8 @@ def solve_constraints(
If allow_polymorphic=True, then use the full algorithm that can potentially return
free type variables in solutions (these require special care when applying). Otherwise,
use a simplified algorithm that just solves each type variable individually if possible.

The skip_unsatisfied flag matches the same one in applytype.apply_generic_arguments().
"""
vars = [tv.id for tv in original_vars]
if not vars:
Expand Down Expand Up @@ -110,7 +113,7 @@ def solve_constraints(
candidate = AnyType(TypeOfAny.special_form)
res.append(candidate)

if not free_vars:
if not free_vars and not skip_unsatisfied:
# Most of the validation for solutions is done in applytype.py, but here we can
# quickly test solutions w.r.t. to upper bounds, and use the latter (if possible),
# if solutions are actually not valid (due to poor inference context).
Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -3748,3 +3748,22 @@ empty: Dict[NoReturn, NoReturn]
def bar() -> Union[Dict[str, Any], Dict[int, Any]]:
return empty
[builtins fixtures/dict.pyi]

[case testUpperBoundInferenceFallbackNotOverused]
from typing import TypeVar, Protocol, List

S = TypeVar("S", covariant=True)
class Foo(Protocol[S]):
def foo(self) -> S: ...
def foo(x: Foo[S]) -> S: ...

T = TypeVar("T", bound="Base")
class Base:
def foo(self: T) -> T: ...
class C(Base):
pass

def f(values: List[T]) -> T: ...
x = foo(f([C()]))
reveal_type(x) # N: Revealed type is "__main__.C"
[builtins fixtures/list.pyi]