Skip to content

Commit 5dcdfec

Browse files
author
Roy Williams
committed
Set type of keyword arguments to Dict[unicode, Any] in Python 2.
This fixes #1954
1 parent a284c48 commit 5dcdfec

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

mypy/checker.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,14 @@ def is_implicit_any(t: Type) -> bool:
553553
arg_type = self.named_generic_type('builtins.tuple',
554554
[arg_type])
555555
elif typ.arg_kinds[i] == nodes.ARG_STAR2:
556+
if self.options.python_version[0] >= 3:
557+
key_type = self.str_type()
558+
else:
559+
key_type = self.unicode_type()
556560
arg_type = self.named_generic_type('builtins.dict',
557-
[self.str_type(),
561+
[key_type,
558562
arg_type])
563+
559564
item.arguments[i].variable.type = arg_type
560565

561566
# Type check initialization expressions.
@@ -2115,6 +2120,10 @@ def str_type(self) -> Instance:
21152120
"""Return instance type 'str'."""
21162121
return self.named_type('builtins.str')
21172122

2123+
def unicode_type(self) -> Instance:
2124+
"""Return instance type 'unicode'."""
2125+
return self.named_type('builtins.unicode')
2126+
21182127
def check_type_equivalency(self, t1: Type, t2: Type, node: Context,
21192128
msg: str = messages.INCOMPATIBLE_TYPES) -> None:
21202129
"""Generate an error if the types are not equivalent. The

test-data/unit/python2eval.test

+19
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,25 @@ print '>', ['a', b'b', u'c']
128128
[out]
129129
> [u'a', 'b', u'c']
130130

131+
[case testUnicodeLiteralsReadKwargs_python2]
132+
from __future__ import unicode_literals
133+
def f(**kwargs): # type: (...) -> None
134+
return kwargs.get('a', '')
135+
f(a='b')
136+
[out]
137+
138+
[case testUnicodeStrReadKwargs_python2]
139+
def f(**kwargs): # type: (...) -> None
140+
return kwargs.get(u'a', '')
141+
f(a='b')
142+
[out]
143+
144+
[case testPy2StrReadKwargs_python2]
145+
def f(**kwargs): # type: (...) -> None
146+
return kwargs.get('a', '')
147+
f(a='b')
148+
[out]
149+
131150
[case testUnicodeLiteralsKwargs_python2]
132151
from __future__ import unicode_literals
133152
def f(**kwargs): # type: (...) -> None

0 commit comments

Comments
 (0)