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

Polymorphic inference: support for parameter specifications and lambdas #15837

Merged
merged 15 commits into from
Aug 15, 2023
Prev Previous commit
Next Next commit
Fix corner case with upper bounds
  • Loading branch information
Ivan Levkivskyi committed Aug 11, 2023
commit 72da8f5d3293619cfc04804ed4ad30e7f0a7e055
2 changes: 1 addition & 1 deletion mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ def visit_instance(self, template: Instance) -> list[Constraint]:
elif isinstance(actual, TupleType) and self.direction == SUPERTYPE_OF:
return infer_constraints(template, mypy.typeops.tuple_fallback(actual), self.direction)
elif isinstance(actual, TypeVarType):
if not actual.values:
if not actual.values and not actual.id.is_meta_var():
return infer_constraints(template, actual.upper_bound, self.direction)
return []
elif isinstance(actual, ParamSpecType):
Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -3244,3 +3244,22 @@ def apply(a: S, b: T) -> None:
v2: Callable[[Tuple[S, T]], None]
return pipe(a, v1, v2)
[builtins fixtures/tuple.pyi]

[case testInferenceAgainstGenericParamSpecSpuriousBoundsNotUsed]
# flags: --new-type-inference
from typing import TypeVar, Callable, Generic
from typing_extensions import ParamSpec, Concatenate

Q = ParamSpec("Q")
class Foo(Generic[Q]): ...

T1 = TypeVar("T1", bound=Foo[...])
T2 = TypeVar("T2", bound=Foo[...])
P = ParamSpec("P")
def pop_off(fn: Callable[Concatenate[T1, P], T2]) -> Callable[P, Callable[[T1], T2]]:
...

@pop_off
def test(command: Foo[Q]) -> Foo[Q]: ...
reveal_type(test) # N: Revealed type is "def () -> def [Q] (__main__.Foo[Q`-1]) -> __main__.Foo[Q`-1]"
[builtins fixtures/tuple.pyi]