-
Notifications
You must be signed in to change notification settings - Fork 342
Use IO instead of BytesIO/TextIO #1495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
BytesIO/TextIO are the types returned by open(), but this precludes other working types such as tempfile.TemporaryFile()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need tempfile.TemporaryFile
and why can't it be compatible with BinaryIO or TextIO?
I think @balamurugana python made it a base class https://docs.python.org/3/glossary.html#term-file-object |
I misremembered about TemporaryFile which does return a BinaryIO subclass, but my point still stands that
Footnotes
|
@harshavardhana It is about typing. Why not https://docs.python.org/3/library/typing.html#abcs-for-working-with-io works? |
If |
It seems you didn't read what I wrote?
Sorry, this is objectively not true. Any type that is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the check
import tempfile
from typing import BinaryIO, TextIO
def read_text(data: TextIO) -> None:
print("textio", data)
def read_binary(data: BinaryIO) -> None:
print("binaryio", data)
binfile = tempfile.TemporaryFile()
read_text(binfile)
read_binary(binfile)
textfile = tempfile.TemporaryFile("w+")
read_text(textfile)
read_binary(textfile)
and mypy
check returns error as expected
$ mypy --strict --no-color-output c.py
c.py:14: error: Argument 1 to "read_text" has incompatible type "BufferedRandom"; expected "TextIO" [arg-type]
c.py:19: error: Argument 1 to "read_binary" has incompatible type "TextIOWrapper[_WrappedBuffer]"; expected "BinaryIO" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
This PR doesn't add any value.
You keep talking about TemporaryFile but I acknowledged in my second message already that it does implement BinaryIO:
Rather than construct hypothetical mypy scripts, you could acknowledge that mypy still passes on minio-py after my change, which it wouldn't if
|
@RazerM Passing CI is not the question here. From python standard libraries, what is not compatible with |
But it is? If minio needed something that
I have explained several times the difference between these interfaces. The amount of arguing against this change is frankly baffling. You keep saying my change has no value despite the fact that Here is the entire definition of class BinaryIO(IO[bytes]):
@abstractmethod
def __enter__(self) -> BinaryIO: ... As you can see, there is no value in using It doesn't make sense for you to ask for some standard library type which this applies to as if we're not just using public interfaces. The whole point of interfaces is for types to implement them. But since you keep asking here is one I've had to go and find: from typing import IO, BinaryIO, reveal_type
import tempfile
def old_put_object(data: BinaryIO) -> None:
reveal_type(data.read())
def new_put_object(data: IO[bytes]) -> None:
reveal_type(data.read())
with tempfile.NamedTemporaryFile() as tmpfile:
old_put_object(tmpfile)
new_put_object(tmpfile)
|
BytesIO/TextIO are the types returned by
open()
, but this precludes other working types such astempfile.TemporaryFile()
.