From a42740283804c40de2ffd8edc5e79e2296f8a4cf Mon Sep 17 00:00:00 2001 From: Huanyi Xie <74809554+xiehuanyi@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:06:26 +0300 Subject: [PATCH] Preserve hashlib.file_digest callable factory return types --- stdlib/hashlib.pyi | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stdlib/hashlib.pyi b/stdlib/hashlib.pyi index 50bc8e21f1d5..261b88158e22 100644 --- a/stdlib/hashlib.pyi +++ b/stdlib/hashlib.pyi @@ -20,7 +20,7 @@ from _hashlib import ( ) from _typeshed import ReadableBuffer from collections.abc import Callable, Set as AbstractSet -from typing import Protocol, type_check_only +from typing import Protocol, TypeVar, overload, type_check_only if sys.version_info >= (3, 15): __all__ = ( @@ -94,6 +94,8 @@ def new(name: str, data: ReadableBuffer = b"", *, usedforsecurity: bool = True) algorithms_guaranteed: AbstractSet[str] algorithms_available: AbstractSet[str] +_T = TypeVar("_T", bound=_HashObject) + if sys.version_info >= (3, 11): @type_check_only class _BytesIOLike(Protocol): @@ -104,9 +106,10 @@ if sys.version_info >= (3, 11): def readinto(self, buf: bytearray, /) -> int: ... def readable(self) -> bool: ... - def file_digest( - fileobj: _BytesIOLike | _FileDigestFileObj, digest: str | Callable[[], _HashObject], /, *, _bufsize: int = 262144 - ) -> HASH: ... + @overload + def file_digest(fileobj: _BytesIOLike | _FileDigestFileObj, digest: str, /, *, _bufsize: int = 262144) -> HASH: ... + @overload + def file_digest(fileobj: _BytesIOLike | _FileDigestFileObj, digest: Callable[[], _T], /, *, _bufsize: int = 262144) -> _T: ... # Legacy typing-only alias _Hash = HASH