Skip to content

Improvements to functools.partial of types #17898

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

Merged
merged 3 commits into from
Oct 9, 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
8 changes: 4 additions & 4 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,10 @@ def extract_callable_type(self, inner_type: Type | None, ctx: Context) -> Callab
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(
type_object_type(inner_type.item.type, self.named_type), inner_type.item
)
inner_type = get_proper_type(
self.expr_checker.analyze_type_type_callee(inner_type.item, ctx)
)

if isinstance(inner_type, CallableType):
outer_type = inner_type
elif isinstance(inner_type, Instance):
Expand Down
36 changes: 36 additions & 0 deletions test-data/unit/check-functools.test
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,39 @@ def bar(f: S) -> S:
g = functools.partial(f, "foo")
return f
[builtins fixtures/primitives.pyi]


[case testFunctoolsPartialAbstractType]
# flags: --python-version 3.9
from abc import ABC, abstractmethod
from functools import partial

class A(ABC):
def __init__(self) -> None: ...
@abstractmethod
def method(self) -> None: ...

def f1(cls: type[A]) -> None:
cls()
partial_cls = partial(cls)
partial_cls()

def f2() -> None:
A() # E: Cannot instantiate abstract class "A" with abstract attribute "method"
partial_cls = partial(A) # E: Cannot instantiate abstract class "A" with abstract attribute "method"
partial_cls() # E: Cannot instantiate abstract class "A" with abstract attribute "method"
[builtins fixtures/tuple.pyi]


[case testFunctoolsPartialSelfType]
from functools import partial
from typing_extensions import Self

class A:
def __init__(self, ts: float, msg: str) -> None: ...

@classmethod
def from_msg(cls, msg: str) -> Self:
factory = partial(cls, ts=0)
return factory(msg=msg)
[builtins fixtures/tuple.pyi]
2 changes: 2 additions & 0 deletions test-data/unit/lib-stub/typing_extensions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Required: _SpecialForm
NotRequired: _SpecialForm
ReadOnly: _SpecialForm

Self: _SpecialForm

@final
class TypeAliasType:
def __init__(
Expand Down
Loading