Skip to content

Commit a284c48

Browse files
rowilliagvanrossum
authored andcommitted
Support passing Dict[unicode, Any] in as **kwargs (#1966)
Fixes #1954.
1 parent 4c1d5de commit a284c48

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

mypy/checkexpr.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,8 +1657,21 @@ def is_valid_var_arg(self, typ: Type) -> bool:
16571657

16581658
def is_valid_keyword_var_arg(self, typ: Type) -> bool:
16591659
"""Is a type valid as a **kwargs argument?"""
1660-
return is_subtype(typ, self.chk.named_generic_type(
1661-
'builtins.dict', [self.named_type('builtins.str'), AnyType()]))
1660+
if self.chk.options.python_version[0] >= 3:
1661+
return is_subtype(typ, self.chk.named_generic_type(
1662+
'builtins.dict', [self.named_type('builtins.str'),
1663+
AnyType()]))
1664+
else:
1665+
return (
1666+
is_subtype(typ, self.chk.named_generic_type(
1667+
'builtins.dict',
1668+
[self.named_type('builtins.str'),
1669+
AnyType()]))
1670+
or
1671+
is_subtype(typ, self.chk.named_generic_type(
1672+
'builtins.dict',
1673+
[self.named_type('builtins.unicode'),
1674+
AnyType()])))
16621675

16631676
def has_non_method(self, typ: Type, member: str) -> bool:
16641677
"""Does type have a member variable / property with the given name?"""

test-data/unit/python2eval.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,28 @@ print '>', ['a', b'b', u'c']
128128
[out]
129129
> [u'a', 'b', u'c']
130130

131+
[case testUnicodeLiteralsKwargs_python2]
132+
from __future__ import unicode_literals
133+
def f(**kwargs): # type: (...) -> None
134+
pass
135+
params = {'a': 'b'}
136+
f(**params)
137+
[out]
138+
139+
[case testUnicodeStringKwargs_python2]
140+
def f(**kwargs): # type: (...) -> None
141+
pass
142+
params = {u'a': 'b'}
143+
f(**params)
144+
[out]
145+
146+
[case testStrKwargs_python2]
147+
def f(**kwargs): # type: (...) -> None
148+
pass
149+
params = {'a': 'b'}
150+
f(**params)
151+
[out]
152+
131153
[case testFromFutureImportUnicodeLiterals2_python2]
132154
from __future__ import unicode_literals
133155
def f(x: str) -> None: pass

0 commit comments

Comments
 (0)