mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-28 10:50:26 +00:00
Issue: https://github.com/astral-sh/ruff/issues/19498 ## Summary [missing-required-import](https://docs.astral.sh/ruff/rules/missing-required-import/) inserts the missing import on the line immediately following the last line of the docstring. However, if the dosctring is immediately followed by a continuation token (i.e. backslash) then this leads to a syntax error because Python interprets the docstring and the inserted import to be on the same line. The proposed solution in this PR is to check if the first token after a file docstring is a continuation character, and if so, to advance an additional line before inserting the missing import. ## Test Plan Added a unit test, and the following example was verified manually: Given this simple test Python file: ```python "Hello, World!"\ print(__doc__) ``` and this ruff linting configuration in the `pyproject.toml` file: ```toml [tool.ruff.lint] select = ["I"] [tool.ruff.lint.isort] required-imports = ["import sys"] ``` Without the changes in this PR, the ruff linter would try to insert the missing import in line 2, resulting in a syntax error, and report the following: `error: Fix introduced a syntax error. Reverting all changes.` With the changes in this PR, ruff correctly advances one more line before adding the missing import, resulting in the following output: ```python "Hello, World!"\ import sys print(__doc__) ``` --------- Co-authored-by: Jim Hoekstra <jim.hoekstra@pacmed.nl> |
||
|---|---|---|
| .. | ||
| 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_static | ||
| ty_test | ||
| ty_vendored | ||
| ty_wasm | ||