Closed
Description
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.
Activity
gvanrossum commentedon Apr 8, 2016
Yeah, PEP 484 says this should work. (Also for a
for
loop.)gvanrossum commentedon Apr 8, 2016
Actually we already have an issue for this. Closing as dupe of #892.