Skip to content

passing unicode to some of the stdlib functions produces incorrect mypy error in python 2.7 #1411

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
2 tasks
manu-chroma opened this issue Jun 15, 2017 · 6 comments

Comments

@manu-chroma
Copy link

manu-chroma commented Jun 15, 2017

This issue highlights few incorrect stubs in Python 2 stdlib stub files. Most of these are related to passing unicode string to stdlib functions which work correctly at runtime, but mypy shows unsupported type. (because stub files have mentioned str as the only allowed type)
List of the errors

from __future__ import unicode_literals
from typing import Any, Text

def hello(name, **kwargs):
    # type: (Text, **Any) -> None
    with open(name, 'w') as r:
        pass

    b = kwargs.get('foo', 'nil')

hello("wow", foo="bar")

output

 ~  mypy --py2 test.py                                                                                                        
test.py:6: error: No overload variant of "open" matches argument types [builtins.unicode, builtins.unicode]
test.py:9: error: No overload variant of "get" of "Mapping" matches argument types [builtins.unicode, builtins.unicode]

Updated issue to include other found errors

@emmatyping
Copy link
Member

emmatyping commented Jun 15, 2017

As mentioned on gitter, the taker of this should also look at whether this method takes unicode

@manu-chroma
Copy link
Author

In my code, I pass Tuple[str, str, str, str, unicode] for parts argument and it seems to work correctly.

@manu-chroma manu-chroma changed the title 2/stdlib/urlparse: incorrect stubs for urlunsplit passing unicode to some of the stdlib functions produces incorrect mypy error in python 2.7 Jun 15, 2017
JelleZijlstra added a commit to JelleZijlstra/typeshed that referenced this issue Jul 1, 2017
Part of python#1411

urlunsplit() and similar functions accept either unicode or str in all
places. Their actual return type is unicode if any of the arguments is
unicode and nonempty, which the type system can't exactly express. I
left the return type as str because str is implicitly promoted to
unicode, so using the return type in a place that accepts unicode should
work.

unquote, parse_qs, and parse_qsl return unicode if you pass them unicode,
so AnyStr is appropriate in their stubs.
JelleZijlstra added a commit to JelleZijlstra/typeshed that referenced this issue Jul 1, 2017
Part of python#1411

Also remove some explicit parameter defaults.
@JelleZijlstra
Copy link
Member

I'm about to submit PRs to fix two of the issues you mentioned.

I'm not sure your third issue (using unicode keys for a **kwargs dict) can be fixed in typeshed. The hack proposed in python/mypy#3619 will probably fix it though.

JelleZijlstra added a commit to JelleZijlstra/typeshed that referenced this issue Jul 1, 2017
Part of python#1411

Also remove some explicit parameter defaults.
@JelleZijlstra
Copy link
Member

See also python/mypy#1954.

matthiaskramm pushed a commit that referenced this issue Jul 5, 2017
* urlparse: allow unicode arguments in more places

Part of #1411

urlunsplit() and similar functions accept either unicode or str in all
places. Their actual return type is unicode if any of the arguments is
unicode and nonempty, which the type system can't exactly express. I
left the return type as str because str is implicitly promoted to
unicode, so using the return type in a place that accepts unicode should
work.

unquote, parse_qs, and parse_qsl return unicode if you pass them unicode,
so AnyStr is appropriate in their stubs.

* fix type for urldefrag

The return type was wrong; this function returns a 2-tuple. The second member of the tuple is always a `str` if the argument type does not contain '#', and otherwise matches the type of the argument.
matthiaskramm pushed a commit that referenced this issue Jul 5, 2017
Part of #1411

Also remove some explicit parameter defaults.
@JelleZijlstra
Copy link
Member

Both of the functions mentioned are fixed.

@mehdigmira
Copy link
Contributor

same issue for:

  • attrgetter() function of operator library
  • CDLL() function of ctypes

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

4 participants