Skip to content

typing.TextIO: add write_through and reconfigure. #8171

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
wants to merge 1 commit into from

Conversation

ppentchev
Copy link
Contributor

Hi,

Thanks a lot for taking care of typeshed, mypy, and the related projects!

What do you think about the attached trivial patch that makes it possible to use e.g. sys.stdin.reconfigure(encoding="UTF-8") in cases when it is needed?

Thanks again, and keep up the great work!

G'luck,
Peter

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@ppentchev
Copy link
Contributor Author

Hm. Okay. So the test suite seems to be correct: Python 3.7's typing module does indeed not declare TextIO.reconfigure() and TextIO.write_through. However, they are present in the actual sys.stdin/stdout/stderr objects. So... what do we do here? And okay, fine, "do nothing and add type: ignore on the sys.stdin.reconfgure() line" is an acceptable answer.

@Akuli
Copy link
Collaborator

Akuli commented Jun 26, 2022

This was already "fixed" in #4035, but that fix doesn't work because sys.stdin is typed as TextIO, not as TextIOWrapper which is the class that provides reconfigure(). Changing sys.stdin to be a TextIOWrapper isn't great either, because then you can't do sys.stdin = io.StringIO().

A couple things we could do instead:

  • Use sys.__stdin__.recoonfigure(). The __stdin__ attribute isn't meant to be reassigned to a StringIO or whatever other file-like object, so it can be typed as a TextIOWrapper, and it's already typed that way. If sys.stdin hasn't been reassigned, __stdin__ and stdin refer to the same object.
  • Change the type of stdin to be TextIOWrapper | Any. This means your code must work if stdin is a TextIOWrapper, but it could technically be something else too, and assigning something else to it is not an error.

You can also use isinstance() like in #4035.

@srittau
Copy link
Collaborator

srittau commented Jun 26, 2022

typeshed's TextIO type follows the implementation of TextIO in Python's standard library. The latter doesn't have write_through() or reconfigure(). Also, considering the pseudo-protocol nature of TextIO, it's difficult to add items to it, even in the standard library.

@srittau srittau closed this Jun 26, 2022
@Akuli
Copy link
Collaborator

Akuli commented Jun 26, 2022

To be clear, here's how to do this with isinstance:

if isinstance(sys.stdout, io.TextIOWrapper) and sys.version_info >= (3, 7):
    sys.stdout.reconfigure(encoding="UTF-8")

@ppentchev
Copy link
Contributor Author

Thanks, that helps a lot! And yes, I get it, the I/O types are, hm, complicated. So yeah, thanks, and sorry for the noise!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants