Skip to content

Set type of keyword arguments to Dict[unicode, Any] in Python 2. #1971

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

Merged
merged 1 commit into from
Aug 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,9 @@ def is_implicit_any(t: Type) -> bool:
[arg_type])
elif typ.arg_kinds[i] == nodes.ARG_STAR2:
arg_type = self.named_generic_type('builtins.dict',
[self.str_type(),
[self.unicode_type(),
arg_type])

item.arguments[i].variable.type = arg_type

# Type check initialization expressions.
Expand Down Expand Up @@ -2115,6 +2116,13 @@ def str_type(self) -> Instance:
"""Return instance type 'str'."""
return self.named_type('builtins.str')

def unicode_type(self) -> Instance:
"""Return instance type 'unicode'."""
if self.options.python_version[0] >= 3:
return self.named_type('builtins.str')
else:
return self.named_type('builtins.unicode')

def check_type_equivalency(self, t1: Type, t2: Type, node: Context,
msg: str = messages.INCOMPATIBLE_TYPES) -> None:
"""Generate an error if the types are not equivalent. The
Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/python2eval.test
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,25 @@ print '>', ['a', b'b', u'c']
[out]
> [u'a', 'b', u'c']

[case testUnicodeLiteralsReadKwargs_python2]
from __future__ import unicode_literals
def f(**kwargs): # type: (...) -> None
return kwargs.get('a', '')
f(a='b')
[out]

[case testUnicodeStrReadKwargs_python2]
def f(**kwargs): # type: (...) -> None
return kwargs.get(u'a', '')
f(a='b')
[out]

[case testPy2StrReadKwargs_python2]
def f(**kwargs): # type: (...) -> None
return kwargs.get('a', '')
f(a='b')
[out]

[case testUnicodeLiteralsKwargs_python2]
from __future__ import unicode_literals
def f(**kwargs): # type: (...) -> None
Expand Down