From 512dce65e908854c8e98fcaa4807e417b46ea3d8 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Fri, 15 Nov 2024 16:04:29 -0600 Subject: [PATCH 1/3] handle lists in `QuoteAnnotator`'s `Visitor` implementation --- .../src/rules/flake8_type_checking/helpers.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs b/crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs index 66e4400715cbe..a946d7ef5c37a 100644 --- a/crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs +++ b/crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs @@ -392,6 +392,24 @@ impl<'a> SourceOrderVisitor<'a> for QuoteAnnotator<'a> { } self.state.pop(); } + Expr::List(ast::ExprList { elts, .. }) => { + let Some((first, remaining)) = elts.split_first() else { + return; + }; + self.annotation.push_str("["); + self.visit_expr(first); + if let Some(last) = self.state.last_mut() { + if *last == QuoteAnnotatorState::AnnotatedFirst { + *last = QuoteAnnotatorState::AnnotatedRest; + } + } + for expr in remaining { + self.annotation.push_str(", "); + self.visit_expr(expr); + } + self.annotation.push_str("]"); + self.state.pop(); + } Expr::BinOp(ast::ExprBinOp { left, op, right, .. }) => { From b5b045f01acd919c6e6a83a932fb179f2a9354a5 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Fri, 15 Nov 2024 16:05:44 -0600 Subject: [PATCH 2/3] update snapshot --- ...ests__quote_typing-only-third-party-import_quote3.py.snap | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__quote_typing-only-third-party-import_quote3.py.snap b/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__quote_typing-only-third-party-import_quote3.py.snap index 2e7b3d46cd4f0..1f51c7afe8cbe 100644 --- a/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__quote_typing-only-third-party-import_quote3.py.snap +++ b/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__quote_typing-only-third-party-import_quote3.py.snap @@ -1,6 +1,5 @@ --- source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs -snapshot_kind: text --- quote3.py:4:44: TCH002 [*] Move third-party import `django.contrib.auth.models.AbstractBaseUser` into a type-checking block | @@ -55,7 +54,7 @@ quote3.py:13:44: TCH002 [*] Move third-party import `django.contrib.auth.models. 13 |- from django.contrib.auth.models import AbstractBaseUser 14 17 | 15 |- def test_callable_literal_mixed_quotes(callable_fn: AbstractBaseUser[Callable[["int", Literal['admin', "user"]], 'bool']]): - 18 |+ def test_callable_literal_mixed_quotes(callable_fn: 'AbstractBaseUser[Callable[[int, Literal[admin, user]], bool]]'): + 18 |+ def test_callable_literal_mixed_quotes(callable_fn: 'AbstractBaseUser[Callable[[int, Literal["admin", "user"]], bool]]'): 16 19 | pass 17 20 | 18 21 | @@ -86,7 +85,7 @@ quote3.py:22:44: TCH002 [*] Move third-party import `django.contrib.auth.models. 22 |- from django.contrib.auth.models import AbstractBaseUser 23 26 | 24 |- def test_callable_annotated_literal(callable_fn: AbstractBaseUser[Callable[[int, Annotated[str, Literal['active', "inactive"]]], bool]]): - 27 |+ def test_callable_annotated_literal(callable_fn: 'AbstractBaseUser[Callable[[int, Annotated[str, Literal[active, inactive]]], bool]]'): + 27 |+ def test_callable_annotated_literal(callable_fn: 'AbstractBaseUser[Callable[[int, Annotated[str, Literal["active", "inactive"]]], bool]]'): 25 28 | pass 26 29 | 27 30 | From 840b911c6cc7ed35ac1a21692d154df72dd97439 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Fri, 15 Nov 2024 19:39:59 -0600 Subject: [PATCH 3/3] clippy --- crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs b/crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs index a946d7ef5c37a..6793becbd1d04 100644 --- a/crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs +++ b/crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs @@ -396,7 +396,7 @@ impl<'a> SourceOrderVisitor<'a> for QuoteAnnotator<'a> { let Some((first, remaining)) = elts.split_first() else { return; }; - self.annotation.push_str("["); + self.annotation.push('['); self.visit_expr(first); if let Some(last) = self.state.last_mut() { if *last == QuoteAnnotatorState::AnnotatedFirst { @@ -407,7 +407,7 @@ impl<'a> SourceOrderVisitor<'a> for QuoteAnnotator<'a> { self.annotation.push_str(", "); self.visit_expr(expr); } - self.annotation.push_str("]"); + self.annotation.push(']'); self.state.pop(); } Expr::BinOp(ast::ExprBinOp {