Skip to content

Commit b0ed105

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 b0ed105

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

mypy/checker.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,9 @@ def is_implicit_any(t: Type) -> bool:
554554
[arg_type])
555555
elif typ.arg_kinds[i] == nodes.ARG_STAR2:
556556
arg_type = self.named_generic_type('builtins.dict',
557-
[self.str_type(),
557+
[self.unicode_type(),
558558
arg_type])
559+
559560
item.arguments[i].variable.type = arg_type
560561

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

2119+
def unicode_type(self) -> Instance:
2120+
"""Return instance type 'unicode'."""
2121+
if self.options.python_version[0] >= 3:
2122+
return self.named_type('builtins.str')
2123+
else:
2124+
return self.named_type('builtins.unicode')
2125+
21182126
def check_type_equivalency(self, t1: Type, t2: Type, node: Context,
21192127
msg: str = messages.INCOMPATIBLE_TYPES) -> None:
21202128
"""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)