Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions doc/whatsnew/fragments/10752.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fixed false positive for ``logging-unsupported-format`` when no arguments are provided to logging functions.

According to Python's logging documentation, no formatting is performed when no arguments are supplied, so strings like ``logging.error("%test")`` are valid.

Closes #10752
16 changes: 10 additions & 6 deletions pylint/checkers/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,16 @@ def _check_format_string(self, node: nodes.Call, format_arg: Literal[0, 1]) -> N
keyword_args_cnt + implicit_pos_args + explicit_pos_args
)
except utils.UnsupportedFormatCharacter as ex:
char = format_string[ex.index]
self.add_message(
"logging-unsupported-format",
node=node,
args=(char, ord(char), ex.index),
)
if num_args > 0:
# Only report unsupported format characters if arguments are provided
# When no arguments are supplied, no formatting is performed
# https://docs.python.org/3/library/logging.html#logging.Logger.debug
char = format_string[ex.index]
self.add_message(
"logging-unsupported-format",
node=node,
args=(char, ord(char), ex.index),
)
return
except utils.IncompleteFormatString:
self.add_message("logging-format-truncated", node=node)
Expand Down
23 changes: 23 additions & 0 deletions tests/functional/l/logging/logging_unsupported_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Tests for logging-unsupported-format (issue #10752)

According to Python logging documentation, no formatting is performed
when no arguments are supplied. This test verifies that unsupported
format characters are only reported when arguments are provided.
"""

import logging

# These should NOT trigger warnings (no args = no formatting)
logging.error("%test")
logging.warning("%badformat")
logging.info("%z - invalid specifier")
logging.debug("%q %k %z - multiple invalid")

# These SHOULD trigger warnings (args provided = formatting attempted)
logging.error("%test", 123) # [logging-unsupported-format]
logging.warning("%bad", "arg") # [logging-unsupported-format]
logging.info("Value: %s, Invalid: %z", "test", "val") # [logging-unsupported-format]

# Valid format strings should work fine
logging.info("User %s logged in", "john")
logging.debug("Count: %d", 42)
3 changes: 3 additions & 0 deletions tests/functional/l/logging/logging_unsupported_format.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
logging-unsupported-format:17:0:17:27::Unsupported logging format character 't' (0x74) at index 1:UNDEFINED
logging-unsupported-format:18:0:18:30::Unsupported logging format character 'b' (0x62) at index 1:UNDEFINED
logging-unsupported-format:19:0:19:53::Unsupported logging format character 'z' (0x7a) at index 21:UNDEFINED