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

functools.partial incorrectly handles constrained generic function arguments #17411

Closed
tamird opened this issue Jun 20, 2024 · 0 comments · Fixed by #17925
Closed

functools.partial incorrectly handles constrained generic function arguments #17411

tamird opened this issue Jun 20, 2024 · 0 comments · Fixed by #17925
Labels
bug mypy got something wrong

Comments

@tamird
Copy link
Contributor

tamird commented Jun 20, 2024

Looks like the generic return type is being resolved even before a value of a concrete type is provided.

from typing import Type, TypeVar
import functools

T = TypeVar("T", int, str)

def foo(a: int, b: str, t: Type[T]) -> T: ...

p1 = functools.partial(foo, 1, 2)  # E: Argument 2 to "foo" has incompatible type "int"; expected "str"
p2 = functools.partial(foo, "hello", "world")  # E: Argument 1 to "foo" has incompatible type "str"; expected "int"
p3 = functools.partial(foo, 1, "hello")

reveal_type(p3)  # N: Revealed type is "functools.partial[builtins.int]"  # should still be generic
reveal_type(p3(int))  # N: Revealed type is "builtins.int"
reveal_type(p3(str))  # N: Revealed type is "builtins.int" \
                      # E: Argument 1 to "foo" has incompatible type "Type[str]"; expected "Type[int]"
reveal_type(p3(list))  # N: Revealed type is "builtins.int" \
                       # E: Argument 1 to "foo" has incompatible type "Type[List[Any]]"; expected "Type[int]"

See #17297 for a runnable test case.

@tamird tamird added the bug mypy got something wrong label Jun 20, 2024
@hauntsaninja hauntsaninja changed the title functools.partial incorrectly handles generic function arguments functools.partial incorrectly handles constrained generic function arguments Jul 1, 2024
hauntsaninja pushed a commit that referenced this issue Oct 14, 2024
Fixes #17411

The fix is that we remove type variables that can never be inferred from
the initial `check_call()` call. Actual diff is tiny, I just moved a
bunch of code, since I need formal to actual mapping sooner now.
JukkaL pushed a commit that referenced this issue Oct 14, 2024
Fixes #17411

The fix is that we remove type variables that can never be inferred from
the initial `check_call()` call. Actual diff is tiny, I just moved a
bunch of code, since I need formal to actual mapping sooner now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant