[flake8-simplify] Do not emit diagnostics for expressions inside string type annotations (SIM222, SIM223) (#15405)

## Summary

Resolves #7127.

## Test Plan

`cargo nextest run` and `cargo insta test`.
This commit is contained in:
InSync 2025-01-17 13:48:35 +07:00 committed by GitHub
parent 7ddf59be5f
commit 556116ee76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 18 additions and 0 deletions

View file

@ -193,3 +193,7 @@ for x in (*a, *b) or [None]:
for x in {**a, **b} or [None]:
pass
# https://github.com/astral-sh/ruff/issues/7127
def f(a: "'b' or 'c'"): ...

View file

@ -153,3 +153,7 @@ print(f"{a}{b}" and "bar")
print(f"{a}{''}" and "bar")
print(f"{''}{''}" and "bar")
print(f"{1}{''}" and "bar")
# https://github.com/astral-sh/ruff/issues/7127
def f(a: "'' and 'b'"): ...

View file

@ -833,6 +833,10 @@ fn is_short_circuit(
/// SIM222
pub(crate) fn expr_or_true(checker: &mut Checker, expr: &Expr) {
if checker.semantic().in_string_type_definition() {
return;
}
if let Some((edit, remove)) = is_short_circuit(expr, BoolOp::Or, checker) {
let mut diagnostic = Diagnostic::new(
ExprOrTrue {
@ -848,6 +852,10 @@ pub(crate) fn expr_or_true(checker: &mut Checker, expr: &Expr) {
/// SIM223
pub(crate) fn expr_and_false(checker: &mut Checker, expr: &Expr) {
if checker.semantic().in_string_type_definition() {
return;
}
if let Some((edit, remove)) = is_short_circuit(expr, BoolOp::And, checker) {
let mut diagnostic = Diagnostic::new(
ExprAndFalse {

View file

@ -1023,3 +1023,5 @@ SIM223.py:154:7: SIM223 [*] Use `f"{''}{''}"` instead of `f"{''}{''}" and ...`
154 |-print(f"{''}{''}" and "bar")
154 |+print(f"{''}{''}")
155 155 | print(f"{1}{''}" and "bar")
156 156 |
157 157 |