python / typeshed

Collection of library stubs for Python, with static types
Other
4.32k stars 1.73k forks source link

Shouldn't FileIO indicate None as possible return of read/write? #12684

Closed dcolascione closed 1 day ago

dcolascione commented 1 week ago

Typeshed currently has these declarations in io.pyi for FileIO.

    def write(self, b: ReadableBuffer, /) -> int: ...
    def read(self, size: int = -1, /) -> bytes: ...

Shouldn't these return int | None and bytes | None? A FileIO is non-blocking mode can return None from these methods, yes?

tomasr8 commented 1 week ago

Looking at the code, both read and write return None when EAGAIN is raised so technically the return annotation should include None as well.

srittau commented 1 week ago

For reference, this is similar to what has been discussed in https://discuss.python.org/t/handling-sys-stdin-read-in-non-blocking-mode/59633.

srittau commented 1 week ago

Also related to #12414. PR: #12686