-
Notifications
You must be signed in to change notification settings - Fork 892
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
feat: pylint codes E1205-E106 #3084
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0d5fd91
feat: pylint codes E1205-E106
md384 796fdbf
fix: rules swapped in conditionals
md384 e41dc64
fix: rename tests, add docs and small fixes
md384 43827b9
fix: move LoggingLevel to own file
md384 7323275
Merge branch 'main' into m.pylint-E1205-E1206
md384 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
crates/ruff/resources/test/fixtures/pylint/logging_too_few_args.py
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,12 @@ | ||
import logging | ||
|
||
logging.warning("Hello %s %s", "World!") # [logging-too-few-args] | ||
|
||
# do not handle calls with kwargs (like pylint) | ||
logging.warning("Hello %s", "World!", "again", something="else") | ||
|
||
logging.warning("Hello %s", "World!") | ||
|
||
import warning | ||
|
||
warning.warning("Hello %s %s", "World!") |
12 changes: 12 additions & 0 deletions
12
crates/ruff/resources/test/fixtures/pylint/logging_too_many_args.py
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,12 @@ | ||
import logging | ||
|
||
logging.warning("Hello %s", "World!", "again") # [logging-too-many-args] | ||
|
||
# do not handle calls with kwargs (like pylint) | ||
logging.warning("Hello %s", "World!", "again", something="else") | ||
|
||
logging.warning("Hello %s", "World!") | ||
|
||
import warning | ||
|
||
warning.warning("Hello %s", "World!", "again") |
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,24 @@ | ||
pub enum LoggingLevel { | ||
Debug, | ||
Critical, | ||
Error, | ||
Exception, | ||
Info, | ||
Warn, | ||
Warning, | ||
} | ||
|
||
impl LoggingLevel { | ||
pub fn from_str(level: &str) -> Option<Self> { | ||
match level { | ||
"debug" => Some(LoggingLevel::Debug), | ||
"critical" => Some(LoggingLevel::Critical), | ||
"error" => Some(LoggingLevel::Error), | ||
"exception" => Some(LoggingLevel::Exception), | ||
"info" => Some(LoggingLevel::Info), | ||
"warn" => Some(LoggingLevel::Warn), | ||
"warning" => Some(LoggingLevel::Warning), | ||
_ => None, | ||
} | ||
} | ||
} |
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
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,139 @@ | ||
use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword}; | ||
|
||
use ruff_macros::{define_violation, derive_message_formats}; | ||
|
||
use crate::ast::helpers::{is_logger_candidate, SimpleCallArgs}; | ||
use crate::ast::logging::LoggingLevel; | ||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::{Diagnostic, Rule}; | ||
use crate::rules::pyflakes::cformat::CFormatSummary; | ||
use crate::violation::Violation; | ||
|
||
define_violation!( | ||
/// ## What it does | ||
/// Checks for too few positional arguments for a `logging` format string. | ||
/// | ||
/// ## Why is this bad? | ||
/// A `TypeError` will be raised if the statement is run. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// import logging | ||
/// | ||
/// try: | ||
/// function() | ||
/// except Exception as e: | ||
/// logging.error('%s error occurred: %s', e) # [logging-too-few-args] | ||
/// raise | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// import logging | ||
/// | ||
/// try: | ||
/// function() | ||
/// except Exception as e: | ||
/// logging.error('%s error occurred: %s', type(e), e) | ||
/// raise | ||
/// ``` | ||
pub struct LoggingTooFewArgs; | ||
); | ||
impl Violation for LoggingTooFewArgs { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Not enough arguments for `logging` format string") | ||
} | ||
} | ||
|
||
define_violation!( | ||
/// ## What it does | ||
/// Checks for too many positional arguments for a `logging` format string. | ||
/// | ||
/// ## Why is this bad? | ||
/// A `TypeError` will be raised if the statement is run. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// import logging | ||
/// | ||
/// try: | ||
/// function() | ||
/// except Exception as e: | ||
/// logging.error('Error occurred: %s', type(e), e) # [logging-too-many-args] | ||
/// raise | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// import logging | ||
/// | ||
/// try: | ||
/// function() | ||
/// except Exception as e: | ||
/// logging.error('%s error occurred: %s', type(e), e) | ||
/// raise | ||
/// ``` | ||
pub struct LoggingTooManyArgs; | ||
); | ||
impl Violation for LoggingTooManyArgs { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Too many arguments for `logging` format string") | ||
} | ||
} | ||
|
||
/// Check logging calls for violations. | ||
pub fn logging_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords: &[Keyword]) { | ||
if !is_logger_candidate(func) { | ||
return; | ||
} | ||
|
||
if let ExprKind::Attribute { attr, .. } = &func.node { | ||
if LoggingLevel::from_str(attr.as_str()).is_some() { | ||
let call_args = SimpleCallArgs::new(args, keywords); | ||
|
||
// E1205 - E1206 | ||
if let Some(msg) = call_args.get_argument("msg", Some(0)) { | ||
if let ExprKind::Constant { | ||
value: Constant::Str(value), | ||
.. | ||
} = &msg.node | ||
{ | ||
if let Ok(summary) = CFormatSummary::try_from(value.as_str()) { | ||
if summary.starred { | ||
return; | ||
} | ||
|
||
if !call_args.kwargs.is_empty() { | ||
// Keyword checking on logging strings is complicated by | ||
// special keywords - out of scope. | ||
return; | ||
} | ||
md384 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let message_args = call_args.args.len() - 1; | ||
|
||
if checker.settings.rules.enabled(&Rule::LoggingTooManyArgs) | ||
&& summary.num_positional < message_args | ||
{ | ||
checker.diagnostics.push(Diagnostic::new( | ||
LoggingTooManyArgs, | ||
Range::from_located(func), | ||
)); | ||
} | ||
|
||
if checker.settings.rules.enabled(&Rule::LoggingTooFewArgs) | ||
&& summary.num_positional > message_args | ||
{ | ||
checker.diagnostics.push(Diagnostic::new( | ||
LoggingTooFewArgs, | ||
Range::from_located(func), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
.../rules/pylint/snapshots/ruff__rules__pylint__tests__PLE1205_logging_too_many_args.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,15 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
LoggingTooManyArgs: ~ | ||
location: | ||
row: 3 | ||
column: 0 | ||
end_location: | ||
row: 3 | ||
column: 15 | ||
fix: ~ | ||
parent: ~ | ||
|
15 changes: 15 additions & 0 deletions
15
...c/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE1206_logging_too_few_args.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,15 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
LoggingTooFewArgs: ~ | ||
location: | ||
row: 3 | ||
column: 0 | ||
end_location: | ||
row: 3 | ||
column: 15 | ||
fix: ~ | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you open to writing documentation for these rules? We're trying to include documentation for all new rules. You can grep for
/// ## What it does
to see some examples of the expected format.