From c110191243174c0baecc10522b8cfbf004b7162b Mon Sep 17 00:00:00 2001 From: SharoonSharif <150296639+SharoonSharif@users.noreply.github.com> Date: Mon, 21 Sep 2026 19:48:12 -0600 Subject: [PATCH] [pywin32] Fix win32file.ReadFile return types and overlapped forms ReadFile returns bytes, not str: a new bytes object when no OVERLAPPED is passed, the buffer object itself (or a freshly allocated read buffer for an int size) when one is. The stub also rejected an OVERLAPPED together with an int size and accepted only PyOVERLAPPEDReadBuffer as the buffer, while the runtime takes any writable buffer. --- stubs/pywin32/win32/win32file.pyi | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/stubs/pywin32/win32/win32file.pyi b/stubs/pywin32/win32/win32file.pyi index 34443c808ade..4cb0def25531 100644 --- a/stubs/pywin32/win32/win32file.pyi +++ b/stubs/pywin32/win32/win32file.pyi @@ -1,11 +1,13 @@ -from _typeshed import Incomplete +from _typeshed import Incomplete, WriteableBuffer from socket import socket -from typing import overload +from typing import TypeVar, overload from typing_extensions import deprecated import _win32typing from win32.lib.pywintypes import TimeType, error as error +_BufferT = TypeVar("_BufferT", bound=_win32typing.PyOVERLAPPEDReadBuffer | WriteableBuffer) + def AreFileApisANSI(): ... def CancelIo(handle: int, /) -> None: ... def CopyFile(_from: str, to: str, bFailIfExists, /) -> None: ... @@ -59,11 +61,17 @@ def GetFileSize(): ... def AllocateReadBuffer(bufSize: int, /) -> _win32typing.PyOVERLAPPEDReadBuffer: ... @overload -def ReadFile(hFile: int, bufSize: int, /) -> tuple[int, str]: ... +def ReadFile(hFile: int, bufSize: int, overlapped: None = None, /) -> tuple[int, bytes]: ... +@overload +def ReadFile( + hFile: int, bufSize: int, overlapped: _win32typing.PyOVERLAPPED, / +) -> tuple[int, _win32typing.PyOVERLAPPEDReadBuffer]: ... @overload def ReadFile( - hFile: int, buffer: _win32typing.PyOVERLAPPEDReadBuffer, overlapped: _win32typing.PyOVERLAPPED | None, / -) -> tuple[int, str]: ... + hFile: int, buffer: _win32typing.PyOVERLAPPEDReadBuffer | WriteableBuffer, overlapped: None = None, / +) -> tuple[int, bytes]: ... +@overload +def ReadFile(hFile: int, buffer: _BufferT, overlapped: _win32typing.PyOVERLAPPED, /) -> tuple[int, _BufferT]: ... def WriteFile( hFile: int, data: str | bytes | _win32typing.PyOVERLAPPEDReadBuffer, ol: _win32typing.PyOVERLAPPED | None = ..., /