-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
open() returns IO[str] instead of io.TextIOWrapper #1229
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
Comments
The solution to your problem is probably to use IO[str], which is compatible with the IO[Any] returned by open() and has (most of) the right methods. It doesn't have the TextIO specific ones (like .encoding, .line_buffering, etc.), so if you want those you should use cast(TextIO, open(...)). However, I agree that this is an interesting problem and worth thinking about. It means TextIO and BinaryIO are annoyingly impractical to use in real code (a cast is necessary whenever open() is called). The documentation doesn't give many examples or suggest that IO[str] is what most people should use. |
Yeah, this is not one of the smoothest APIs. Note that in master, I think this is still wrong though. open() with a recognized mode argument should return f: TextIO = open(fn, 'rb') Some classes used by the implementation in the (Another note: it's not great to use runtime inspection to come up with type names to put into annotations -- things at runtime are a bit different from how things are dine on the static typing side of things.) |
I think any further improvements in typeshed for this are dependent on implementing dependent types, see python/mypy#3062. |
This is part of the IO classes restructuring. |
This was fixed in #4146 and others! |
Hello,
My env is Win7P+SP1 x64, Py 3.5.2 32b, Mypy 0.501.
I opened this issue (#3286) in mypy, but GVR told me to open it here because it was a "(complex) typeshed issue".
The var used in the with context is of type <class '_io.TextIOWrapper'>.
But when I try to set it's type, mypy returns the following error
data_processing.py:917: error: Incompatible types in assignment (expression has type IO[Any], variable has type "TextIOWrapper")
If I try to use module typing's own TextIO type it returns
data_processing.py:917: error: Incompatible types in assignment (expression has type IO[Any], variable has type "TextIO")
It only works if I do
Best regards,
JM
The text was updated successfully, but these errors were encountered: