Skip to content

Commit 96211b2

Browse files
[feat] Detect missing formatting args when the string is not interpolated too
The original decision was taken in db8b3a4 during implementation with a different rational ("If no args were supplied, then all format strings are valid don't check any further.") and was not discussed again when the comment was updated in #2713. I think the new behavior make sense especially considering that the primer shows 4 false negative in home-assistant. Co-authored-by: Alex Prabhat Bara <[email protected]>
1 parent 3b9e2d2 commit 96211b2

9 files changed

+28
-12
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
We now raise a ``logging-too-few-args`` for format string with no
2+
interpolation arguments at all (i.e. for something like ``logging.debug("Awaiting process %s")``
3+
or ``logging.debug("Awaiting process {pid}")``). Previously we did not raise for such case.
4+
5+
Closes #9999

pylint/checkers/logging.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,6 @@ def _check_format_string(self, node: nodes.Call, format_arg: Literal[0, 1]) -> N
326326
format_arg: Index of the format string in the node arguments.
327327
"""
328328
num_args = _count_supplied_tokens(node.args[format_arg + 1 :])
329-
if not num_args:
330-
# If no args were supplied the string is not interpolated and can contain
331-
# formatting characters - it's used verbatim. Don't check any further.
332-
return
333-
334329
format_string = node.args[format_arg].value
335330
required_num_args = 0
336331
if isinstance(format_string, bytes):

tests/functional/l/logging/logging_too_few_args.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/functional/l/logging/logging_too_few_args.py renamed to tests/functional/l/logging/logging_too_few_args_new_style.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
"""Tests for logging-too-few-args"""
1+
"""Tests for logging-too-few-args new style"""
22

33
import logging
44

55
logging.error("{}, {}", 1) # [logging-too-few-args]
66
logging.error("{0}, {1}", 1) # [logging-too-few-args]
7+
logging.error("{}") # [logging-too-few-args]
8+
logging.error("{0}") # [logging-too-few-args]
9+
logging.error("{named}") # [logging-too-few-args]
710
logging.error("{named1}, {named2}", {"named1": 1}) # [logging-too-few-args]
811
logging.error("{0}, {named}", 1) # [logging-too-few-args]
912
logging.error("{}, {named}", {"named": 1}) # [logging-too-few-args]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
logging-too-few-args:5:0:5:26::Not enough arguments for logging format string:UNDEFINED
2+
logging-too-few-args:6:0:6:28::Not enough arguments for logging format string:UNDEFINED
3+
logging-too-few-args:7:0:7:19::Not enough arguments for logging format string:UNDEFINED
4+
logging-too-few-args:8:0:8:20::Not enough arguments for logging format string:UNDEFINED
5+
logging-too-few-args:9:0:9:24::Not enough arguments for logging format string:UNDEFINED
6+
logging-too-few-args:10:0:10:50::Not enough arguments for logging format string:UNDEFINED
7+
logging-too-few-args:11:0:11:32::Not enough arguments for logging format string:UNDEFINED
8+
logging-too-few-args:12:0:12:42::Not enough arguments for logging format string:UNDEFINED
9+
logging-too-few-args:13:0:13:43::Not enough arguments for logging format string:UNDEFINED
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""Tests for logging-too-few-args old style"""
2+
3+
import logging
4+
5+
logging.error("%s, %s", 1) # [logging-too-few-args]
6+
logging.debug("Sisyphus table %s: sleep") # [logging-too-few-args]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[LOGGING]
2+
logging-format-style=old
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
logging-too-few-args:5:0:5:26::Not enough arguments for logging format string:UNDEFINED
2+
logging-too-few-args:6:0:6:41::Not enough arguments for logging format string:UNDEFINED

0 commit comments

Comments
 (0)