Skip to content

Commit

Permalink
Fix names
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 16, 2024
1 parent 9229004 commit 6f85308
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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__)
Expand Down

0 comments on commit 6f85308

Please sign in to comment.