Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-logging] Add flake8_logging boilerplate and first rule LOG009 #7249

Merged
merged 8 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,31 @@ are:
SOFTWARE.
"""

- flake8-logging, license as follows:
qdegraaf marked this conversation as resolved.
Show resolved Hide resolved
"""
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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
26 changes: 26 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_logging/LOG009.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 10 additions & 4 deletions crates/ruff/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions crates/ruff/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
3 changes: 3 additions & 0 deletions crates/ruff/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions crates/ruff/src/rules/flake8_logging/mod.rs
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(())
}
}
3 changes: 3 additions & 0 deletions crates/ruff/src/rules/flake8_logging/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub(crate) use undocumented_warn::*;

mod undocumented_warn;
63 changes: 63 additions & 0 deletions crates/ruff/src/rules/flake8_logging/rules/undocumented_warn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 `WARN`
qdegraaf marked this conversation as resolved.
Show resolved Hide resolved
///
/// ## 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 Violation for UndocumentedWarn {
const AUTOFIX: AutofixKind = AutofixKind::Sometimes;
#[derive_message_formats]
fn message(&self) -> String {
format!("Use of undocumented logging.WARN constant")
qdegraaf marked this conversation as resolved.
Show resolved Hide resolved
}

fn autofix_title(&self) -> Option<String> {
Some(format!("Replace logging.WARN with logging.WARNING"))
qdegraaf marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// 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);
}
}
Original file line number Diff line number Diff line change
@@ -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


1 change: 1 addition & 0 deletions crates/ruff/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.