Skip to content

About pprint's stream arg #177

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
FichteFoll opened this issue May 1, 2016 · 5 comments
Closed

About pprint's stream arg #177

FichteFoll opened this issue May 1, 2016 · 5 comments

Comments

@FichteFoll
Copy link
Contributor

FichteFoll commented May 1, 2016

According to the docs:

An output stream may be set using the stream keyword; the only method used on the stream object is the file protocol’s write() method.

Currently the stream arg is specified as TextIO, which seems overly restrictive to me. I had my variable declared as IO[str], which supports the .write() method just fine and would completely suffice, so I believe that stream should at least be stripped down to IO[str].

However, since the code actually ony uses stream.write() (I checked the source to verify), IO[str] still seems overly restrictive with its ~20 methods.

Should a new private typing type be created in the stub that only defines write()?

Related: #163

@gvanrossum
Copy link
Member

I think it's reasonable to switch this to IO[str] (and if you send a PR to do that I'll happily review it). I don't think we should introduce new classes for duck typing like SupportsWrite; it's fragile (there aren't many interfaces that are documented to call just write()).

Eventually we should introduce protocols and then we might be able to deal with this better.

@FichteFoll
Copy link
Contributor Author

FichteFoll commented May 2, 2016

python/typing#11 sounds almost exactly like what I imagined for this. Alright, let's wait for Protocol support before thinking about a SupportsWrite protocol. I'll submit a pr for IO[str] in the meantime.

@FichteFoll
Copy link
Contributor Author

FichteFoll commented May 2, 2016

So, I did some testing and found out that the Python 2 implementation of pprint uses literal strings, i.e. str bytestrings, while in Python 3 literal strings are also str but Unicode strings.

In Py3, IO[str] is undoubtedly the correct type, but what would I put in Python 2 to specifically target bytestrings? bytes or str?

Note that the 2.7 stub currently has IO[Any] 😕 .

@gvanrossum
Copy link
Member

You seem to be making up terminology ("literal" strings usually refers to writing a string literal in source code, not to the type itself). But I understand your point. In Python 2 bytes is just an alias for str, and given that in Python 3 this uses IO[str] I think that that's also the best type for Python 2.

@FichteFoll
Copy link
Contributor Author

The usage of the term "literal string" was intended, example: https://github.com/python/cpython/blob/02f91871c1a976530cd2c566cbd6c85ad37f97a4/Lib/pprint.py#L140. Either way, str is the default string type in Python 2, so that should be natural.

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

No branches or pull requests

2 participants