253

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.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
grepcake
  • 3,960
  • 3
  • 16
  • 26
  • 1
    Just a remark : Normally you should not need it as the type is deduced from the range function (this is relevant for all internal declared variables) – gdoumenc Jun 02 '20 at 09:06

5 Answers5

371

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

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.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
87

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

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
David Vasquez
  • 979
  • 6
  • 4
  • 1
    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
  • 15
    FYI: This format is explicitly allowed/mentioned in PEP 484 (also to be python 2.7 compatible) – Claude Sep 14 '18 at 10:56
  • 4
    This is also a valid option according to [PEP 484](https://www.python.org/dev/peps/pep-0484/#id41) – Marco Feb 11 '19 at 07:50
  • 5
    This form also works with for/enumerate loops and PyCharm 2018. e.g. `for index, area in enumerate(area_list): # type: int, AreaInfo` – simpleuser Nov 04 '19 at 03:49
  • and I thought python can't get any weirder :) at first sight I thought the answer is totally unrelated lol. if you on the same path don't worry folks, it's actually working – eddym Aug 31 '23 at 07:55
27

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

for i in range(5):
    i: int = i
    pass
Samir
  • 401
  • 4
  • 3
1

Although I prefer to use type hints when possible, using assert isinstance(...) could be an alternative solution/work-around to achieve the same benefits (that is: proper syntax highlighting and auto-completion in the IDE). I don't know if this works in PyCharm, but it does work in Visual Studio Code.

for x, y, z in range(5):
    assert isinstance(i, int)
    # Now VS Code knows the type of `i`, so syntax highlighting
    # and auto-completion do work as intended :-)

Obviously, adding the assert has an effect on the code, and this might be a Good Thing or a Bad Thing, depending on your use case. It is definitely not the same as type-hinting, but as a side effect it seems to have the same benefits.

wovano
  • 4,543
  • 5
  • 22
  • 49
-6

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.

Edward Ned Harvey
  • 6,525
  • 5
  • 36
  • 45
  • 13
    It is valid syntax,at least, for python 3.6. See [PEP 526](https://www.python.org/dev/peps/pep-0526/#global-and-local-variable-annotations) – grepcake Jun 06 '17 at 14:44
  • 8
    Not exactly wrong but this is coding without love ;) – Leo Dec 08 '20 at 17:43