Skip to content

Commit 85f0682

Browse files
committed
Resolve TypeVar upper bounds in functools.partial
Mostly fixes python#17646
1 parent 8b74b5a commit 85f0682

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

mypy/checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,8 @@ def extract_callable_type(self, inner_type: Type | None, ctx: Context) -> Callab
682682
inner_type = get_proper_type(inner_type)
683683
outer_type: CallableType | None = None
684684
if inner_type is not None and not isinstance(inner_type, AnyType):
685+
if isinstance(inner_type, TypeVarLikeType):
686+
inner_type = inner_type.upper_bound
685687
if isinstance(inner_type, TypeType):
686688
if isinstance(inner_type.item, Instance):
687689
inner_type = expand_type_by_instance(

test-data/unit/check-functools.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,14 @@ p(1, "no") # E: Argument 2 to "A" has incompatible type "str"; expected "int"
541541

542542
q: partial[A] = partial(A, 1) # OK
543543
[builtins fixtures/tuple.pyi]
544+
545+
[case testFunctoolsPartialTypeVarBound]
546+
from typing import Callable, TypeVar
547+
import functools
548+
549+
T = TypeVar("T", bound=Callable[[str, int], str])
550+
551+
def foo(f: T) -> T:
552+
g = functools.partial(f, "foo")
553+
return f
554+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)