From 2088a4049583ebffc9ee4c2d5d5f3e388ac30ced Mon Sep 17 00:00:00 2001 From: Hrithik Saini <49943287+hrithiksaini99@users.noreply.github.com> Date: Sat, 7 Nov 2026 14:03:34 +0530 Subject: [PATCH 1/2] [functools] Preserve return type in partialmethod (#16499) --- stdlib/@tests/test_cases/check_functools.py | 20 ++++++++++++++++++-- stdlib/functools.pyi | 8 +------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/stdlib/@tests/test_cases/check_functools.py b/stdlib/@tests/test_cases/check_functools.py index d3aaf5ca1c4d..e84cc4ba963b 100644 --- a/stdlib/@tests/test_cases/check_functools.py +++ b/stdlib/@tests/test_cases/check_functools.py @@ -1,7 +1,8 @@ from __future__ import annotations -from functools import cache, cached_property, lru_cache, singledispatchmethod, update_wrapper, wraps -from typing import Callable, ParamSpec, TypeVar +from collections.abc import Callable +from functools import cache, cached_property, lru_cache, partialmethod, singledispatchmethod, update_wrapper, wraps +from typing import ParamSpec, TypeVar from typing_extensions import assert_type P = ParamSpec("P") @@ -225,3 +226,18 @@ def format_float(self: Formatter, value: float) -> str: assert_type(formatter.format.__name__, str) assert_type(formatter.format.__qualname__, str) assert_type(formatter.format.__wrapped__, Callable[..., str]) + + +def _pm_func(arg: object, /, *, options: dict[str, str]) -> str: + return "dummy" + + +pm = partialmethod(_pm_func, options={}) +assert_type(pm, partialmethod[str]) + + +class _Bar: + my_method = partialmethod(_pm_func, options={}) + + +assert_type(_Bar().my_method(), str) diff --git a/stdlib/functools.pyi b/stdlib/functools.pyi index 42041befd999..953c56edba81 100644 --- a/stdlib/functools.pyi +++ b/stdlib/functools.pyi @@ -208,15 +208,9 @@ class partialmethod(Generic[_T]): args: tuple[Any, ...] keywords: dict[str, Any] if sys.version_info >= (3, 14): - @overload - def __new__(self, func: Callable[..., _T], /, *args: Any, **keywords: Any) -> Self: ... - @overload - def __new__(self, func: _Descriptor, /, *args: Any, **keywords: Any) -> Self: ... + def __new__(cls, func: Callable[..., _T], /, *args: Any, **keywords: Any) -> Self: ... else: - @overload def __init__(self, func: Callable[..., _T], /, *args: Any, **keywords: Any) -> None: ... - @overload - def __init__(self, func: _Descriptor, /, *args: Any, **keywords: Any) -> None: ... def __get__(self, obj: Any, cls: type[Any] | None = None) -> Callable[..., _T]: ... @property From 6bd15a5a79db61426abc941f526d9c933d88b19d Mon Sep 17 00:00:00 2001 From: Hrithik Saini <49943287+hrithiksaini99@users.noreply.github.com> Date: Sat, 7 Nov 2026 14:17:22 +0530 Subject: [PATCH 2/2] Remove unused functools.partialmethod.__new__ from stubtest allowlists --- stdlib/@tests/stubtest_allowlists/py314.txt | 3 --- stdlib/@tests/stubtest_allowlists/py315.txt | 3 --- 2 files changed, 6 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/py314.txt b/stdlib/@tests/stubtest_allowlists/py314.txt index 9237ab9f1f25..ddc468cca273 100644 --- a/stdlib/@tests/stubtest_allowlists/py314.txt +++ b/stdlib/@tests/stubtest_allowlists/py314.txt @@ -9,9 +9,6 @@ types.UnionType.__mro_entries__ types.UnionType.__name__ types.UnionType.__qualname__ -# Assigning `__new__` causes `func` not to get recognized. -functools.partialmethod.__new__ - # Decorator approximated by classmethod concurrent.interpreters._crossinterp.classonly.* # Method using this decorator diff --git a/stdlib/@tests/stubtest_allowlists/py315.txt b/stdlib/@tests/stubtest_allowlists/py315.txt index 2ecb38cd0ba4..cdf531a239ae 100644 --- a/stdlib/@tests/stubtest_allowlists/py315.txt +++ b/stdlib/@tests/stubtest_allowlists/py315.txt @@ -72,9 +72,6 @@ concurrent.interpreters._crossinterp.UNBOUND_REMOVE # Condition functions are exported in __init__ threading.Condition.locked -# Assigning `__new__` causes `func` not to get recognized. -functools.partialmethod.__new__ - # These protocols use ABC hackery at runtime. (io|typing_extensions)\.Reader\.__class_getitem__ (io|typing_extensions)\.Reader\.read