Skip to content
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

Support stringified type annotations that span across multiple lines #4359

Closed
yilei opened this issue Dec 20, 2022 · 1 comment
Closed

Support stringified type annotations that span across multiple lines #4359

yilei opened this issue Dec 20, 2022 · 1 comment
Labels
as designed Not a bug, working as intended enhancement request New feature or request

Comments

@yilei
Copy link

yilei commented Dec 20, 2022

Is your feature request related to a problem? Please describe.

Given the following code:

def something(
    arg,
) -> (
    "VeryLongClassNameWithAwkwardGenericSubtype[int] |"
    "VeryLongClassNameWithAwkwardGenericSubtype[str]"
):
    ...

Currently pyright won't accept it since the return stringified type annotation spans across multiple lines.

Describe the solution you'd like

It should support it and treat it the same as regular implicitly concatenated strings.

Additional context

This is discovered as part of psf/black#3462, Black can reflow the strings (merge implicitly concatenated strings into one when fits, or split them into multiple lines when it exceeds line length). I checked other type checkers (mypy, pytype, pyre), they do support it. But since pyright doesn't, psf/black#3462 special cases string annotations for all.

@yilei yilei added the enhancement request New feature or request label Dec 20, 2022
@erictraut
Copy link
Collaborator

erictraut commented Dec 20, 2022

There are good reasons why pyright does not support this. Unlike other type checkers, pyright is the basis for a language server, and it needs to be able to report accurate ranges for errors so they are underlined correctly. When it parses a stringified type annotation, it must parse the the entire string as a single block of text so any errors can be mapped back to the offset within the string.

For that reason, we have no plans to support strings that are concatenated string literals in type annotations.

If you need a type annotation to span multiple lines, you could use a multi-line string, like this:

    """(VeryLongClassNameWithAwkwardGenericSubtype[int] |
    VeryLongClassNameWithAwkwardGenericSubtype[str])"""

Another option is to use a typing.Union, like this:

    Union[
        "VeryLongClassNameWithAwkwardGenericSubtype[int]",
        "VeryLongClassNameWithAwkwardGenericSubtype[str])"
    ]

Or you can use from __future__ import annotations at the top of the file to eliminate the need for stringified types in type annotations.

@erictraut erictraut added the as designed Not a bug, working as intended label Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended enhancement request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants