Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
mypy cannot parse type comment of "with" and "for" statement in PEP 484 #892
I tried to check type comments, but it cannot parse "with" and "for" statement as below.
# Example 1 x =  # type: List[Employee] x, y, z = , ,  # type: List[int], List[int], List[str] x, y, z = , ,  # type: (List[int], List[int], List[str]) x = [ 1, 2, ] # type: List[int] # Example 2 from contextlib import contextmanager @contextmanager def frobnicate(): yield 1 with frobnicate() as foo: # type: int # Here foo is an int ... # Example 3 points = [(1.0, 2.5)] # type: List[Tuple[float, float]] for x, y in points: # type: float, float # Here x and y are floats ...
$ mypy type_comments.py type_comments.py:17: error: Parse error before "int" type_comments.py:23: error: Parse error before "float"
Not supporting type comments of anything other than a function (is that even in the PEP?) or assignment is a known bug. I'm currently rewriting the parser (to track location information better and give better errors) but haven't gotten to type comments yet.
Ugh, since when are tuples of types directly supported? I know it's given in an example in the PEP, but it doesn't specify this stuff at all.
@gvanrossum What are we supposed to do about an unclear PEP?
referenced this issue
Aug 27, 2016
How would type comments work with multiple targets in
with X as (x, y), Z as (z, w): # type: ...?
Would the type comment be of the form
It's a bit complicated by the fact that when parsing, this is read as two distinct targets (each of which has a tuple type), so maybe
I think this should work okay for the new parser, actually.