Skip to content

open() overloads mixes typing.IO and io.IOBase hierarchies #6076

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

Closed
aucampia opened this issue Sep 25, 2021 · 6 comments
Closed

open() overloads mixes typing.IO and io.IOBase hierarchies #6076

aucampia opened this issue Sep 25, 2021 · 6 comments
Labels
topic: io I/O related issues

Comments

@aucampia
Copy link

Currently the overloads for the open builtin uses io.IOBase based types for specific cases (specifically io.{TextIOWrapper,FileIO,BufferedRandom,BufferedWriter,BufferedReader}), but for fallbacks it uses typing.IO based types (specifically typing.{BinaryIO,IO[Any]}).

The same is the case for Path.open and Traversable.open.

This would maybe not be a problem if the io.IOBase classes were considered subclasses, subtypes or implementations of typing.IO, but this is not the case. This can maybe be considered a problem on it's own, but it would still seem better to use a consistent approach for typing.

The practical problem that this causes is that functions that should deal with all result from open will have to accept both typing.IO and io.IOBase.

Written for commit 2217ac8, but can also be seen in typeshed included with mypy 0.910.

@aucampia
Copy link
Author

aucampia commented Sep 27, 2021

Given the response in #6077, I would assume the right approach is to rather move to typing.IO hierarchies then.

@srittau
Copy link
Collaborator

srittau commented Sep 27, 2021

All types returned by open() derive from typing.IO (in the stubs, at least). It should be enough to annotate fields using typing.IO[typing.Any]. Are there any examples where this fails (and that are not related to mypy's open plugin)?

@aucampia
Copy link
Author

The core of the issue is this: #6061 - but if the solution is to just not use the io.BaseIO type hierarchy for typing, then I guess it is not really a problem.

@aucampia
Copy link
Author

The reason why this all came up, I'm trying to type functions that should accept things returned by open, and then would pass them onto TextIOWrapper, and I was typing them using io.IOBase heirarchy, but if I should not do that, then problem solved. I just did not know I should not do that. Apologies for the issue spam. I think the documentation could be clearer, but I understand now.

@aucampia
Copy link
Author

Closing this issue as all returned types do derive from typing.IO, and typing.IO should be used instead of io.*IOBase when typing things.

@srittau
Copy link
Collaborator

srittau commented Sep 27, 2021

@aucampia I agree. The typing documentation is lacking. I added better documentation for I/O as a todo item for our not-yet-written Best Practices document: python/typing#851.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: io I/O related issues
Projects
None yet
Development

No branches or pull requests

2 participants