Skip to content

[functools] Preserve return type in partialmethod (#16499) - #16736

Open
hrithiksaini99 wants to merge 2 commits into
python:mainfrom
hrithiksaini99:fix-partialmethod-return-type
Open

hrithiksaini99 wants to merge 2 commits into
python:mainfrom
hrithiksaini99:fix-partialmethod-return-type

Conversation

@hrithiksaini99

Copy link
Copy Markdown
Contributor

Fixes #16499.

Description

In #16499, wrapping a callable in partialmethod resulted in partialmethod[Any] in mypy (e.g. partialmethod(pprint, options={}) where pprint -> str).

This was caused by the secondary func: _Descriptor overload where _Descriptor: TypeAlias = Any. Because _Descriptor had no _T type parameter, mypy unified every callable argument against both overloads, joining _T with Any and losing return type information.

Align partialmethod with partial (which does not have an unconstrained descriptor overload):

  • Remove the unconstrained func: _Descriptor overload on __init__ / __new__
  • Fix the first parameter name of __new__ from self to cls
  • Add regression test in stdlib/@tests/test_cases/check_functools.py asserting partialmethod and instance calls preserve _T.

@github-actions

This comment has been minimized.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

functools.partialmethod loses return type and callable signature information

1 participant