-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-logging
] Add flake8_logging
boilerplate and first rule `L…
…OG009` (#7249) ## Summary Adds `LOG009` from [flake8-logging](https://github.com/adamchainz/flake8-logging). Also adds the boilerplate for a new plugin Checks for usages of undocumented `logging.WARN` constant and suggests replacement with `logging.WARNING`. ## Test Plan `cargo test` with fresh fixture ## Issue links Refers: #7248
- Loading branch information
Showing
14 changed files
with
204 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import logging | ||
|
||
logging.WARN # LOG009 | ||
logging.WARNING # OK | ||
|
||
from logging import WARN, WARNING | ||
|
||
WARN # LOG009 | ||
WARNING # OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//! Rules from [flake8-logging](https://pypi.org/project/flake8-logging/). | ||
pub(crate) mod rules; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
use test_case::test_case; | ||
|
||
use crate::assert_messages; | ||
use crate::registry::Rule; | ||
use crate::settings::Settings; | ||
use crate::test::test_path; | ||
|
||
#[test_case(Rule::UndocumentedWarn, Path::new("LOG009.py"))] | ||
fn rules(rule_code: Rule, path: &Path) -> Result<()> { | ||
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy()); | ||
let diagnostics = test_path( | ||
Path::new("flake8_logging").join(path).as_path(), | ||
&Settings::for_rule(rule_code), | ||
)?; | ||
assert_messages!(snapshot, diagnostics); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub(crate) use undocumented_warn::*; | ||
|
||
mod undocumented_warn; |
71 changes: 71 additions & 0 deletions
71
crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use ruff_python_ast::Expr; | ||
|
||
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::importer::ImportRequest; | ||
use crate::registry::AsRule; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `logging.WARN`. | ||
/// | ||
/// ## Why is this bad? | ||
/// The `logging.WARN` constant is an undocumented alias for `logging.WARNING`. | ||
/// | ||
/// Although it’s not explicitly deprecated, `logging.WARN` is not mentioned | ||
/// in the `logging` documentation. Prefer `logging.WARNING` instead. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// import logging | ||
/// | ||
/// | ||
/// logging.basicConfig(level=logging.WARN) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// import logging | ||
/// | ||
/// | ||
/// logging.basicConfig(level=logging.WARNING) | ||
/// ``` | ||
#[violation] | ||
pub struct UndocumentedWarn; | ||
|
||
impl Violation for UndocumentedWarn { | ||
const AUTOFIX: AutofixKind = AutofixKind::Sometimes; | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use of undocumented `logging.WARN` constant") | ||
} | ||
|
||
fn autofix_title(&self) -> Option<String> { | ||
Some(format!("Replace `logging.WARN` with `logging.WARNING`")) | ||
} | ||
} | ||
|
||
/// LOG009 | ||
pub(crate) fn undocumented_warn(checker: &mut Checker, expr: &Expr) { | ||
if checker | ||
.semantic() | ||
.resolve_call_path(expr) | ||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["logging", "WARN"])) | ||
{ | ||
let mut diagnostic = Diagnostic::new(UndocumentedWarn, expr.range()); | ||
if checker.patch(diagnostic.kind.rule()) { | ||
diagnostic.try_set_fix(|| { | ||
let (import_edit, binding) = checker.importer().get_or_import_symbol( | ||
&ImportRequest::import("logging", "WARNING"), | ||
expr.range().start(), | ||
checker.semantic(), | ||
)?; | ||
let reference_edit = Edit::range_replacement(binding, expr.range()); | ||
Ok(Fix::suggested_edits(import_edit, [reference_edit])) | ||
}); | ||
} | ||
checker.diagnostics.push(diagnostic); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
.../rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG009_LOG009.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_logging/mod.rs | ||
--- | ||
LOG009.py:3:1: LOG009 [*] Use of undocumented `logging.WARN` constant | ||
| | ||
1 | import logging | ||
2 | | ||
3 | logging.WARN # LOG009 | ||
| ^^^^^^^^^^^^ LOG009 | ||
4 | logging.WARNING # OK | ||
| | ||
= help: Replace `logging.WARN` with `logging.WARNING` | ||
|
||
ℹ Suggested fix | ||
1 1 | import logging | ||
2 2 | | ||
3 |-logging.WARN # LOG009 | ||
3 |+logging.WARNING # LOG009 | ||
4 4 | logging.WARNING # OK | ||
5 5 | | ||
6 6 | from logging import WARN, WARNING | ||
|
||
LOG009.py:8:1: LOG009 [*] Use of undocumented `logging.WARN` constant | ||
| | ||
6 | from logging import WARN, WARNING | ||
7 | | ||
8 | WARN # LOG009 | ||
| ^^^^ LOG009 | ||
9 | WARNING # OK | ||
| | ||
= help: Replace `logging.WARN` with `logging.WARNING` | ||
|
||
ℹ Suggested fix | ||
5 5 | | ||
6 6 | from logging import WARN, WARNING | ||
7 7 | | ||
8 |-WARN # LOG009 | ||
8 |+logging.WARNING # LOG009 | ||
9 9 | WARNING # OK | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.