How do I annotate types in a for-loop


How do I annotate types in a for-loop



I want to annotate a type of a variable in a for-loop.
I tried this:


for


for i: int in range(5):
pass



But it didn't work, obviously.



What I expect is working autocomplete in PyCharm 2016.3.2.
Pre-annotation like this:


i: int
for i in range(5):
pass



doesn't help.



P.S. Pre-annotation works for PyCharm >= 2017.1




4 Answers
4



According to PEP 526, this is not allowed:



In addition, one cannot annotate variables used in a for or with
statement
; they can be annotated ahead of time, in a similar manner to
tuple unpacking


for


with



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.





I tried it, but it doesn't seem to be working in the last stable PyCharm. I don't get any autocompletion at least.
– grepcake
Jan 13 '17 at 18:52






@A.Yurchenko yeah, I have 2017.2 and don't see the autocompletion to recognize i as an integer inside the loop (but it is recognized outside the loop). Looks like a PyCharm problem at this point, let me dig it more. Thanks.
– alecxe
Jan 13 '17 at 18:55



i





I can confirm, it works and Visual Studio. Big thanks!
– Soaku
Jan 19 at 17:56





PyCharm 2017.3.3 does recognize the type if it is annotated outside of the loop. However, it underscores it as an unused variable.
– David Bruce Borenstein
Mar 18 at 14:41





@philologon nice! Thanks for heads up, updated the answer accordingly.
– alecxe
Jul 2 at 18:50



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





Thank you, but I'll stick to the PEP 526 option
– grepcake
Jul 13 '17 at 14:49






While not PEP 526 compliant, this does work in PyCharm (at least as of 2017.2.1) and has the added benefit of also working in Python 3.0-3.5 (which doesn't support pre-annotation syntax introduced in Python 3.6).
– phoenix
Aug 13 '17 at 12:24




This works well for my in PyCharm (using Python 3.6)


for i in range(5):
i: int = i
pass



None of the responses here were useful, except to say that you can't. Even the accepted answer uses syntax from the PEP 526 document, which isn't valid python syntax. If you try to type in


x: int



You'll see it's a syntax error.



Here is a useful workaround:


for __x in range(5):
x = __x # type: int
print(x)



Do your work with x. PyCharm recognizes its type, and autocomplete works.


x





It is valid syntax,at least, for python 3.6. See PEP 526
– grepcake
Jun 6 '17 at 14:44







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PHP contact form sending but not receiving emails

PHP parse/syntax errors; and how to solve them?

iOS Top Alignment constraint based on screen (superview) height