From 6f853082fde83b2ed20bdce4e09f0214f1f4b15a Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 15 Jan 2024 21:46:47 -0500 Subject: [PATCH] Fix names --- .../pygrep_hooks/rules/deprecated_log_warn.rs | 25 +++++++++++++++---- ...s__tests__preview__PGH002_PGH002_1.py.snap | 12 ++++++++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/crates/ruff_linter/src/rules/pygrep_hooks/rules/deprecated_log_warn.rs b/crates/ruff_linter/src/rules/pygrep_hooks/rules/deprecated_log_warn.rs index 83047d190ffc5..19f6df52f9986 100644 --- a/crates/ruff_linter/src/rules/pygrep_hooks/rules/deprecated_log_warn.rs +++ b/crates/ruff_linter/src/rules/pygrep_hooks/rules/deprecated_log_warn.rs @@ -6,6 +6,7 @@ use ruff_python_stdlib::logging::LoggingLevel; use ruff_text_size::Ranged; use crate::checkers::ast::Checker; +use crate::importer::ImportRequest; /// ## What it does /// Check for usages of the deprecated `warn` method from the `logging` module. @@ -81,11 +82,25 @@ pub(crate) fn deprecated_log_warn(checker: &mut Checker, call: &ast::ExprCall) { let mut diagnostic = Diagnostic::new(DeprecatedLogWarn, call.func.range()); if checker.settings.preview.is_enabled() { - if let Expr::Attribute(ast::ExprAttribute { attr, .. }) = call.func.as_ref() { - diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( - "warning".to_string(), - attr.range(), - ))); + match call.func.as_ref() { + Expr::Attribute(ast::ExprAttribute { attr, .. }) => { + diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( + "warning".to_string(), + attr.range(), + ))); + } + Expr::Name(_) => { + diagnostic.try_set_fix(|| { + let (import_edit, binding) = checker.importer().get_or_import_symbol( + &ImportRequest::import("logging", "warning"), + call.start(), + checker.semantic(), + )?; + let name_edit = Edit::range_replacement(binding, call.func.range()); + Ok(Fix::safe_edits(import_edit, [name_edit])) + }); + } + _ => {} } } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff_linter/src/rules/pygrep_hooks/snapshots/ruff_linter__rules__pygrep_hooks__tests__preview__PGH002_PGH002_1.py.snap b/crates/ruff_linter/src/rules/pygrep_hooks/snapshots/ruff_linter__rules__pygrep_hooks__tests__preview__PGH002_PGH002_1.py.snap index ac08da9a39668..6c1c5f1f712be 100644 --- a/crates/ruff_linter/src/rules/pygrep_hooks/snapshots/ruff_linter__rules__pygrep_hooks__tests__preview__PGH002_PGH002_1.py.snap +++ b/crates/ruff_linter/src/rules/pygrep_hooks/snapshots/ruff_linter__rules__pygrep_hooks__tests__preview__PGH002_PGH002_1.py.snap @@ -21,7 +21,7 @@ PGH002_1.py:4:1: PGH002 [*] `warn` is deprecated in favor of `warning` 6 6 | 7 7 | logger = logging.getLogger(__name__) -PGH002_1.py:5:1: PGH002 `warn` is deprecated in favor of `warning` +PGH002_1.py:5:1: PGH002 [*] `warn` is deprecated in favor of `warning` | 4 | logging.warn("this is not ok") 5 | warn("not ok") @@ -31,6 +31,16 @@ PGH002_1.py:5:1: PGH002 `warn` is deprecated in favor of `warning` | = help: Replace with `warning` +ℹ Safe fix +2 2 | from logging import warn +3 3 | +4 4 | logging.warn("this is not ok") +5 |-warn("not ok") + 5 |+logging.warning("not ok") +6 6 | +7 7 | logger = logging.getLogger(__name__) +8 8 | logger.warn("this is not ok") + PGH002_1.py:8:1: PGH002 [*] `warn` is deprecated in favor of `warning` | 7 | logger = logging.getLogger(__name__)