Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Comments

@t2y
Copy link
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
Copy link
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
Copy link

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
Copy link
Member

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
Copy link
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
Copy link
Member

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

@gvanrossum
Copy link
Member

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

@afrieder
Copy link
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
Copy link
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.

@ddfisher
Copy link
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
Projects
None yet
Development

No branches or pull requests

7 participants