Skip to content

Commit 83b5bbf

Browse files
Treat form feed as valid whitespace before a line continuation (astral-sh#19220)
Co-authored-by: Micha Reiser <[email protected]>
1 parent 87f6f08 commit 83b5bbf

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Regression test for: https://github.com/astral-sh/ruff/issues/19175
2+
# there is a (potentially invisible) unicode formfeed character (000C) between `TYPE_CHECKING` and the backslash
3+
from typing import TYPE_CHECKING \
4+
5+
if TYPE_CHECKING: import builtins
6+
builtins.print("!")

crates/ruff_linter/src/importer/insertion.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use ruff_python_ast::Stmt;
55
use ruff_python_ast::helpers::is_docstring_stmt;
66
use ruff_python_codegen::Stylist;
77
use ruff_python_parser::{TokenKind, Tokens};
8+
use ruff_python_trivia::is_python_whitespace;
89
use ruff_python_trivia::{PythonWhitespace, textwrap::indent};
910
use ruff_source_file::{LineRanges, UniversalNewlineIterator};
1011
use ruff_text_size::{Ranged, TextSize};
@@ -306,7 +307,7 @@ fn match_semicolon(s: &str) -> Option<TextSize> {
306307
fn match_continuation(s: &str) -> Option<TextSize> {
307308
for (offset, c) in s.char_indices() {
308309
match c {
309-
' ' | '\t' => continue,
310+
_ if is_python_whitespace(c) => continue,
310311
'\\' => return Some(TextSize::try_from(offset).unwrap()),
311312
_ => break,
312313
}

crates/ruff_linter/src/rules/flake8_type_checking/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ mod tests {
3636
#[test_case(Rule::RuntimeImportInTypeCheckingBlock, Path::new("TC004_8.py"))]
3737
#[test_case(Rule::RuntimeImportInTypeCheckingBlock, Path::new("TC004_9.py"))]
3838
#[test_case(Rule::RuntimeImportInTypeCheckingBlock, Path::new("quote.py"))]
39+
#[test_case(Rule::RuntimeImportInTypeCheckingBlock, Path::new("whitespace.py"))]
3940
#[test_case(Rule::RuntimeStringUnion, Path::new("TC010_1.py"))]
4041
#[test_case(Rule::RuntimeStringUnion, Path::new("TC010_2.py"))]
4142
#[test_case(Rule::TypingOnlyFirstPartyImport, Path::new("TC001.py"))]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs
3+
---
4+
whitespace.py:5:26: TC004 [*] Move import `builtins` out of type-checking block. Import is used for more than type hinting.
5+
|
6+
3 | from typing import TYPE_CHECKING \
7+
4 |
8+
5 | if TYPE_CHECKING: import builtins
9+
| ^^^^^^^^ TC004
10+
6 | builtins.print("!")
11+
|
12+
= help: Move out of type-checking block
13+
14+
Unsafe fix
15+
1 1 | # Regression test for: https://github.com/astral-sh/ruff/issues/19175
16+
2 2 | # there is a (potentially invisible) unicode formfeed character (000C) between `TYPE_CHECKING` and the backslash
17+
3 |-from typing import TYPE_CHECKING \
18+
3 |+from typing import TYPE_CHECKING; import builtins \
19+
4 4 |
20+
5 |-if TYPE_CHECKING: import builtins
21+
5 |+if TYPE_CHECKING: pass
22+
6 6 | builtins.print("!")

0 commit comments

Comments
 (0)