-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
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:
- Correctly handle the nested quotes when applying UP007 fixes, or
- 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.