Skip to content

Commit

Permalink
Add detection for missing logging format arguments (pylint-dev#9999)
Browse files Browse the repository at this point in the history
Implements the detection of too-few-args in logging format strings.
Includes new test cases and documentation updates.
  • Loading branch information
andydrewwm committed Dec 10, 2024
1 parent fa00126 commit 76394d0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/whatsnew/fragments/9999.false_negative.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add your info here
4 changes: 2 additions & 2 deletions pylint/checkers/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,12 @@ def _check_format_string(self, node: nodes.Call, format_arg: Literal[0, 1]) -> N
format_arg: Index of the format string in the node arguments.
"""
num_args = _count_supplied_tokens(node.args[format_arg + 1 :])
if not num_args:
format_string = node.args[format_arg].value
if not num_args and "%" not in format_string:
# If no args were supplied the string is not interpolated and can contain
# formatting characters - it's used verbatim. Don't check any further.
return

format_string = node.args[format_arg].value
required_num_args = 0
if isinstance(format_string, bytes):
format_string = format_string.decode()
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/l/logging/logging_too_few_args_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Tests for logging-too-few-args when static placeholders are present."""

import logging

logging.error("foo %s") # [logging-too-few-args]
logging.info("Process %s started with ID %d") # [logging-too-few-args]
logging.debug("Missing args: %s %s") # [logging-too-few-args]
2 changes: 2 additions & 0 deletions tests/functional/l/logging/logging_too_few_args_static.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[LOGGING]
logging-format-style=new

0 comments on commit 76394d0

Please sign in to comment.