Skip to content

Commit c236745

Browse files
[logging-too-few-args] Raise for format strings with no interpolation at all (#10321)
* [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]> * Fix required because logging are tested elsewhere
1 parent 3b9e2d2 commit c236745

13 files changed

+30
-15
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/config/data/logging_format_interpolation_style.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import logging
66

77
logging.error("constant string")
8-
logging.error("{}")
98
logging.error("{}", 1)
109
logging.error("{0}", 1)
1110
logging.error("{named}", {"named": 1})

tests/functional/l/logging/logging_format_interpolation_style.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import logging
44

55
logging.error("constant string")
6-
logging.error("{}")
76
logging.error("{}", 1)
87
logging.error("{0}", 1)
98
logging.error("{named}", {"named": 1})

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

tests/functional/s/string/string_log_formatting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def pprint():
2222
logging.info(1)
2323
logging.info(True)
2424
logging.info('')
25-
logging.info('%s%')
25+
logging.info('%s%') # [logging-format-truncated]
2626
logging.info('%s', '')
2727
logging.info('%s%%', '')
2828
logging.info('%s%s', '', '')

tests/functional/s/string/string_log_formatting.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ logging-format-truncated:16:4:16:27:pprint:Logging format string ends in middle
44
logging-too-few-args:17:4:17:28:pprint:Not enough arguments for logging format string:UNDEFINED
55
logging-unsupported-format:18:4:18:32:pprint:Unsupported logging format character 'y' (0x79) at index 3:UNDEFINED
66
logging-too-many-args:19:4:19:36:pprint:Too many arguments for logging format string:HIGH
7+
logging-format-truncated:25:4:25:23:pprint:Logging format string ends in middle of conversion specifier:UNDEFINED

0 commit comments

Comments
 (0)