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

[Fix error]: Ruff UP007 fix introduces syntax error with nested string type annotations #14132

Open
Paillat-dev opened this issue Nov 6, 2024 · 2 comments
Labels
bug Something isn't working great writeup A wonderful example of a quality contribution help wanted Contributions especially welcome

Comments

@Paillat-dev
Copy link

When running Ruff's auto-fix for UP007 (non-pep604-annotation), it introduces a syntax error when handling nested quotes in type annotations. The specific case occurs when using string literals for forward references that contain quotes within quotes.

Minimal Reproducible Example

from typing import Union

class AClass:
    ...

def myfunc(param: "tuple[Union[int, 'AClass', None], str]"):
    print(param)

Current Behavior

Running ruff check . --fix produces:

error: Fix introduced a syntax error. Reverting all changes.
This indicates a bug in Ruff.

The code itself is valid Python and works correctly with type checkers. The nested quote structure ('AClass' inside "tuple[...]") is a legitimate pattern for forward references in type annotations, even tough the quotes are redundant.

Expected Behavior

Ruff should either:

  1. Correctly handle the nested quotes when applying UP007 fixes, or
  2. Skip applying UP007 fixes for complex nested quote cases to avoid introducing syntax errors

Environment

  • Ruff version: 0.7.2
  • Command: ruff check . --fix

Configuration

[tool.ruff]
select = ["ALL"]
# (other settings omitted for brevity)

Additional Context

This appears to be an edge case in quote handling for type annotations. While the syntax may look unusual, it's valid Python and is correctly interpreted by type checkers. The UP007 fixer might need to be more careful when handling nested quotes in type annotation strings.

@MichaReiser
Copy link
Member

Wow, nice write up!

This is related to #13934

@MichaReiser MichaReiser added great writeup A wonderful example of a quality contribution bug Something isn't working help wanted Contributions especially welcome labels Nov 6, 2024
@dhruvmanila
Copy link
Member

dhruvmanila commented Nov 7, 2024

I think this is related to #10586 instead. When the checker is visiting the Union[...] which is inside a string annotation, the Generator is not considering the outer quote. The rule mentioned here (UP007) doesn't use the quote_annotation function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working great writeup A wonderful example of a quality contribution help wanted Contributions especially welcome
Projects
None yet
Development

No branches or pull requests

3 participants