PyCharm - Trailing type hints and line width limit

Viewed 1169

In PyCharm, I typically use trailing type hints (not sure of the right way of calling them) for variables such as this:

my_var = my_other_variable  # type: MyObject

However, sometimes I already have a very long line which I cannot really split, or the type hint is a bit long, such as:

my_var = my_really_long_variable_name  # type: QtWidgets.QVeryLongQtClassName

This would make the full line longer than my line width limit (79 characters), and hence PyCharm would flag a warning.

Hence, is there a way to put the type hint in a different line? I tried these but they do not seem to work:

# type: QtWidgets.QVeryLongQtClassName
my_var = my_really_long_variable_name

my_var = my_really_long_variable_name \
    # type: QtWidgets.QVeryLongQtClassName

The closest thing I can think of is this, which may not really shorten that much the line width:

my_var = \
    my_really_long_variable_name  # type: QtWidgets.QVeryLongQtClassName

Otherwise, the only alternative I have is to do something like this:

v = my_really_long_variable_name
my_var = v  # type: QtWidgets.QVeryLongQtClassName

The other alternative of shortening the type does not seem to work based on the tests I have done; PyCharm seems to struggle recognising that my_var's type is really QtWidgets.QVeryLongQtClassName in this case:

t = QtWidgets.QVeryLongQtClassName
my_var = my_really_long_variable_name  # type: t
1 Answers
Related