From b0ed1054fe1f97106fd2339666e460eceb4eba1c Mon Sep 17 00:00:00 2001 From: Roy Williams Date: Mon, 1 Aug 2016 11:03:45 -0400 Subject: [PATCH] Set type of keyword arguments to `Dict[unicode, Any]` in Python 2. This fixes https://github.com/python/mypy/issues/1954 --- mypy/checker.py | 10 +++++++++- test-data/unit/python2eval.test | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/mypy/checker.py b/mypy/checker.py index 76b229b8e405..2aff22da839d 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -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. @@ -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 diff --git a/test-data/unit/python2eval.test b/test-data/unit/python2eval.test index b203b22f2eeb..5ded00a3065f 100644 --- a/test-data/unit/python2eval.test +++ b/test-data/unit/python2eval.test @@ -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