I want to annotate a type of a variable in a for
-loop. I tried this but it didn't work:
for i: int in range(5):
pass
What I expect is working autocomplete in PyCharm 2016.3.2, but using pre-annotation didn't work:
i: int
for i in range(5):
pass
P.S. Pre-annotation works for PyCharm >= 2017.1.
According to PEP 526, this is not allowed:
In addition, one cannot annotate variables used in a
for
orwith
statement; they can be annotated ahead of time, in a similar manner to tuple unpacking
Annotate it before the loop:
i: int
for i in range(5):
pass
PyCharm 2018.1 and up now recognizes the type of the variable inside the loop. This was not supported in older PyCharm versions.