Skip to content

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

@Paillat-dev

Description

@Paillat-dev

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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggreat writeupA wonderful example of a quality contributionhelp wantedContributions especially welcome

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions