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

Support type objects in functools.partial #17292

Merged
merged 2 commits into from
May 25, 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
5 changes: 5 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,11 @@ 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, TypeType):
if isinstance(inner_type.item, Instance):
inner_type = expand_type_by_instance(
type_object_type(inner_type.item.type, self.named_type), inner_type.item
)
if isinstance(inner_type, CallableType):
outer_type = inner_type
elif isinstance(inner_type, Instance):
Expand Down
32 changes: 29 additions & 3 deletions test-data/unit/check-functools.test
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ p(1) # E: Argument 1 to "A" has incompatible type "int"; expected "str"
p(z=1) # E: Unexpected keyword argument "z" for "A"

def main(t: Type[A]) -> None:
p = functools.partial(t, 1) # E: "Type[A]" not callable
p = functools.partial(t, 1)
reveal_type(p) # N: Revealed type is "functools.partial[__main__.A]"

p("a") # OK
p(1) # False negative
p(z=1) # False negative
p(1) # E: Argument 1 to "A" has incompatible type "int"; expected "str"
p(z=1) # E: Unexpected keyword argument "z" for "A"
Comment on lines -306 to +311
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome that these are now fixed!


[builtins fixtures/dict.pyi]

Expand Down Expand Up @@ -346,3 +346,29 @@ reveal_type(functools.partial(fn3, 2)()) # E: "str" not callable \
# N: Revealed type is "builtins.int" \
# E: Argument 1 to "partial" has incompatible type "Union[Callable[[int], int], str]"; expected "Callable[..., int]"
[builtins fixtures/tuple.pyi]

[case testFunctoolsPartialTypeObject]
import functools
from typing import Type, Generic, TypeVar

class A:
def __init__(self, val: int) -> None: ...

cls1: Type[A]
reveal_type(functools.partial(cls1, 2)()) # N: Revealed type is "__main__.A"
functools.partial(cls1, "asdf") # E: Argument 1 to "A" has incompatible type "str"; expected "int"

T = TypeVar("T")
class B(Generic[T]):
def __init__(self, val: T) -> None: ...

cls2: Type[B[int]]
reveal_type(functools.partial(cls2, 2)()) # N: Revealed type is "__main__.B[builtins.int]"
functools.partial(cls2, "asdf") # E: Argument 1 to "B" has incompatible type "str"; expected "int"

def foo(cls3: Type[B[T]]):
reveal_type(functools.partial(cls3, "asdf")) # N: Revealed type is "functools.partial[__main__.B[T`-1]]" \
# E: Argument 1 to "B" has incompatible type "str"; expected "T"
reveal_type(functools.partial(cls3, 2)()) # N: Revealed type is "__main__.B[T`-1]" \
# E: Argument 1 to "B" has incompatible type "int"; expected "T"
[builtins fixtures/tuple.pyi]
Loading