# type: comments over multiple lines for functions, however, there doesn't seem to be such a thing to split markup for inline attributes. For example:import collections
from typing import DefaultDict, List
def test(pools):
really_long_attribute = collections.defaultdict(list) # type: DefaultDict[str, List[str]]
for pool in pools:
pass # meaningful stuff in here
Mypy follows PEP 484 in this respect. Changes should be proposed at https://github.com/python/typing/issues.
There are a few things that can help:
1) Define a type alias and use it as the type instead.
2) Split the assignment statement so that there is more space for the type annotation.
3) Declare the variable in a separate assignment statement with a dummy None initializer. Example:
really_long_attribute = None # type: DefaultDict[str, List[str]]
really_long_attribute = collections.defaultdict(list)
Maybe we should add this to common issues in mypy documentation?
Mypy follows PEP 484 in this respect. Changes should be proposed at https://github.com/python/typing/issues.
@JukkaL Thanks for the tip. I'll open the issue there shortly.
Maybe we should add this to common issues in mypy documentation?
Yes, please. The way I've worked around this for now is by escaping the newline. However, this is ugly and should be unnecessary.
def test(pools):
really_long_attribute = collections.defaultdict(list) \
# type: DefaultDict[str, List[str]]
for pool in pools:
pass # meaningful stuff in here
Reported in python/typing#534
Actually escaping the newline is a perfectly reasonable solution and avoids
the ambiguity of putting the type comment on a separate line (someone not
familiar with this convention might wonder if it applies to the previous
line or to the next).
Escaping the newline is not really a solution if the type signature by itself doesn't fit on one line though.
Escaping the newline is not really a solution if the type signature by itself doesn't fit on one line though.
Agree. In a lot of cases, the type signature would be very long, especially with Callables.
Most helpful comment
Escaping the newline is not really a solution if the type signature by itself doesn't fit on one line though.