Skip to content

Commit aeea17a

Browse files
authored
Accept unicode literals to TypeVar and NamedTuple/namedtuple (#1928)
Fixes #1926.
1 parent aad72a3 commit aeea17a

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

mypy/semanal.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,8 @@ def check_typevar_name(self, call: CallExpr, name: str, context: Context) -> boo
13211321
if len(call.args) < 1:
13221322
self.fail("Too few arguments for TypeVar()", context)
13231323
return False
1324-
if not isinstance(call.args[0], (StrExpr, BytesExpr)) or not call.arg_kinds[0] == ARG_POS:
1324+
if (not isinstance(call.args[0], (StrExpr, BytesExpr, UnicodeExpr))
1325+
or not call.arg_kinds[0] == ARG_POS):
13251326
self.fail("TypeVar() expects a string literal as first argument", context)
13261327
return False
13271328
if cast(StrExpr, call.args[0]).value != name:
@@ -1467,13 +1468,14 @@ def parse_namedtuple_args(self, call: CallExpr,
14671468
return self.fail_namedtuple_arg("Too many arguments for namedtuple()", call)
14681469
if call.arg_kinds != [ARG_POS, ARG_POS]:
14691470
return self.fail_namedtuple_arg("Unexpected arguments to namedtuple()", call)
1470-
if not isinstance(args[0], (StrExpr, BytesExpr)):
1471+
if not isinstance(args[0], (StrExpr, BytesExpr, UnicodeExpr)):
14711472
return self.fail_namedtuple_arg(
14721473
"namedtuple() expects a string literal as the first argument", call)
14731474
types = [] # type: List[Type]
14741475
ok = True
14751476
if not isinstance(args[1], ListExpr):
1476-
if fullname == 'collections.namedtuple' and isinstance(args[1], (StrExpr, BytesExpr)):
1477+
if (fullname == 'collections.namedtuple'
1478+
and isinstance(args[1], (StrExpr, BytesExpr, UnicodeExpr))):
14771479
str_expr = cast(StrExpr, args[1])
14781480
items = str_expr.value.split()
14791481
else:
@@ -1483,7 +1485,8 @@ def parse_namedtuple_args(self, call: CallExpr,
14831485
listexpr = args[1]
14841486
if fullname == 'collections.namedtuple':
14851487
# The fields argument contains just names, with implicit Any types.
1486-
if any(not isinstance(item, (StrExpr, BytesExpr)) for item in listexpr.items):
1488+
if any(not isinstance(item, (StrExpr, BytesExpr, UnicodeExpr))
1489+
for item in listexpr.items):
14871490
return self.fail_namedtuple_arg("String literal expected as namedtuple() item",
14881491
call)
14891492
items = [cast(StrExpr, item).value for item in listexpr.items]
@@ -1504,7 +1507,7 @@ def parse_namedtuple_fields_with_types(self, nodes: List[Node],
15041507
return self.fail_namedtuple_arg("Invalid NamedTuple field definition",
15051508
item)
15061509
name, type_node = item.items
1507-
if isinstance(name, (StrExpr, BytesExpr)):
1510+
if isinstance(name, (StrExpr, BytesExpr, UnicodeExpr)):
15081511
items.append(name.value)
15091512
else:
15101513
return self.fail_namedtuple_arg("Invalid NamedTuple() field name", item)

test-data/unit/check-python2.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ s = u'foo' # E: Incompatible types in assignment (expression has type "unicode",
99
s = b'foo'
1010
[builtins_py2 fixtures/python2.py]
1111

12+
[case testTypeVariableUnicode]
13+
from typing import TypeVar
14+
T = TypeVar(u'T')
15+
16+
[case testNamedTuple*sh Unicode]
17+
from typing import NamedTuple
18+
from collections import namedtuple
19+
N = NamedTuple(u'N', [(u'x', int)])
20+
n = namedtuple(u'n', u'x y')
21+
1222
[case testPrintStatement]
1323
print ''() # E: "str" not callable
1424
print 1, 1() # E: "int" not callable

0 commit comments

Comments
 (0)