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

Auto-quoting fails when type annotation spans multiple lines #9136

Closed
charliermarsh opened this issue Dec 14, 2023 · 0 comments · Fixed by #9142
Closed

Auto-quoting fails when type annotation spans multiple lines #9136

charliermarsh opened this issue Dec 14, 2023 · 0 comments · Fixed by #9142
Assignees
Labels
bug Something isn't working

Comments

@charliermarsh
Copy link
Member

Given:

from django.contrib.auth.models import AbstractBaseUser

def foo(
    self, user: AbstractBaseUser[
        int
    ]
):
    pass

Running cargo run -p ruff_cli -- check foo.py --select TCH --fix -n --unsafe-fixes fails with:

error: Fix introduced a syntax error in `foo.py` with rule codes TCH002: EOL while scanning string literal at byte offset 156
---
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from django.contrib.auth.models import AbstractBaseUser

def foo(
    self, user: 'AbstractBaseUser[
        int
    ]', view: 'type[CondorBaseViewSet]'
):
    pass

---
@charliermarsh charliermarsh added the bug Something isn't working label Dec 14, 2023
@charliermarsh charliermarsh self-assigned this Dec 14, 2023
charliermarsh added a commit that referenced this issue Dec 15, 2023
Given:

```python
x: DataFrame[
    int
] = 1
```

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

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

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

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

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

```python
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:

```python
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.
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 a pull request may close this issue.

1 participant