[flake8-type-checking] Fix helper function which surrounds annotations in quotes (#14371)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz (push) Blocked by required conditions
CI / Fuzz the parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

This PR adds corrected handling of list expressions to the `Visitor`
implementation of `QuotedAnnotator` in `flake8_type_checking::helpers`.

Closes #14368
This commit is contained in:
Dylan 2024-11-16 11:58:02 -06:00 committed by GitHub
parent 81d3c419e9
commit fc392c663a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 3 deletions

View file

@ -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('[');
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(']');
self.state.pop();
}
Expr::BinOp(ast::ExprBinOp {
left, op, right, ..
}) => {

View file

@ -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 |