Skip to content
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

Type annotations on with statements #1356

Closed
rwbarton opened this issue Apr 8, 2016 · 2 comments
Closed

Type annotations on with statements #1356

rwbarton opened this issue Apr 8, 2016 · 2 comments

Comments

@rwbarton
Copy link
Contributor

rwbarton commented Apr 8, 2016

We might not always be able to infer the type of the variable bound by a with statement, just like we might not always be able to infer the type of a variable being assigned to for the first time. For this minimal example

from typing import TypeVar
from contextlib import ContextManager
T = TypeVar('T')
def g() -> ContextManager[T]: ...

with g() as x:
    pass

mypy reports

with.py:6: error: Need type annotation for variable

It would be nice to be able to write, for example,

with g() as x:  # type: int

declaring the type of x. This is currently rejected by mypy as a parse error.

@gvanrossum
Copy link
Member

Yeah, PEP 484 says this should work. (Also for a for loop.)

@gvanrossum
Copy link
Member

Actually we already have an issue for this. Closing as dupe of #892.

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

2 participants