Repository navigation
[pywin32] Improve pipe and device I/O typing - #16456
Robolightning wants to merge 4 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Avasam
left a comment
There was a problem hiding this comment.
Thanks for the contribution, here's a few improvements I found.
| Device: int, IoControlCode: int, InBuffer: ReadableBuffer | None, OutBuffer: int, Overlapped: None = None | ||
| ) -> bytes: ... | ||
| @overload | ||
| def DeviceIoControl( | ||
| Device: int, IoControlCode: int, InBuffer: ReadableBuffer | None, OutBuffer: int, Overlapped: _win32typing.PyOVERLAPPED | ||
| ) -> memoryview: ... | ||
| @overload | ||
| def DeviceIoControl( | ||
| Device: int, |
There was a problem hiding this comment.
Device: int is overly restrictive and rejects handle objects
| Device: int, IoControlCode: int, InBuffer: ReadableBuffer | None, OutBuffer: int, Overlapped: None = None | |
| ) -> bytes: ... | |
| @overload | |
| def DeviceIoControl( | |
| Device: int, IoControlCode: int, InBuffer: ReadableBuffer | None, OutBuffer: int, Overlapped: _win32typing.PyOVERLAPPED | |
| ) -> memoryview: ... | |
| @overload | |
| def DeviceIoControl( | |
| Device: int, | |
| Device: int | _win32typing.PyHANDLE, IoControlCode: int, InBuffer: ReadableBuffer | None, OutBuffer: int, Overlapped: None = None | |
| ) -> bytes: ... | |
| @overload | |
| def DeviceIoControl( | |
| Device: int | _win32typing.PyHANDLE, IoControlCode: int, InBuffer: ReadableBuffer | None, OutBuffer: int, Overlapped: _win32typing.PyOVERLAPPED | |
| ) -> memoryview: ... | |
| @overload | |
| def DeviceIoControl( | |
| Device: int | _win32typing.PyHANDLE, |
| nOutBufferSize: int, | ||
| nInBufferSize: int, | ||
| nDefaultTimeOut: int, | ||
| sa: _win32typing.PySECURITY_ATTRIBUTES, |
There was a problem hiding this comment.
Not part of your changes, but may as well fix: This can be None, pywin32's own win32/test/test_win32file.py calls this as such.
| sa: _win32typing.PySECURITY_ATTRIBUTES, | |
| sa: _win32typing.PySECURITY_ATTRIBUTES | None, |
| OutBuffer: WriteableBuffer, | ||
| Overlapped: _win32typing.PyOVERLAPPED | None = None, | ||
| ) -> WriteableBuffer: ... |
There was a problem hiding this comment.
OutBuffer: WriteableBuffer rejects AllocateReadBuffer(n), even though the pywin32 docs name it as the intended input
// @pyparm int/buffer|OutBuffer||Size of the buffer to allocate for output, or a writeable buffer
// as returned by .
The stub types its return as _win32typing.PyOVERLAPPEDReadBuffer, an empty class that is not a Buffer. The ReadFile overloads in the same file already handle this with _BufferT = TypeVar(bound=PyOVERLAPPEDReadBuffer | WriteableBuffer). I'd suggest the same here:
| OutBuffer: WriteableBuffer, | |
| Overlapped: _win32typing.PyOVERLAPPED | None = None, | |
| ) -> WriteableBuffer: ... | |
| OutBuffer: _BufferT, | |
| Overlapped: _win32typing.PyOVERLAPPED | None = None, | |
| ) -> _BufferT: ... |
This also tightens the return type.
| nDefaultTimeOut: int, | ||
| sa: _win32typing.PySECURITY_ATTRIBUTES, | ||
| /, | ||
| ) -> int: ... |
There was a problem hiding this comment.
Not directly part of your changes, but may as well fix if modifying this function:
| ) -> int: ... | |
| ) -> _win32typing.PyHANDLE: ... |
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
This improves typing for several pywin32 pipe and device-I/O APIs.
Changes:
ConnectNamedPipeas returningintCreateNamedPipenumeric parameters andWaitNamedPipe.timeoutasintPeekNamedPipeas returningtuple[bytes, int, int]DeviceIoControloverloads for synchronous bytes output, overlapped memoryview output, and caller-supplied writable buffersValidation:
win32pipeandwin32fileon Windows: pass1.1.414) consumer: 0 errors, 0 warnings, 0 informations