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

Open
tamird opened this issue Jun 20, 2024 · 0 comments
Open
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
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

No branches or pull requests

1 participant