Skip to content

Commit

Permalink
[flake8-simplify] Do not emit diagnostics for expressions inside st…
Browse files Browse the repository at this point in the history
…ring type annotations (`SIM222`, `SIM223`) (#15405)

## Summary

Resolves #7127.

## Test Plan

`cargo nextest run` and `cargo insta test`.
  • Loading branch information
InSyncWithFoo authored Jan 17, 2025
1 parent 7ddf59b commit 556116e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,7 @@ def secondToTime(s0: int) -> ((int, int, int) or str):

for x in {**a, **b} or [None]:
pass


# https://github.com/astral-sh/ruff/issues/7127
def f(a: "'b' or 'c'"): ...
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,7 @@
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'"): ...
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

0 comments on commit 556116e

Please sign in to comment.