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

Enable annotation quoting for multi-line expressions #9142

Merged
merged 2 commits into from
Dec 15, 2023

Conversation

charliermarsh
Copy link
Member

Given:

x: DataFrame[
    int
] = 1

We currently wrap the annotation in single quotes, which leads to a syntax error:

x: "DataFrame[
    int
]" = 1

There are a few options for what to suggest for users here... Use triple quotes:

x: """DataFrame[
    int
]""" = 1

Or, use an implicit string concatenation (which may require parentheses):

x: ("DataFrame["
    "int"
"]") = 1

The solution I settled on here is to use the Generator, which effectively means we write it out on a single line, like:

x: "DataFrame[int]" = 1

It's kind of the "least opinionated" solution, but it does mean we'll expand to a very long line in some cases.

Closes #9136.

@charliermarsh charliermarsh added the bug Something isn't working label Dec 15, 2023
@charliermarsh charliermarsh marked this pull request as ready for review December 15, 2023 01:00
@charliermarsh charliermarsh enabled auto-merge (squash) December 15, 2023 01:00
@charliermarsh charliermarsh merged commit d1a7bc3 into main Dec 15, 2023
16 checks passed
@charliermarsh charliermarsh deleted the charlie/multi-quote branch December 15, 2023 01:03
@MichaReiser
Copy link
Member

Or, use an implicit string concatenation (which may require parentheses):

I wonder if that trips over static analysis tools.

The solution I settled on here is to use the Generator, which effectively means we write it out on a single line, like:

The main downside of this is that it undoes any formatting and the formatter won't be able to properly format the type annotation (except if we would parse it) on the next run, because it is now an unsplittable string. But I think it's a good enough solution to fix the bug.

Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto-quoting fails when type annotation spans multiple lines
2 participants