Skip to content

Commit a0c2c1b

Browse files
msullivanJukkaL
authored andcommitted
Fix some more type errors under mypyc (#5585)
Add test cases for the constructs that triggered the trouble.
1 parent 16af91f commit a0c2c1b

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

mypy/fastparse2.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def transform_args(self,
405405
n: ast27.arguments,
406406
line: int,
407407
) -> Tuple[List[Argument], List[Statement]]:
408-
type_comments = n.type_comments
408+
type_comments = n.type_comments # type: Sequence[Optional[str]]
409409
converter = TypeConverter(self.errors, line=line)
410410
decompose_stmts = [] # type: List[Statement]
411411

@@ -432,8 +432,10 @@ def convert_arg(index: int, arg: ast27.expr) -> Var:
432432
return Var(v)
433433

434434
def get_type(i: int) -> Optional[Type]:
435-
if i < len(type_comments) and type_comments[i] is not None:
436-
return converter.visit_raw_str(type_comments[i])
435+
if i < len(type_comments):
436+
comment = type_comments[i]
437+
if comment is not None:
438+
return converter.visit_raw_str(comment)
437439
return None
438440

439441
args = [(convert_arg(i, arg), get_type(i)) for i, arg in enumerate(n.args)]

mypy/semanal_namedtuple.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ def parse_namedtuple_args(self, call: CallExpr, fullname: str
239239
for item in listexpr.items):
240240
return self.fail_namedtuple_arg("String literal expected as namedtuple() item",
241241
call)
242-
items = [cast(StrExpr, item).value for item in listexpr.items]
242+
items = [cast(Union[StrExpr, BytesExpr, UnicodeExpr], item).value
243+
for item in listexpr.items]
243244
else:
244245
# The fields argument contains (name, type) tuples.
245246
items, types, _, ok = self.parse_namedtuple_fields_with_types(listexpr.items, call)

test-data/unit/check-basic.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,12 @@ f(x)
364364
[file mock.py]
365365
[builtins fixtures/isinstance.pyi]
366366
[out]
367+
368+
[case testPartialTypeComments]
369+
def foo(
370+
a, # type: str
371+
b,
372+
args=None,
373+
):
374+
# type: (...) -> None
375+
pass

test-data/unit/check-namedtuple.test

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ a = x[1]
2020
a, b, c = x # E: Need more than 2 values to unpack (3 expected)
2121
x[2] # E: Tuple index out of range
2222

23+
[case testNamedTupleUnicode_python2]
24+
from __future__ import unicode_literals
25+
from collections import namedtuple
26+
27+
# This test is a regression test for a bug where mypyc-compiled mypy
28+
# would crash on namedtuple's with unicode arguments. Our test stubs
29+
# don't actually allow that, though, so we ignore the error and just
30+
# care we don't crash.
31+
X = namedtuple('X', ('x', 'y')) # type: ignore
32+
2333
[case testNamedTupleNoUnderscoreFields]
2434
from collections import namedtuple
2535

@@ -55,9 +65,9 @@ a.y = 5 # E: Property "y" defined in "X" is read-only
5565
from typing import NamedTuple
5666
from typing_extensions import Protocol
5767

58-
class HasX(Protocol):
68+
class HasX(Protocol):
5969
x: str
60-
70+
6171
class A(NamedTuple):
6272
x: str
6373

test-data/unit/check-python2.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,12 @@ class B(A):
324324
def g(self): # type: () -> None
325325
super(B, self).f()
326326
super().f() # E: Too few arguments for "super"
327+
328+
[case testPartialTypeComments_python2]
329+
def foo(
330+
a, # type: str
331+
b,
332+
args=None,
333+
):
334+
# type: (...) -> None
335+
pass

0 commit comments

Comments
 (0)