mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-01 20:31:57 +00:00
<!-- Thank you for contributing to Ruff/ty! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? (Please prefix with `[ty]` for ty pull requests.) - Does this pull request include references to any relevant issues? --> ## Summary <!-- What's the purpose of the change? What does it do, and why? --> I noticed this while working on #18972. If the string targeted by [quoted-type-alias (TC008)](https://docs.astral.sh/ruff/rules/quoted-type-alias/#quoted-type-alias-tc008) is a multiline string, the fix would introduce a syntax error. This PR fixes that by adding parenthesis around the resulting replacement if the string contained any newline characters (`\n`, `\r`) if it doesn't already have parenthesis outside `("""...""")` or inside `"""(...)"""` the annotation. Failing examples: https://play.ruff.rs/8793eb95-860a-4bb3-9cbc-6a042fee2946 ``` PS D:\rust_projects\ruff> Get-Content issue.py ``` ```py from typing import TypeAlias OptInt: TypeAlias = """int | None""" type OptInt = """int | None""" ``` ``` PS D:\rust_projects\ruff> uvx ruff check issue.py --isolated --select TC008 --fix --diff --preview ``` ``` 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 `issue.py`, the rule codes TC008, along with the `pyproject.toml` settings and executed command, we'd be very appreciative! ``` This PR also makes the example error out-of-the-box for #18972 Old example: https://play.ruff.rs/f6cd5adb-7f9b-444d-bb3e-8c045241d93e ```py OptInt: TypeAlias = "int | None" ``` New example: https://play.ruff.rs/906c1056-72c0-4777-b70b-2114eb9e6eaf ```py from typing import TypeAlias OptInt: TypeAlias = "int | None" ``` The import was also added to the "Use instead" section. ## Test Plan <!-- How was it tested? --> Added multiple test cases |
||
|---|---|---|
| .. | ||
| ruff | ||
| ruff_annotate_snippets | ||
| ruff_benchmark | ||
| ruff_cache | ||
| ruff_db | ||
| ruff_dev | ||
| ruff_diagnostics | ||
| ruff_formatter | ||
| ruff_graph | ||
| ruff_index | ||
| ruff_linter | ||
| ruff_macros | ||
| ruff_notebook | ||
| ruff_options_metadata | ||
| ruff_python_ast | ||
| ruff_python_ast_integration_tests | ||
| ruff_python_codegen | ||
| ruff_python_formatter | ||
| ruff_python_index | ||
| ruff_python_literal | ||
| ruff_python_parser | ||
| ruff_python_semantic | ||
| ruff_python_stdlib | ||
| ruff_python_trivia | ||
| ruff_python_trivia_integration_tests | ||
| ruff_server | ||
| ruff_source_file | ||
| ruff_text_size | ||
| ruff_wasm | ||
| ruff_workspace | ||
| ty | ||
| ty_ide | ||
| ty_project | ||
| ty_python_semantic | ||
| ty_server | ||
| ty_test | ||
| ty_vendored | ||
| ty_wasm | ||