-
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
Changes from 2 commits
0d5fd91
796fdbf
e41dc64
43827b9
7323275
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") |
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!") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ use crate::rules::flake8_logging_format::violations::{ | |
LoggingRedundantExcInfo, LoggingStringConcat, LoggingStringFormat, LoggingWarn, | ||
}; | ||
|
||
enum LoggingLevel { | ||
pub enum LoggingLevel { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps this should be moved to a different file now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move this to a new |
||
Debug, | ||
Critical, | ||
Error, | ||
|
@@ -21,7 +21,7 @@ enum LoggingLevel { | |
} | ||
|
||
impl LoggingLevel { | ||
fn from_str(level: &str) -> Option<Self> { | ||
pub fn from_str(level: &str) -> Option<Self> { | ||
match level { | ||
"debug" => Some(LoggingLevel::Debug), | ||
"critical" => Some(LoggingLevel::Critical), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
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::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::{Diagnostic, Rule}; | ||
use crate::rules::flake8_logging_format::rules::LoggingLevel; | ||
use crate::rules::pyflakes::cformat::CFormatSummary; | ||
use crate::violation::Violation; | ||
|
||
define_violation!( | ||
pub struct LoggingTooFewArgs; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
); | ||
impl Violation for LoggingTooFewArgs { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Not enough arguments for logging format string") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: should we wrap |
||
} | ||
} | ||
|
||
define_violation!( | ||
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 let Some(_logging_level) = LoggingLevel::from_str(attr.as_str()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be |
||
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), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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: ~ | ||
|
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.
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.
Nit: can we name these to match the rule? I think that's the convention we've been using for Pylint (e.g.,
logging_too_few_args.py
)