Skip to content

Support multi-line # type: comments for inline types #4511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stephenfin opened this issue Jan 25, 2018 · 7 comments
Closed

Support multi-line # type: comments for inline types #4511

stephenfin opened this issue Jan 25, 2018 · 7 comments

Comments

@stephenfin
Copy link

stephenfin commented Jan 25, 2018

#1102 provided a way to split # 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
@JukkaL
Copy link
Collaborator

JukkaL commented Jan 26, 2018

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?

@stephenfin
Copy link
Author

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

@stephenfin
Copy link
Author

Reported in python/typing#534

@gvanrossum
Copy link
Member

gvanrossum commented Jan 26, 2018 via email

@cristicbz
Copy link

Escaping the newline is not really a solution if the type signature by itself doesn't fit on one line though.

@cwhy
Copy link

cwhy commented Mar 12, 2020

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.

@hauntsaninja
Copy link
Collaborator

This is less of an issue now that all alive Pythons support PEP 526

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants