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

Resolve TypeVar upper bounds in functools.partial #17660

Merged
merged 3 commits into from
Aug 10, 2024
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
2 changes: 2 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ def extract_callable_type(self, inner_type: Type | None, ctx: Context) -> Callab
inner_type = get_proper_type(inner_type)
outer_type: CallableType | None = None
if inner_type is not None and not isinstance(inner_type, AnyType):
if isinstance(inner_type, TypeVarLikeType):
inner_type = get_proper_type(inner_type.upper_bound)
if isinstance(inner_type, TypeType):
if isinstance(inner_type.item, Instance):
inner_type = expand_type_by_instance(
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-functools.test
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,19 @@ p(1, "no") # E: Argument 2 to "A" has incompatible type "str"; expected "int"

q: partial[A] = partial(A, 1) # OK
[builtins fixtures/tuple.pyi]

[case testFunctoolsPartialTypeVarBound]
from typing import Callable, TypeVar, Type
import functools

T = TypeVar("T", bound=Callable[[str, int], str])
S = TypeVar("S", bound=Type[int])

def foo(f: T) -> T:
g = functools.partial(f, "foo")
return f

def bar(f: S) -> S:
g = functools.partial(f, "foo")
return f
[builtins fixtures/primitives.pyi]
Loading