How do I annotate types in a for-loop
I don't know if this solution is PEP-compatible or just a feature of PyCharm, but I made it work like this:
for i in range(5): #type: int
pass
and I'm using Pycharm Community Edition 2016.2.1
This works well for my in PyCharm (using Python 3.6)
for i in range(5):
i: int = i
pass
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.