Skip to content

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

Closed

Description

@t2y

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"

Activity

  1. o11c commented on Oct 5, 2015

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

  2. mgeisler commented on Mar 14, 2016

    @mgeisler

    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.

  3. added this to the milestone on Apr 7, 2016
  4. gvanrossum commented on Apr 7, 2016

    @gvanrossum
    Member

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

  5. modified the milestones: , on Apr 7, 2016
  6. ddfisher commented on Apr 8, 2016

    @ddfisher
    Collaborator

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

  7. modified the milestones: , on Jul 14, 2016
  8. gvanrossum commented on Jul 14, 2016

    @gvanrossum
    Member

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

  9. gvanrossum commented on Nov 1, 2016

    @gvanrossum
    Member

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

  10. afrieder commented on Jan 20, 2017

    @afrieder
    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?

  11. gvanrossum commented on Jan 20, 2017

    @gvanrossum
    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.

  12. ddfisher commented on Jan 20, 2017

    @ddfisher
    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.)

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions