mypy cannot parse type comment of "with" and "for" statement in PEP 484 #892

Closed
t2y opened this Issue Oct 4, 2015 · 9 comments

Comments

Projects
None yet
7 participants
@t2y
Contributor

t2y commented Oct 4, 2015

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"
@o11c

This comment has been minimized.

Show comment
Hide comment
@o11c

o11c Oct 5, 2015

Contributor

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?

Contributor

o11c commented Oct 5, 2015

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?

@mgeisler

This comment has been minimized.

Show comment
Hide comment
@mgeisler

mgeisler Mar 14, 2016

I ran into this today and have resorted to using cast on the context manager:

with cast(BinaryIO, open(path, 'rb')) as fp:
    # use fp...

That makes the work-around nicely localized.

I ran into this today and have resorted to using cast on the context manager:

with cast(BinaryIO, open(path, 'rb')) as fp:
    # use fp...

That makes the work-around nicely localized.

@gvanrossum gvanrossum added this to the 0.4.0 milestone Apr 7, 2016

@gvanrossum

This comment has been minimized.

Show comment
Hide comment
@gvanrossum

gvanrossum Apr 7, 2016

Member

Since this is specified in PEP 484, mypy should support it. But it's not high priority.

Member

gvanrossum commented Apr 7, 2016

Since this is specified in PEP 484, mypy should support it. But it's not high priority.

@gvanrossum gvanrossum modified the milestones: 0.3.3, 0.4.0 Apr 7, 2016

@ddfisher

This comment has been minimized.

Show comment
Hide comment
@ddfisher

ddfisher Apr 8, 2016

Collaborator

The WIP fast parser (#1353) supports parsing types on with statements, so this should be easier to implement after that lands.

Collaborator

ddfisher commented Apr 8, 2016

The WIP fast parser (#1353) supports parsing types on with statements, so this should be easier to implement after that lands.

@gvanrossum gvanrossum modified the milestones: 0.5, 0.4.x Jul 14, 2016

@gvanrossum

This comment has been minimized.

Show comment
Hide comment
@gvanrossum

gvanrossum Jul 14, 2016

Member

Moving to 0.5.0 given we aren't very far with the fast parser yet and this feels pretty unimportant.

Member

gvanrossum commented Jul 14, 2016

Moving to 0.5.0 given we aren't very far with the fast parser yet and this feels pretty unimportant.

@gvanrossum

This comment has been minimized.

Show comment
Hide comment
@gvanrossum

gvanrossum Nov 1, 2016

Member

(This is still TODO; we parse those type comments but don't use them.)

Member

gvanrossum commented Nov 1, 2016

(This is still TODO; we parse those type comments but don't use them.)

@afrieder

This comment has been minimized.

Show comment
Hide comment
@afrieder

afrieder Jan 20, 2017

Contributor

How would type comments work with multiple targets in with stmts. For example:

with X as (x, y), Z as (z, w):  # type: ...?

Would the type comment be of the form # type: int, float, float, int, where the number of elements in the type-tuple must match the number of targets in the with?

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 # type: Tuple[int, float], Tuple[float, int] is sensible?

Contributor

afrieder commented Jan 20, 2017

How would type comments work with multiple targets in with stmts. For example:

with X as (x, y), Z as (z, w):  # type: ...?

Would the type comment be of the form # type: int, float, float, int, where the number of elements in the type-tuple must match the number of targets in the with?

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 # type: Tuple[int, float], Tuple[float, int] is sensible?

@gvanrossum

This comment has been minimized.

Show comment
Hide comment
@gvanrossum

gvanrossum Jan 20, 2017

Member

Maybe like this:

with X as (a, b):  # type: Tuple[int, float]
    ...
with X as (a, b), Y as (c, d):  # type Tuple[int, float], Tuple[str, str]
    ...

This may be tricky because IIRC the # type: parser currently only doesn't allow more than one top-level type.

Member

gvanrossum commented Jan 20, 2017

Maybe like this:

with X as (a, b):  # type: Tuple[int, float]
    ...
with X as (a, b), Y as (c, d):  # type Tuple[int, float], Tuple[str, str]
    ...

This may be tricky because IIRC the # type: parser currently only doesn't allow more than one top-level type.

@ddfisher

This comment has been minimized.

Show comment
Hide comment
@ddfisher

ddfisher Jan 20, 2017

Collaborator

I think this should work okay for the new parser, actually. Tuple[int, float], Tuple[str, str] is a valid Python expression which parses the same as (Tuple[int, float], Tuple[str, str]), so there shouldn't be an issue. (And I tried mypy --fast-parser -c 'x, y = 1, 2 # type: int, str', which works and gives you the error you'd expect.)

Collaborator

ddfisher commented Jan 20, 2017

I think this should work okay for the new parser, actually. Tuple[int, float], Tuple[str, str] is a valid Python expression which parses the same as (Tuple[int, float], Tuple[str, str]), so there shouldn't be an issue. (And I tried mypy --fast-parser -c 'x, y = 1, 2 # type: int, str', which works and gives you the error you'd expect.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment