Skip to content

**kwargs should be of type Dict[Union[str, unicode], Any] in Python 2 #1954

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
rowillia opened this issue Jul 28, 2016 · 13 comments · Fixed by #1971
Closed

**kwargs should be of type Dict[Union[str, unicode], Any] in Python 2 #1954

rowillia opened this issue Jul 28, 2016 · 13 comments · Fixed by #1971

Comments

@rowillia
Copy link
Contributor

If a python 2 file is using from __future__ import unicode_literals breaks reading values out of kwargs using string literals.

Tested using e7ddab1acf44f02d63e8f5d70b1b07c70eeff73b

$ cat > test_kwargs.py << EOL
from __future__ import unicode_literals

def kwargs_func(**kwargs):
  # type: (...) -> str
  return kwargs.get('some_value', '')

kwargs_func(some_value='foo')
EOL

$ mypy --fast-parser --py2 test_kwargs.py
test_kwargs.py: note: In function "kwargs_func":
test_kwargs.py:5: error: Argument 1 to "get" of "dict" has incompatible type "unicode"; expected "str"
@gvanrossum
Copy link
Member

gvanrossum commented Jul 29, 2016 via email

@rowillia
Copy link
Contributor Author

Thanks @gvanrossum! Out of curiosity, why unicode and not Union[str, unicode]? Shouldn't users be able to access **kwargs via string literals as well as unicode literals?

@gvanrossum
Copy link
Member

In mypy, anything that accepts unicode is also deemed to accept str (even though that's a bit of a lie). So the union would be unneeded.

@rowillia
Copy link
Contributor Author

rowillia commented Jul 29, 2016

@gvanrossum Thanks for the explanation! Does that suggest typeshed should use unicode everywhere instead of str? For example:

https://github.com/python/typeshed/blob/39325bf159e9af94e7aad5086b713ec03a23e6a3/stdlib/2and3/argparse.pyi#L49
https://github.com/python/typeshed/blob/746f4db9899dfccb2e236033f6736e1665e1286d/stdlib/2.7/os/__init__.pyi#L12-L13

(We tend to use unicode_literals in our Python Codebase everywhere so this is tripping us up when adopting mypy)

@gvanrossum
Copy link
Member

gvanrossum commented Jul 29, 2016 via email

@rowillia
Copy link
Contributor Author

rowillia commented Aug 1, 2016

@gvanrossum Unfortunately a284c48 didn't fix this issue, it fixed passing a Dict[unicode, Any] in as **kwargs but didn't fix reading values out of **kwargs.

@gvanrossum
Copy link
Member

Reopening, since I rolled back PR #1971.

@gvanrossum gvanrossum reopened this Aug 2, 2016
@rowillia
Copy link
Contributor Author

rowillia commented Aug 3, 2016

@gvanrossum Any more info on what broke? Happy to fix it up.

@gvanrossum
Copy link
Member

This is the prime example: #1971 (comment)

@rowillia
Copy link
Contributor Author

rowillia commented Aug 4, 2016

@gvanrossum OK digging into this a bit it seems that in Python 2 type of kwargs should be Dict[basetring, Any] with a contravariant key. Does that seem right? Trying to figure out how to accomplish that in the mypy codebase itself now.

@gvanrossum
Copy link
Member

I don't think you can do that without creating a custom subclass of Dict --
co/contravariance is a property of a type, not of a type alias (nor of a
type variable). Maybe we will just have to do that. The value type should
be taken from the type specified in **kwds: <type> though.

On Thu, Aug 4, 2016 at 11:16 AM, Roy Williams [email protected]
wrote:

@gvanrossum https://github.com/gvanrossum OK digging into this a bit it
seems that in Python 2 type of kwargs should be Dict[basetring, Any] with
a contravariant key. Does that seem right? Trying to figure out how to
accomplish that in the mypy codebase itself now.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#1954 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/ACwrMlqnUOm6LGs3-E54A5Jqkh-qt6k4ks5qcixrgaJpZM4JXvR1
.

--Guido van Rossum (python.org/~guido)

@gnprice gnprice added the bug mypy got something wrong label Aug 4, 2016
@gnprice
Copy link
Collaborator

gnprice commented Aug 4, 2016

This may be something best resolved with the more precise types for Python 2's interconversion bytes <-> unicode when things are all ASCII -- @ddfisher will be writing up more about this soon. (There was a discussion back at PyCon that unfortunately wasn't written up electronically, but we just now reconstructed it and will post one or more issues with the specifics.)

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 28, 2020

I think that it's too late to change this, and since this problem is specific to Python 2, it will gradually get less important.

@JukkaL JukkaL closed this as completed Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants