From 276f3a08efb220672a82d573f73cb171854e29c8 Mon Sep 17 00:00:00 2001 From: Sawbez Date: Sat, 24 Dec 2022 10:04:51 -0800 Subject: [PATCH 1/2] Add autofix, update insta tests, update README. --- README.md | 2 +- src/checkers/tokens.rs | 6 ++- src/checks.rs | 1 + src/pycodestyle/.checks.rs.swp | Bin 0 -> 1024 bytes src/pycodestyle/checks.rs | 9 ++++- ...f__pycodestyle__tests__W605_W605_0.py.snap | 36 ++++++++++++++++-- ...f__pycodestyle__tests__W605_W605_1.py.snap | 36 ++++++++++++++++-- 7 files changed, 78 insertions(+), 12 deletions(-) create mode 100644 src/pycodestyle/.checks.rs.swp diff --git a/README.md b/README.md index e3eb544ccdf79..79cf612584cf0 100644 --- a/README.md +++ b/README.md @@ -541,7 +541,7 @@ For more, see [pycodestyle](https://pypi.org/project/pycodestyle/2.9.1/) on PyPI | E902 | IOError | IOError: `...` | | | E999 | SyntaxError | SyntaxError: `...` | | | W292 | NoNewLineAtEndOfFile | No newline at end of file | 🛠 | -| W605 | InvalidEscapeSequence | Invalid escape sequence: '\c' | | +| W605 | InvalidEscapeSequence | Invalid escape sequence: '\c' | 🛠 | ### mccabe (C90) diff --git a/src/checkers/tokens.rs b/src/checkers/tokens.rs index 365babadc52be..eacf330715895 100644 --- a/src/checkers/tokens.rs +++ b/src/checkers/tokens.rs @@ -89,7 +89,11 @@ pub fn check_tokens( if enforce_invalid_escape_sequence { if matches!(tok, Tok::String { .. }) { checks.extend(pycodestyle::checks::invalid_escape_sequence( - locator, start, end, + locator, + start, + end, + matches!(autofix, flags::Autofix::Enabled) + && settings.fixable.contains(&CheckCode::W605), )); } } diff --git a/src/checks.rs b/src/checks.rs index cb4e4289d68ea..0625dd5a78f68 100644 --- a/src/checks.rs +++ b/src/checks.rs @@ -2972,6 +2972,7 @@ impl CheckKind { | CheckKind::GetAttrWithConstant | CheckKind::ImplicitReturn | CheckKind::ImplicitReturnValue + | CheckKind::InvalidEscapeSequence(..) | CheckKind::IsLiteral | CheckKind::KeyInDict(..) | CheckKind::MisplacedComparisonConstant(..) diff --git a/src/pycodestyle/.checks.rs.swp b/src/pycodestyle/.checks.rs.swp new file mode 100644 index 0000000000000000000000000000000000000000..3ce8d7db827d5a168345f2635fec9e7096b9036b GIT binary patch literal 1024 zcmYc?$V<%2S1{8vVL$;2D;XHdlJYA`i&KkGB(QKolS=bSO0h_w2_ Vec { let mut checks = vec![]; @@ -235,13 +236,17 @@ pub fn invalid_escape_sequence( }; let location = Location::new(start.row() + row_offset, col); let end_location = Location::new(location.row(), location.column() + 2); - checks.push(Check::new( + let mut check = Check::new( CheckKind::InvalidEscapeSequence(next_char), Range { location, end_location, }, - )); + ); + if autofix { + check.amend(Fix::insertion(r"\".to_string(), location)); + } + checks.push(check); } } } diff --git a/src/pycodestyle/snapshots/ruff__pycodestyle__tests__W605_W605_0.py.snap b/src/pycodestyle/snapshots/ruff__pycodestyle__tests__W605_W605_0.py.snap index d095a040ff52a..88d8e8a7ae624 100644 --- a/src/pycodestyle/snapshots/ruff__pycodestyle__tests__W605_W605_0.py.snap +++ b/src/pycodestyle/snapshots/ruff__pycodestyle__tests__W605_W605_0.py.snap @@ -10,7 +10,14 @@ expression: checks end_location: row: 2 column: 11 - fix: ~ + fix: + content: "\\" + location: + row: 2 + column: 9 + end_location: + row: 2 + column: 9 - kind: InvalidEscapeSequence: "." location: @@ -19,7 +26,14 @@ expression: checks end_location: row: 6 column: 2 - fix: ~ + fix: + content: "\\" + location: + row: 6 + column: 0 + end_location: + row: 6 + column: 0 - kind: InvalidEscapeSequence: _ location: @@ -28,7 +42,14 @@ expression: checks end_location: row: 11 column: 7 - fix: ~ + fix: + content: "\\" + location: + row: 11 + column: 5 + end_location: + row: 11 + column: 5 - kind: InvalidEscapeSequence: _ location: @@ -37,5 +58,12 @@ expression: checks end_location: row: 18 column: 7 - fix: ~ + fix: + content: "\\" + location: + row: 18 + column: 5 + end_location: + row: 18 + column: 5 diff --git a/src/pycodestyle/snapshots/ruff__pycodestyle__tests__W605_W605_1.py.snap b/src/pycodestyle/snapshots/ruff__pycodestyle__tests__W605_W605_1.py.snap index d095a040ff52a..88d8e8a7ae624 100644 --- a/src/pycodestyle/snapshots/ruff__pycodestyle__tests__W605_W605_1.py.snap +++ b/src/pycodestyle/snapshots/ruff__pycodestyle__tests__W605_W605_1.py.snap @@ -10,7 +10,14 @@ expression: checks end_location: row: 2 column: 11 - fix: ~ + fix: + content: "\\" + location: + row: 2 + column: 9 + end_location: + row: 2 + column: 9 - kind: InvalidEscapeSequence: "." location: @@ -19,7 +26,14 @@ expression: checks end_location: row: 6 column: 2 - fix: ~ + fix: + content: "\\" + location: + row: 6 + column: 0 + end_location: + row: 6 + column: 0 - kind: InvalidEscapeSequence: _ location: @@ -28,7 +42,14 @@ expression: checks end_location: row: 11 column: 7 - fix: ~ + fix: + content: "\\" + location: + row: 11 + column: 5 + end_location: + row: 11 + column: 5 - kind: InvalidEscapeSequence: _ location: @@ -37,5 +58,12 @@ expression: checks end_location: row: 18 column: 7 - fix: ~ + fix: + content: "\\" + location: + row: 18 + column: 5 + end_location: + row: 18 + column: 5 From 40c90ad45e3d855d0146697f2eb25a3a644a79c6 Mon Sep 17 00:00:00 2001 From: Sawbez <63512091+Sawbez@users.noreply.github.com> Date: Sat, 24 Dec 2022 10:10:10 -0800 Subject: [PATCH 2/2] Delete leftover file again --- src/pycodestyle/.checks.rs.swp | Bin 1024 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/pycodestyle/.checks.rs.swp diff --git a/src/pycodestyle/.checks.rs.swp b/src/pycodestyle/.checks.rs.swp deleted file mode 100644 index 3ce8d7db827d5a168345f2635fec9e7096b9036b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1024 zcmYc?$V<%2S1{8vVL$;2D;XHdlJYA`i&KkGB(QKolS=bSO0h_w2_