How do I annotate types in a for-loop?

48,926

Solution 1

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.

Solution 2

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

Solution 3

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

for i in range(5):
    i: int = i
    pass
Share:
48,926

Related videos on Youtube

grepcake
Author by

grepcake

Updated on January 09, 2022

Comments

  • grepcake
    grepcake over 2 years

    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.

    • gdoumenc
      gdoumenc about 4 years
      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)
  • grepcake
    grepcake about 7 years
    It is valid syntax,at least, for python 3.6. See PEP 526
  • phoenix
    phoenix almost 7 years
    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).
  • Claude
    Claude almost 6 years
    FYI: This format is explicitly allowed/mentioned in PEP 484 (also to be python 2.7 compatible)
  • Cloud
    Cloud over 5 years
    But there will be a inspect info Local variable 'i' value is not used.
  • alecxe
    alecxe over 5 years
    @SiminJie yes, because this is just an example.
  • Marco
    Marco over 5 years
    This is also a valid option according to PEP 484
  • Jani Kärkkäinen
    Jani Kärkkäinen almost 5 years
    I think this should be the accepted answer, as this does exactly what was requested and doesn't give out other errors and/or warnings, as opposed to the currently accepted one.
  • Giovanni Di Milia
    Giovanni Di Milia over 4 years
    MyPy actually complains if you redefine the variable in the for loop
  • simpleuser
    simpleuser over 4 years
    This form also works with for/enumerate loops and PyCharm 2018. e.g. for index, area in enumerate(area_list): # type: int, AreaInfo
  • user136036
    user136036 over 4 years
    Do not redefine the variable. i: int is enough and you won't get any complaints.
  • Leo
    Leo over 3 years
    Not exactly wrong but this is coding without love ;)
  • topher217
    topher217 almost 3 years
    This also works well for for loops over something that is unpacked two multiple objects: e.g. key: str df: pd.DataFrame for key, df in myData.items(): ...
  • Vlad Iliescu
    Vlad Iliescu almost 2 years
    An elegant option, to me this looks better to me specifying types in comments.