From 8d0b33c555b40b5b0e101ef1e922e11c821ddf6c Mon Sep 17 00:00:00 2001 From: qdegraaf Date: Sat, 9 Sep 2023 00:47:16 +0200 Subject: [PATCH 1/7] Add LOG009 and flake8_logging boilerplate --- LICENSE | 25 ++++++ README.md | 1 + .../test/fixtures/flake8_logging/LOG009.py | 26 ++++++ .../src/checkers/ast/analyze/expression.rs | 14 ++- crates/ruff/src/codes.rs | 3 + crates/ruff/src/registry.rs | 3 + crates/ruff/src/rules/flake8_logging/mod.rs | 26 ++++++ .../src/rules/flake8_logging/rules/mod.rs | 3 + .../flake8_logging/rules/undocumented_warn.rs | 62 +++++++++++++ ...ake8_logging__tests__LOG009_LOG009.py.snap | 86 +++++++++++++++++++ crates/ruff/src/rules/mod.rs | 1 + ruff.schema.json | 2 + 12 files changed, 248 insertions(+), 4 deletions(-) create mode 100644 crates/ruff/resources/test/fixtures/flake8_logging/LOG009.py create mode 100644 crates/ruff/src/rules/flake8_logging/mod.rs create mode 100644 crates/ruff/src/rules/flake8_logging/rules/mod.rs create mode 100644 crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs create mode 100644 crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG009_LOG009.py.snap diff --git a/LICENSE b/LICENSE index 8ffd09c5f2b5c..2c1263ea5c7f9 100644 --- a/LICENSE +++ b/LICENSE @@ -1224,6 +1224,31 @@ are: SOFTWARE. """ +- flake8-logging, license as follows: + """ + MIT License + + Copyright (c) 2023 Adam Johnson + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + """ + - Pyright, licensed as follows: """ MIT License diff --git a/README.md b/README.md index e7dd44d3bc1ba..2f3aa2fcebe36 100644 --- a/README.md +++ b/README.md @@ -274,6 +274,7 @@ quality tools, including: - [flake8-gettext](https://pypi.org/project/flake8-gettext/) - [flake8-implicit-str-concat](https://pypi.org/project/flake8-implicit-str-concat/) - [flake8-import-conventions](https://github.com/joaopalmeiro/flake8-import-conventions) +- [flake8-logging](https://pypi.org/project/flake8-logging/) - [flake8-logging-format](https://pypi.org/project/flake8-logging-format/) - [flake8-no-pep420](https://pypi.org/project/flake8-no-pep420) - [flake8-pie](https://pypi.org/project/flake8-pie/) diff --git a/crates/ruff/resources/test/fixtures/flake8_logging/LOG009.py b/crates/ruff/resources/test/fixtures/flake8_logging/LOG009.py new file mode 100644 index 0000000000000..e3c8519dcafd0 --- /dev/null +++ b/crates/ruff/resources/test/fixtures/flake8_logging/LOG009.py @@ -0,0 +1,26 @@ +import logging +from logging import WARN, WARNING + + +logging.WARN # LOG009 + + +logging.WARNING # OK + + +WARN # LOG009 + + +WARNING # OK + + +logging.basicConfig(level=logging.WARN) # LOG009 + + +logging.basicConfig(level=logging.WARNING) # OK + + +x = logging.WARN # LOG009 + + +y = WARN # LOG009 diff --git a/crates/ruff/src/checkers/ast/analyze/expression.rs b/crates/ruff/src/checkers/ast/analyze/expression.rs index 97d593de0620a..0f3b756b73096 100644 --- a/crates/ruff/src/checkers/ast/analyze/expression.rs +++ b/crates/ruff/src/checkers/ast/analyze/expression.rs @@ -13,10 +13,10 @@ use crate::registry::Rule; use crate::rules::{ flake8_2020, flake8_async, flake8_bandit, flake8_boolean_trap, flake8_bugbear, flake8_builtins, flake8_comprehensions, flake8_datetimez, flake8_debugger, flake8_django, - flake8_future_annotations, flake8_gettext, flake8_implicit_str_concat, flake8_logging_format, - flake8_pie, flake8_print, flake8_pyi, flake8_pytest_style, flake8_self, flake8_simplify, - flake8_tidy_imports, flake8_use_pathlib, flynt, numpy, pandas_vet, pep8_naming, pycodestyle, - pyflakes, pygrep_hooks, pylint, pyupgrade, ruff, + flake8_future_annotations, flake8_gettext, flake8_implicit_str_concat, flake8_logging, + flake8_logging_format, flake8_pie, flake8_print, flake8_pyi, flake8_pytest_style, flake8_self, + flake8_simplify, flake8_tidy_imports, flake8_use_pathlib, flynt, numpy, pandas_vet, + pep8_naming, pycodestyle, pyflakes, pygrep_hooks, pylint, pyupgrade, ruff, }; use crate::settings::types::PythonVersion; @@ -258,6 +258,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) { if checker.enabled(Rule::SixPY3) { flake8_2020::rules::name_or_attribute(checker, expr); } + if checker.enabled(Rule::UndocumentedWarn) { + flake8_logging::rules::undocumented_warn(checker, expr); + } if checker.enabled(Rule::LoadBeforeGlobalDeclaration) { pylint::rules::load_before_global_declaration(checker, id, expr); } @@ -324,6 +327,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) { if checker.enabled(Rule::CollectionsNamedTuple) { flake8_pyi::rules::collections_named_tuple(checker, expr); } + if checker.enabled(Rule::UndocumentedWarn) { + flake8_logging::rules::undocumented_warn(checker, expr); + } pandas_vet::rules::attr(checker, attribute); } Expr::Call( diff --git a/crates/ruff/src/codes.rs b/crates/ruff/src/codes.rs index 8c9cbf416fa17..461f4f720e124 100644 --- a/crates/ruff/src/codes.rs +++ b/crates/ruff/src/codes.rs @@ -870,6 +870,9 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> { (Refurb, "131") => (RuleGroup::Nursery, rules::refurb::rules::DeleteFullSlice), (Refurb, "132") => (RuleGroup::Nursery, rules::refurb::rules::CheckAndRemoveFromSet), + // flake8-logging + (Flake8Logging, "009") => (RuleGroup::Nursery, rules::flake8_logging::rules::UndocumentedWarn), + _ => return None, }) } diff --git a/crates/ruff/src/registry.rs b/crates/ruff/src/registry.rs index 23b54dc9b25fd..55f128b38d0e1 100644 --- a/crates/ruff/src/registry.rs +++ b/crates/ruff/src/registry.rs @@ -199,6 +199,9 @@ pub enum Linter { /// [refurb](https://pypi.org/project/refurb/) #[prefix = "FURB"] Refurb, + /// [flake8-logging](https://pypi.org/project/flake8-logging/) + #[prefix = "LOG"] + Flake8Logging, /// Ruff-specific rules #[prefix = "RUF"] Ruff, diff --git a/crates/ruff/src/rules/flake8_logging/mod.rs b/crates/ruff/src/rules/flake8_logging/mod.rs new file mode 100644 index 0000000000000..05700213f993f --- /dev/null +++ b/crates/ruff/src/rules/flake8_logging/mod.rs @@ -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(()) + } +} diff --git a/crates/ruff/src/rules/flake8_logging/rules/mod.rs b/crates/ruff/src/rules/flake8_logging/rules/mod.rs new file mode 100644 index 0000000000000..07b712a4f4239 --- /dev/null +++ b/crates/ruff/src/rules/flake8_logging/rules/mod.rs @@ -0,0 +1,3 @@ +pub(crate) use undocumented_warn::*; + +mod undocumented_warn; diff --git a/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs b/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs new file mode 100644 index 0000000000000..914cf2feffede --- /dev/null +++ b/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs @@ -0,0 +1,62 @@ +use ruff_python_ast::Expr; + +use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; +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 `WARN` +/// +/// ## Why is this bad? +/// The WARN constant is an undocumented alias for WARNING. Whilst it’s not deprecated, it’s not +/// mentioned at all in the documentation, so the documented WARNING should always be used instead. +/// +/// ## Example +/// ```python +/// logging.basicConfig(level=logging.WARN) +/// ``` +/// +/// Use instead: +/// ```python +/// logging.basicConfig(level=logging.WARNING) +/// ``` +#[violation] +pub struct UndocumentedWarn; + +impl AlwaysAutofixableViolation for UndocumentedWarn { + #[derive_message_formats] + fn message(&self) -> String { + format!("Use of undocumented logging.WARN constant") + } + + fn autofix_title(&self) -> String { + 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); + } +} diff --git a/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG009_LOG009.py.snap b/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG009_LOG009.py.snap new file mode 100644 index 0000000000000..a1fb53819a183 --- /dev/null +++ b/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG009_LOG009.py.snap @@ -0,0 +1,86 @@ +--- +source: crates/ruff/src/rules/flake8_logging/mod.rs +--- +LOG009.py:5:1: LOG009 [*] Use of undocumented logging.WARN constant + | +5 | logging.WARN # LOG009 + | ^^^^^^^^^^^^ LOG009 + | + = help: Replace logging.WARN with logging.WARNING + +ℹ Suggested fix +2 2 | from logging import WARN, WARNING +3 3 | +4 4 | +5 |-logging.WARN # LOG009 + 5 |+logging.WARNING # LOG009 +6 6 | +7 7 | +8 8 | logging.WARNING # OK + +LOG009.py:11:1: LOG009 [*] Use of undocumented logging.WARN constant + | +11 | WARN # LOG009 + | ^^^^ LOG009 + | + = help: Replace logging.WARN with logging.WARNING + +ℹ Suggested fix +8 8 | logging.WARNING # OK +9 9 | +10 10 | +11 |-WARN # LOG009 + 11 |+logging.WARNING # LOG009 +12 12 | +13 13 | +14 14 | WARNING # OK + +LOG009.py:17:27: LOG009 [*] Use of undocumented logging.WARN constant + | +17 | logging.basicConfig(level=logging.WARN) # LOG009 + | ^^^^^^^^^^^^ LOG009 + | + = help: Replace logging.WARN with logging.WARNING + +ℹ Suggested fix +14 14 | WARNING # OK +15 15 | +16 16 | +17 |-logging.basicConfig(level=logging.WARN) # LOG009 + 17 |+logging.basicConfig(level=logging.WARNING) # LOG009 +18 18 | +19 19 | +20 20 | logging.basicConfig(level=logging.WARNING) # OK + +LOG009.py:23:5: LOG009 [*] Use of undocumented logging.WARN constant + | +23 | x = logging.WARN # LOG009 + | ^^^^^^^^^^^^ LOG009 + | + = help: Replace logging.WARN with logging.WARNING + +ℹ Suggested fix +20 20 | logging.basicConfig(level=logging.WARNING) # OK +21 21 | +22 22 | +23 |-x = logging.WARN # LOG009 + 23 |+x = logging.WARNING # LOG009 +24 24 | +25 25 | +26 26 | y = WARN # LOG009 + +LOG009.py:26:5: LOG009 [*] Use of undocumented logging.WARN constant + | +26 | y = WARN # LOG009 + | ^^^^ LOG009 + | + = help: Replace logging.WARN with logging.WARNING + +ℹ Suggested fix +23 23 | x = logging.WARN # LOG009 +24 24 | +25 25 | +26 |-y = WARN # LOG009 + 26 |+y = logging.WARNING # LOG009 + + diff --git a/crates/ruff/src/rules/mod.rs b/crates/ruff/src/rules/mod.rs index 225acb00e9e77..6240d93d12719 100644 --- a/crates/ruff/src/rules/mod.rs +++ b/crates/ruff/src/rules/mod.rs @@ -22,6 +22,7 @@ pub mod flake8_future_annotations; pub mod flake8_gettext; pub mod flake8_implicit_str_concat; pub mod flake8_import_conventions; +pub mod flake8_logging; pub mod flake8_logging_format; pub mod flake8_no_pep420; pub mod flake8_pie; diff --git a/ruff.schema.json b/ruff.schema.json index 855add9dbc6b5..11e692634832f 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -2107,6 +2107,8 @@ "ISC001", "ISC002", "ISC003", + "LOG", + "LOG009", "N", "N8", "N80", From 40f12ded8e25a53fb0e88de0561c7de6ca133479 Mon Sep 17 00:00:00 2001 From: qdegraaf Date: Sat, 9 Sep 2023 00:59:31 +0200 Subject: [PATCH 2/7] Change to AutofixKind::Sometimes --- .../src/rules/flake8_logging/rules/undocumented_warn.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs b/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs index 914cf2feffede..3b14cacaa6850 100644 --- a/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs +++ b/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs @@ -1,6 +1,6 @@ use ruff_python_ast::Expr; -use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; +use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_text_size::Ranged; @@ -27,14 +27,15 @@ use crate::registry::AsRule; #[violation] pub struct UndocumentedWarn; -impl AlwaysAutofixableViolation for 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) -> String { - format!("Replace logging.WARN with logging.WARNING") + fn autofix_title(&self) -> Option { + Some(format!("Replace logging.WARN with logging.WARNING")) } } From 1dea2496da1f5690b32f2079e3ce7dae56526616 Mon Sep 17 00:00:00 2001 From: qdegraaf Date: Mon, 11 Sep 2023 09:47:51 +0200 Subject: [PATCH 3/7] Fix license, docstring, violations strings --- LICENSE | 2 +- .../src/rules/flake8_logging/rules/undocumented_warn.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/LICENSE b/LICENSE index 2c1263ea5c7f9..7711c47e852c1 100644 --- a/LICENSE +++ b/LICENSE @@ -1224,7 +1224,7 @@ are: SOFTWARE. """ -- flake8-logging, license as follows: +- flake8-logging, licensed as follows: """ MIT License diff --git a/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs b/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs index 3b14cacaa6850..b2f350df023c4 100644 --- a/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs +++ b/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs @@ -9,7 +9,7 @@ use crate::importer::ImportRequest; use crate::registry::AsRule; /// ## What it does -/// Checks for uses of `WARN` +/// Checks for uses of `logging.WARN`. /// /// ## Why is this bad? /// The WARN constant is an undocumented alias for WARNING. Whilst it’s not deprecated, it’s not @@ -31,11 +31,11 @@ impl Violation for UndocumentedWarn { const AUTOFIX: AutofixKind = AutofixKind::Sometimes; #[derive_message_formats] fn message(&self) -> String { - format!("Use of undocumented logging.WARN constant") + format!("Use of undocumented `logging.WARN` constant") } fn autofix_title(&self) -> Option { - Some(format!("Replace logging.WARN with logging.WARNING")) + Some(format!("Replace `logging.WARN` with `logging.WARNING`")) } } From a9c8d3c9c43db68c7f1769fb28038d5daed84a83 Mon Sep 17 00:00:00 2001 From: qdegraaf Date: Mon, 11 Sep 2023 12:13:20 +0200 Subject: [PATCH 4/7] Regenerate snapshots --- ...ake8_logging__tests__LOG009_LOG009.py.snap | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG009_LOG009.py.snap b/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG009_LOG009.py.snap index a1fb53819a183..7c896d7b463d2 100644 --- a/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG009_LOG009.py.snap +++ b/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG009_LOG009.py.snap @@ -1,12 +1,12 @@ --- source: crates/ruff/src/rules/flake8_logging/mod.rs --- -LOG009.py:5:1: LOG009 [*] Use of undocumented logging.WARN constant +LOG009.py:5:1: LOG009 [*] Use of undocumented `logging.WARN` constant | 5 | logging.WARN # LOG009 | ^^^^^^^^^^^^ LOG009 | - = help: Replace logging.WARN with logging.WARNING + = help: Replace `logging.WARN` with `logging.WARNING` ℹ Suggested fix 2 2 | from logging import WARN, WARNING @@ -18,12 +18,12 @@ LOG009.py:5:1: LOG009 [*] Use of undocumented logging.WARN constant 7 7 | 8 8 | logging.WARNING # OK -LOG009.py:11:1: LOG009 [*] Use of undocumented logging.WARN constant +LOG009.py:11:1: LOG009 [*] Use of undocumented `logging.WARN` constant | 11 | WARN # LOG009 | ^^^^ LOG009 | - = help: Replace logging.WARN with logging.WARNING + = help: Replace `logging.WARN` with `logging.WARNING` ℹ Suggested fix 8 8 | logging.WARNING # OK @@ -35,12 +35,12 @@ LOG009.py:11:1: LOG009 [*] Use of undocumented logging.WARN constant 13 13 | 14 14 | WARNING # OK -LOG009.py:17:27: LOG009 [*] Use of undocumented logging.WARN constant +LOG009.py:17:27: LOG009 [*] Use of undocumented `logging.WARN` constant | 17 | logging.basicConfig(level=logging.WARN) # LOG009 | ^^^^^^^^^^^^ LOG009 | - = help: Replace logging.WARN with logging.WARNING + = help: Replace `logging.WARN` with `logging.WARNING` ℹ Suggested fix 14 14 | WARNING # OK @@ -52,12 +52,12 @@ LOG009.py:17:27: LOG009 [*] Use of undocumented logging.WARN constant 19 19 | 20 20 | logging.basicConfig(level=logging.WARNING) # OK -LOG009.py:23:5: LOG009 [*] Use of undocumented logging.WARN constant +LOG009.py:23:5: LOG009 [*] Use of undocumented `logging.WARN` constant | 23 | x = logging.WARN # LOG009 | ^^^^^^^^^^^^ LOG009 | - = help: Replace logging.WARN with logging.WARNING + = help: Replace `logging.WARN` with `logging.WARNING` ℹ Suggested fix 20 20 | logging.basicConfig(level=logging.WARNING) # OK @@ -69,12 +69,12 @@ LOG009.py:23:5: LOG009 [*] Use of undocumented logging.WARN constant 25 25 | 26 26 | y = WARN # LOG009 -LOG009.py:26:5: LOG009 [*] Use of undocumented logging.WARN constant +LOG009.py:26:5: LOG009 [*] Use of undocumented `logging.WARN` constant | 26 | y = WARN # LOG009 | ^^^^ LOG009 | - = help: Replace logging.WARN with logging.WARNING + = help: Replace `logging.WARN` with `logging.WARNING` ℹ Suggested fix 23 23 | x = logging.WARN # LOG009 From 1ea71f06a9493a6dd65f7035fb4d61bd75fdc362 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 14 Sep 2023 21:25:37 -0400 Subject: [PATCH 5/7] Refine docs --- .../test/fixtures/flake8_logging/LOG009.py | 19 +--- crates/ruff/src/codes.rs | 2 +- .../flake8_logging/rules/undocumented_warn.rs | 12 ++- ...ake8_logging__tests__LOG009_LOG009.py.snap | 101 +++++------------- 4 files changed, 40 insertions(+), 94 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/flake8_logging/LOG009.py b/crates/ruff/resources/test/fixtures/flake8_logging/LOG009.py index e3c8519dcafd0..9740486265e72 100644 --- a/crates/ruff/resources/test/fixtures/flake8_logging/LOG009.py +++ b/crates/ruff/resources/test/fixtures/flake8_logging/LOG009.py @@ -1,26 +1,9 @@ import logging -from logging import WARN, WARNING - logging.WARN # LOG009 - - logging.WARNING # OK +from logging import WARN, WARNING WARN # LOG009 - - WARNING # OK - - -logging.basicConfig(level=logging.WARN) # LOG009 - - -logging.basicConfig(level=logging.WARNING) # OK - - -x = logging.WARN # LOG009 - - -y = WARN # LOG009 diff --git a/crates/ruff/src/codes.rs b/crates/ruff/src/codes.rs index 461f4f720e124..ab4259003f522 100644 --- a/crates/ruff/src/codes.rs +++ b/crates/ruff/src/codes.rs @@ -871,7 +871,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> { (Refurb, "132") => (RuleGroup::Nursery, rules::refurb::rules::CheckAndRemoveFromSet), // flake8-logging - (Flake8Logging, "009") => (RuleGroup::Nursery, rules::flake8_logging::rules::UndocumentedWarn), + (Flake8Logging, "009") => (RuleGroup::Preview, rules::flake8_logging::rules::UndocumentedWarn), _ => return None, }) diff --git a/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs b/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs index b2f350df023c4..abbdf81c38ab1 100644 --- a/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs +++ b/crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs @@ -12,16 +12,24 @@ use crate::registry::AsRule; /// Checks for uses of `logging.WARN`. /// /// ## Why is this bad? -/// The WARN constant is an undocumented alias for WARNING. Whilst it’s not deprecated, it’s not -/// mentioned at all in the documentation, so the documented WARNING should always be used instead. +/// 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] diff --git a/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG009_LOG009.py.snap b/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG009_LOG009.py.snap index 7c896d7b463d2..3332f318d569b 100644 --- a/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG009_LOG009.py.snap +++ b/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG009_LOG009.py.snap @@ -1,86 +1,41 @@ --- source: crates/ruff/src/rules/flake8_logging/mod.rs --- -LOG009.py:5:1: LOG009 [*] Use of undocumented `logging.WARN` constant +LOG009.py:3:1: LOG009 [*] Use of undocumented `logging.WARN` constant | -5 | logging.WARN # LOG009 +1 | import logging +2 | +3 | logging.WARN # LOG009 | ^^^^^^^^^^^^ LOG009 +4 | logging.WARNING # OK | = help: Replace `logging.WARN` with `logging.WARNING` ℹ Suggested fix -2 2 | from logging import WARN, WARNING -3 3 | -4 4 | -5 |-logging.WARN # LOG009 - 5 |+logging.WARNING # LOG009 -6 6 | -7 7 | -8 8 | logging.WARNING # OK - -LOG009.py:11:1: LOG009 [*] Use of undocumented `logging.WARN` constant - | -11 | WARN # LOG009 - | ^^^^ LOG009 - | - = help: Replace `logging.WARN` with `logging.WARNING` - -ℹ Suggested fix -8 8 | logging.WARNING # OK -9 9 | -10 10 | -11 |-WARN # LOG009 - 11 |+logging.WARNING # LOG009 -12 12 | -13 13 | -14 14 | WARNING # OK - -LOG009.py:17:27: LOG009 [*] Use of undocumented `logging.WARN` constant - | -17 | logging.basicConfig(level=logging.WARN) # LOG009 - | ^^^^^^^^^^^^ LOG009 - | - = help: Replace `logging.WARN` with `logging.WARNING` - -ℹ Suggested fix -14 14 | WARNING # OK -15 15 | -16 16 | -17 |-logging.basicConfig(level=logging.WARN) # LOG009 - 17 |+logging.basicConfig(level=logging.WARNING) # LOG009 -18 18 | -19 19 | -20 20 | logging.basicConfig(level=logging.WARNING) # OK - -LOG009.py:23:5: LOG009 [*] Use of undocumented `logging.WARN` constant - | -23 | x = logging.WARN # LOG009 - | ^^^^^^^^^^^^ LOG009 - | - = help: Replace `logging.WARN` with `logging.WARNING` - -ℹ Suggested fix -20 20 | logging.basicConfig(level=logging.WARNING) # OK -21 21 | -22 22 | -23 |-x = logging.WARN # LOG009 - 23 |+x = logging.WARNING # LOG009 -24 24 | -25 25 | -26 26 | y = WARN # LOG009 - -LOG009.py:26:5: LOG009 [*] Use of undocumented `logging.WARN` constant - | -26 | y = WARN # LOG009 - | ^^^^ LOG009 - | - = help: Replace `logging.WARN` with `logging.WARNING` +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 -23 23 | x = logging.WARN # LOG009 -24 24 | -25 25 | -26 |-y = WARN # LOG009 - 26 |+y = logging.WARNING # LOG009 +5 5 | +6 6 | from logging import WARN, WARNING +7 7 | +8 |-WARN # LOG009 + 8 |+logging.WARNING # LOG009 +9 9 | WARNING # OK From 5a53da52330beff5d7328b4b58e40de3113ba97f Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 14 Sep 2023 21:28:35 -0400 Subject: [PATCH 6/7] Add as preview --- crates/ruff_workspace/src/configuration.rs | 6 +++++- ruff.schema.json | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index 8e98012327659..0f7cc75e8e269 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -852,7 +852,11 @@ mod tests { Rule::QuadraticListSummation, ]; - const PREVIEW_RULES: &[Rule] = &[Rule::TooManyPublicMethods, Rule::SliceCopy]; + const PREVIEW_RULES: &[Rule] = &[ + Rule::TooManyPublicMethods, + Rule::SliceCopy, + Rule::UndocumentedWarn, + ]; #[allow(clippy::needless_pass_by_value)] fn resolve_rules( diff --git a/ruff.schema.json b/ruff.schema.json index e0c2aebfe56cb..f95de41c7887b 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -2134,6 +2134,8 @@ "ISC002", "ISC003", "LOG", + "LOG0", + "LOG00", "LOG009", "N", "N8", From 056426382f4de728424b8e4e15cbd8cadb7b1567 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 14 Sep 2023 21:29:13 -0400 Subject: [PATCH 7/7] Add to docs --- docs/faq.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/faq.md b/docs/faq.md index 6002a2fc45a38..62f67d73ad766 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -52,6 +52,7 @@ natively, including: - [flake8-gettext](https://pypi.org/project/flake8-gettext/) - [flake8-implicit-str-concat](https://pypi.org/project/flake8-implicit-str-concat/) - [flake8-import-conventions](https://github.com/joaopalmeiro/flake8-import-conventions) +- [flake8-logging](https://pypi.org/project/flake8-logging-format/) - [flake8-logging-format](https://pypi.org/project/flake8-logging-format/) - [flake8-no-pep420](https://pypi.org/project/flake8-no-pep420) - [flake8-pie](https://pypi.org/project/flake8-pie/) @@ -156,6 +157,7 @@ Today, Ruff can be used to replace Flake8 when used with any of the following pl - [flake8-gettext](https://pypi.org/project/flake8-gettext/) - [flake8-implicit-str-concat](https://pypi.org/project/flake8-implicit-str-concat/) - [flake8-import-conventions](https://github.com/joaopalmeiro/flake8-import-conventions) +- [flake8-logging](https://pypi.org/project/flake8-logging/) - [flake8-logging-format](https://pypi.org/project/flake8-logging-format/) - [flake8-no-pep420](https://pypi.org/project/flake8-no-pep420) - [flake8-pie](https://pypi.org/project/flake8-pie/)