Skip to content

[Fix error] Syntax Error Bug encountered with pyupgrade and multi-line Union #23207

@tlambert03

Description

@tlambert03

Just encountered the request for bug report when running ruff:

error: Fix introduced a syntax error. Reverting all changes.

This indicates a bug in Ruff. If you could open an issue at:

I was able to narrow it down to a pretty small reproducer.

$ tree .
.
├── main.py
└── pyproject.toml
# pyproject.toml
[project]
name = "ruffbug"
version = "0.1.0"
requires-python = ">=3.10"

[tool.ruff]
line-length = 88
target-version = "py310"
fix = true

[tool.ruff.lint]
select = ["UP"]
# main.py
from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from typing import Literal, TypeAlias, Union

    LongLiterals: TypeAlias = Union[
        Literal["LongLiteralNumberOne"]
        | Literal["LongLiteralNumberTwo"]
        | Literal["LongLiteralNumberThree"]
    ]
$ uv run ruff check main.py --fix

error: Fix introduced a syntax error. Reverting all changes.

This indicates a bug in Ruff. If you could open an issue at:

    https://github.com/astral-sh/ruff/issues/new?title=%5BFix%20error%5D

...quoting the contents of `main.py`, the rule codes UP007, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!

UP007 Use `X | Y` for type annotations
  --> main.py:8:31
   |
 6 |       from typing import Literal, TypeAlias, Union
 7 |
 8 |       LongLiterals: TypeAlias = Union[
   |  _______________________________^
 9 | |         Literal["LongLiteralNumberOne"]
10 | |         | Literal["LongLiteralNumberTwo"]
11 | |         | Literal["LongLiteralNumberThree"]
12 | |     ]
   | |_____^
   |
help: Convert to `X | Y`

Found 1 error.
[*] 1 fixable with the `--fix` option.

importantly: If I get rid of that third Literal["LongLiteralNumberThree"] (such that formatting would cause the union to fit on a single line), then a round of ruff format and ruff check work fine. It seems to only have difficulty with the multi-line union

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingfixesRelated to suggested fixes for violations

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions