Skip to content

Enable annotation quoting for multi-line expressions#9142

Merged
charliermarsh merged 2 commits intomainfrom
charlie/multi-quote
Dec 15, 2023
Merged

Enable annotation quoting for multi-line expressions#9142
charliermarsh merged 2 commits intomainfrom
charlie/multi-quote

Conversation

@charliermarsh
Copy link
Copy Markdown
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
@charliermarsh charliermarsh deleted the charlie/multi-quote branch December 15, 2023 01:03
@MichaReiser
Copy link
Copy Markdown
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.

@github-actions
Copy link
Copy Markdown
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