Avoid flagging Pylint logging rules with starred arguments (#3244)

This commit is contained in:
Charlie Marsh 2023-02-26 22:58:24 -05:00 committed by GitHub
parent 7e7aec7d74
commit 40c5abf16e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 11 deletions

View File

@ -10,6 +10,12 @@ logging.warning("Hello %s", "World!")
# do not handle calls without any args
logging.info("100% dynamic")
# do not handle calls with *args
logging.error("Example log %s, %s", "foo", "bar", "baz", *args)
# do not handle calls with **kwargs
logging.error("Example log %s, %s", "foo", "bar", "baz", **kwargs)
import warning
warning.warning("Hello %s %s", "World!")

View File

@ -2,11 +2,16 @@ 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!")
# do not handle calls with *args
logging.error("Example log %s, %s", "foo", "bar", "baz", *args)
# do not handle calls with **kwargs
logging.error("Example log %s, %s", "foo", "bar", "baz", **kwargs)
import warning
warning.warning("Hello %s", "World!", "again")

View File

@ -84,8 +84,22 @@ impl Violation for LoggingTooManyArgs {
}
}
/// Check logging calls for violations.
/// PLE1205
/// PLE1206
pub fn logging_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords: &[Keyword]) {
// If there are any starred arguments, abort.
if args
.iter()
.any(|arg| matches!(arg.node, ExprKind::Starred { .. }))
{
return;
}
// If there are any starred keyword arguments, abort.
if keywords.iter().any(|keyword| keyword.node.arg.is_none()) {
return;
}
if !is_logger_candidate(func) {
return;
}
@ -93,8 +107,6 @@ pub fn logging_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords:
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),
@ -106,12 +118,6 @@ pub fn logging_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords:
return;
}
if !call_args.kwargs.is_empty() {
// Keyword checking on logging strings is complicated by
// special keywords - out of scope.
return;
}
let message_args = call_args.args.len() - 1;
if checker.settings.rules.enabled(&Rule::LoggingTooManyArgs) {
@ -124,7 +130,10 @@ pub fn logging_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords:
}
if checker.settings.rules.enabled(&Rule::LoggingTooFewArgs) {
if message_args > 0 && summary.num_positional > message_args {
if message_args > 0
&& call_args.kwargs.is_empty()
&& summary.num_positional > message_args
{
checker.diagnostics.push(Diagnostic::new(
LoggingTooFewArgs,
Range::from_located(func),

View File

@ -12,4 +12,14 @@ expression: diagnostics
column: 15
fix: ~
parent: ~
- kind:
LoggingTooManyArgs: ~
location:
row: 5
column: 0
end_location:
row: 5
column: 15
fix: ~
parent: ~