From f17001b0f9b7eed1d38e38c1da63e111fbf5967a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 18 May 2023 22:47:17 +0300 Subject: [PATCH 1/3] Avoid some false positives in dunder variable assigments --- crates/ruff_python_ast/src/helpers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff_python_ast/src/helpers.rs b/crates/ruff_python_ast/src/helpers.rs index d064c8f15fb7c..5edffd441f52b 100644 --- a/crates/ruff_python_ast/src/helpers.rs +++ b/crates/ruff_python_ast/src/helpers.rs @@ -542,7 +542,7 @@ where body.iter().any(|stmt| any_over_stmt(stmt, func)) } -static DUNDER_REGEX: Lazy = Lazy::new(|| Regex::new(r"__[^\s]+__").unwrap()); +static DUNDER_REGEX: Lazy = Lazy::new(|| Regex::new(r"^__[^\s]+__$").unwrap()); /// Return `true` if the [`Stmt`] is an assignment to a dunder (like `__all__`). pub fn is_assignment_to_a_dunder(stmt: &Stmt) -> bool { From f6509836e53295455e5cb7080517c030cbb395d5 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 18 May 2023 21:48:55 -0400 Subject: [PATCH 2/3] Remove regex; add test --- .../ruff/resources/test/fixtures/pycodestyle/E402.py | 2 +- .../ruff__rules__pycodestyle__tests__E402_E402.py.snap | 2 +- crates/ruff_python_ast/src/helpers.rs | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/pycodestyle/E402.py b/crates/ruff/resources/test/fixtures/pycodestyle/E402.py index b95b2ae1e832a..81e6306c7af11 100644 --- a/crates/ruff/resources/test/fixtures/pycodestyle/E402.py +++ b/crates/ruff/resources/test/fixtures/pycodestyle/E402.py @@ -19,7 +19,7 @@ else: import e -y = x + 1 +__some__magic = 1 import f diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E402_E402.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E402_E402.py.snap index c5ab26ba38029..b2e69a17baac0 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E402_E402.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E402_E402.py.snap @@ -3,7 +3,7 @@ source: crates/ruff/src/rules/pycodestyle/mod.rs --- E402.py:24:1: E402 Module level import not at top of file | -24 | y = x + 1 +24 | __some__magic = 1 25 | 26 | import f | ^^^^^^^^ E402 diff --git a/crates/ruff_python_ast/src/helpers.rs b/crates/ruff_python_ast/src/helpers.rs index 5edffd441f52b..534e29bb9bef8 100644 --- a/crates/ruff_python_ast/src/helpers.rs +++ b/crates/ruff_python_ast/src/helpers.rs @@ -4,8 +4,6 @@ use std::path::Path; use itertools::Itertools; use log::error; use num_traits::Zero; -use once_cell::sync::Lazy; -use regex::Regex; use ruff_text_size::{TextRange, TextSize}; use rustc_hash::{FxHashMap, FxHashSet}; use rustpython_parser::ast::{ @@ -542,7 +540,9 @@ where body.iter().any(|stmt| any_over_stmt(stmt, func)) } -static DUNDER_REGEX: Lazy = Lazy::new(|| Regex::new(r"^__[^\s]+__$").unwrap()); +fn is_dunder(id: &str) -> bool { + id.starts_with("__") && id.ends_with("__") +} /// Return `true` if the [`Stmt`] is an assignment to a dunder (like `__all__`). pub fn is_assignment_to_a_dunder(stmt: &Stmt) -> bool { @@ -554,12 +554,12 @@ pub fn is_assignment_to_a_dunder(stmt: &Stmt) -> bool { return false; } match &targets[0] { - Expr::Name(ast::ExprName { id, .. }) => DUNDER_REGEX.is_match(id.as_str()), + Expr::Name(ast::ExprName { id, .. }) => is_dunder(&id), _ => false, } } Stmt::AnnAssign(ast::StmtAnnAssign { target, .. }) => match target.as_ref() { - Expr::Name(ast::ExprName { id, .. }) => DUNDER_REGEX.is_match(id.as_str()), + Expr::Name(ast::ExprName { id, .. }) => is_dunder(&id), _ => false, }, _ => false, From 10d85c6e7ca62a9f2ee1456ab3c4daffae51eebb Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 18 May 2023 22:03:40 -0400 Subject: [PATCH 3/3] Fix Clippy --- crates/ruff_python_ast/src/helpers.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/ruff_python_ast/src/helpers.rs b/crates/ruff_python_ast/src/helpers.rs index 534e29bb9bef8..d41e9ad4fb700 100644 --- a/crates/ruff_python_ast/src/helpers.rs +++ b/crates/ruff_python_ast/src/helpers.rs @@ -553,15 +553,19 @@ pub fn is_assignment_to_a_dunder(stmt: &Stmt) -> bool { if targets.len() != 1 { return false; } - match &targets[0] { - Expr::Name(ast::ExprName { id, .. }) => is_dunder(&id), - _ => false, + if let Expr::Name(ast::ExprName { id, .. }) = &targets[0] { + is_dunder(id) + } else { + false + } + } + Stmt::AnnAssign(ast::StmtAnnAssign { target, .. }) => { + if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() { + is_dunder(id) + } else { + false } } - Stmt::AnnAssign(ast::StmtAnnAssign { target, .. }) => match target.as_ref() { - Expr::Name(ast::ExprName { id, .. }) => is_dunder(&id), - _ => false, - }, _ => false, } }