Skip to content

Fix click.getchar return type - #1473

Merged
gvanrossum merged 4 commits into
python:masterfrom
pkch:click_getchar
Jun 11, 2018
Merged

gvanrossum merged 4 commits into
python:masterfrom
pkch:click_getchar

Conversation

@pkch

@pkch pkch commented Jul 9, 2017

Copy link
Copy Markdown
Contributor

The docs and actual behavior confirm click.getchar returns a bytes object.

@JelleZijlstra

Copy link
Copy Markdown
Member

Are you sure? http://click.pocoo.org/5/utils/#getting-characters-from-terminal says getchar returns a character as "a Unicode character". The implementation at https://github.com/pallets/click/blob/master/click/_termui_impl.py#L518 suggests it may return bytes in Python 3 on Windows, but in all other circumstances it decodes the character first.

@pkch

pkch commented Jul 9, 2017

Copy link
Copy Markdown
Contributor Author

Hmm I was on Windows and python 3 and saw a bytes object. Then I read the docs and, given that python doesn't have a type called "character", I conveniently interpreted this to mean a "bytes object that contains the unicode code point" :)

So yeah, you're right it's not correct. But... I don't think it's the greatest API to return a str in one case and bytes in another. I suppose I should change it to Union[str, bytes]?

@JelleZijlstra

Copy link
Copy Markdown
Member

I think you can do this with version/platform checks (unless I'm misreading the code): the return type is bytes if you're on Windows and Python 3, and otherwise Text.

I have a hard time understanding why it's implemented this way though; maybe it's a bug in the library?

@pkch

pkch commented Jul 9, 2017

Copy link
Copy Markdown
Contributor Author

Ok, let me ask on click github first to clarify; then I can update this PR.

@JelleZijlstra

Copy link
Copy Markdown
Member

Your click issue didn't get much response, but the one responder also seems to think that returning bytes on Windows in Python 3 is unexpected. For now, can we just change the return annotation to Text? That should fix an existing bug in Python 2. If you want, we can also use version and platform checks to reflect the fact that currently it returns bytes on Windows in py3.

@gvanrossum

Copy link
Copy Markdown
Member

Ping? Is this still needed? It's been well over half a year since the last activity.

@JelleZijlstra

Copy link
Copy Markdown
Member

I just implemented my suggestion above of using Text (which is mostly correct). I think we should merge that version.

@kip-13

kip-13 commented Mar 18, 2018

Copy link
Copy Markdown

I think that the return type can be maybe Union[Text, bytes], because if the getchar run in Windows / Python 3 the value is a byte string.

The function getchar use the function getch from module msvcrt.

Is just my suggestion.

@gvanrossum
gvanrossum merged commit a753aba into python:master Jun 11, 2018
yedpodtrzitko pushed a commit to yedpodtrzitko/typeshed that referenced this pull request Jan 23, 2019
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.

4 participants