Skip to content

Commit fb59ed8

Browse files
AWhetterPCManticore
authored andcommitted
Brought back logging-fstring-interpolation
1 parent db0d55b commit fb59ed8

12 files changed

+113
-112
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ Release date: TBA
275275
Close #1482
276276
Close #1553
277277

278+
* Multiple types of string formatting are allowed in logging functions.
279+
280+
The `logging-fstring-interpolation` message has been brought back to allow
281+
multiple types of string formatting to be used.
282+
283+
Close #3361
284+
278285
What's New in Pylint 2.4.4?
279286
===========================
280287
Release date: 2019-11-13

doc/whatsnew/2.5.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,11 @@ Other Changes
9292
This command lists all extensions present in ``pylint.extensions``.
9393

9494
* Various false positives have been fixed which you can read more about in the Changelog files.
95+
96+
* Multiple types of string formatting are allowed in logging functions.
97+
98+
The `logging-fstring-interpolation` message has been brought back to allow
99+
multiple types of string formatting to be used.
100+
The type of formatting to use is chosen through enabling and disabling messages
101+
rather than through the logging-format-style option.
102+
The fstr value of the logging-format-style option is not valid.

pylint/checkers/logging.py

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,43 @@
2626

2727
MSGS = {
2828
"W1201": (
29-
"Specify string format arguments as logging function parameters",
29+
"Use %s formatting in logging functions",
3030
"logging-not-lazy",
3131
"Used when a logging statement has a call form of "
3232
'"logging.<logging method>(format_string % (format_args...))". '
33-
"Such calls should leave string interpolation to the logging "
34-
"method itself and be written "
35-
'"logging.<logging method>(format_string, format_args...)" '
36-
"so that the program may avoid incurring the cost of the "
37-
"interpolation in those cases in which no message will be "
38-
"logged. For more, see "
39-
"http://www.python.org/dev/peps/pep-0282/.",
33+
"Use another type of string formatting instead. "
34+
"You can use % formatting but leave interpolation to "
35+
"the logging function by passing the parameters as arguments. "
36+
"If logging-fstring-interpolation is disabled then "
37+
"you can use fstring formatting. "
38+
"If logging-format-interpolation is disabled then "
39+
"you can use str.format.",
4040
),
4141
"W1202": (
42-
"Use %s formatting in logging functions%s",
42+
"Use %s formatting in logging functions",
4343
"logging-format-interpolation",
4444
"Used when a logging statement has a call form of "
45-
'"logging.<logging method>(<string formatting>)".'
46-
" with invalid string formatting. "
47-
"Use another way for format the string instead.",
45+
'"logging.<logging method>(format_string.format(format_args...))". '
46+
"Use another type of string formatting instead. "
47+
"You can use % formatting but leave interpolation to "
48+
"the logging function by passing the parameters as arguments. "
49+
"If logging-fstring-interpolation is disabled then "
50+
"you can use fstring formatting. "
51+
"If logging-not-lazy is disabled then "
52+
"you can use % formatting as normal.",
53+
),
54+
"W1203": (
55+
"Use %s formatting in logging functions",
56+
"logging-fstring-interpolation",
57+
"Used when a logging statement has a call form of "
58+
'"logging.<logging method>(f"...")".'
59+
"Use another type of string formatting instead. "
60+
"You can use % formatting but leave interpolation to "
61+
"the logging function by passing the parameters as arguments. "
62+
"If logging-format-interpolation is disabled then "
63+
"you can use str.format. "
64+
"If logging-not-lazy is disabled then "
65+
"you can use % formatting as normal.",
4866
),
4967
"E1200": (
5068
"Unsupported logging format character %r (%#02x) at index %d",
@@ -126,11 +144,10 @@ class LoggingChecker(checkers.BaseChecker):
126144
{
127145
"default": "old",
128146
"type": "choice",
129-
"metavar": "<old (%) or new ({) or fstr (f'')>",
130-
"choices": ["old", "new", "fstr"],
131-
"help": "Format style used to check logging format string. "
132-
"`old` means using % formatting, `new` is for `{}` formatting,"
133-
"and `fstr` is for f-strings.",
147+
"metavar": "<old (%) or new ({)>",
148+
"choices": ["old", "new"],
149+
"help": "The type of string formatting that logging methods do. "
150+
"`old` means using % formatting, `new` is for `{}` formatting.",
134151
},
135152
),
136153
)
@@ -144,12 +161,6 @@ def visit_module(self, node): # pylint: disable=unused-argument
144161
logging_mods = self.config.logging_modules
145162

146163
self._format_style = self.config.logging_format_style
147-
format_styles = {"old": "%", "new": "{", "fstr": "f-string"}
148-
format_style_help = ""
149-
if self._format_style == "old":
150-
format_style_help = " and pass the % parameters as arguments"
151-
152-
self._format_style_args = (format_styles[self._format_style], format_style_help)
153164

154165
self._logging_modules = set(logging_mods)
155166
self._from_imports = {}
@@ -238,20 +249,36 @@ def _check_log_method(self, node, name):
238249
)
239250
emit = total_number_of_strings > 0
240251
if emit:
241-
self.add_message("logging-not-lazy", node=node)
252+
self.add_message(
253+
"logging-not-lazy", node=node, args=(self._helper_string(node),),
254+
)
242255
elif isinstance(node.args[format_pos], astroid.Call):
243256
self._check_call_func(node.args[format_pos])
244257
elif isinstance(node.args[format_pos], astroid.Const):
245258
self._check_format_string(node, format_pos)
246-
elif isinstance(
247-
node.args[format_pos], (astroid.FormattedValue, astroid.JoinedStr)
259+
elif isinstance(node.args[format_pos], astroid.JoinedStr):
260+
self.add_message(
261+
"logging-fstring-interpolation",
262+
node=node,
263+
args=(self._helper_string(node),),
264+
)
265+
266+
def _helper_string(self, node):
267+
"""Create a string that lists the valid types of formatting for this node."""
268+
valid_types = ["lazy %"]
269+
270+
if not self.linter.is_message_enabled(
271+
"logging-fstring-formatting", node.fromlineno
248272
):
249-
if self._format_style != "fstr":
250-
self.add_message(
251-
"logging-format-interpolation",
252-
node=node,
253-
args=self._format_style_args,
254-
)
273+
valid_types.append("fstring")
274+
if not self.linter.is_message_enabled(
275+
"logging-format-formatting", node.fromlineno
276+
):
277+
valid_types.append(".format()")
278+
if not self.linter.is_message_enabled("logging-not-lazy", node.fromlineno):
279+
valid_types.append("%")
280+
281+
return " or ".join(valid_types)
255282

256283
@staticmethod
257284
def _is_operand_literal_str(operand):
@@ -274,7 +301,9 @@ def _check_call_func(self, node):
274301
func.bound
275302
):
276303
self.add_message(
277-
"logging-format-interpolation", node=node, args=self._format_style_args
304+
"logging-format-interpolation",
305+
node=node,
306+
args=(self._helper_string(node),),
278307
)
279308

280309
def _check_format_string(self, node, format_arg):
@@ -317,13 +346,6 @@ def _check_format_string(self, node, format_arg):
317346
required_num_args = (
318347
keyword_args_cnt + implicit_pos_args + explicit_pos_args
319348
)
320-
else:
321-
self.add_message(
322-
"logging-format-interpolation",
323-
node=node,
324-
args=self._format_style_args,
325-
)
326-
return
327349
except utils.UnsupportedFormatCharacter as ex:
328350
char = format_string[ex.index]
329351
self.add_message(
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
logging-format-interpolation:16::Use % formatting in logging functions and pass the % parameters as arguments
2-
logging-format-interpolation:17::Use % formatting in logging functions and pass the % parameters as arguments
3-
logging-format-interpolation:18::Use % formatting in logging functions and pass the % parameters as arguments
4-
logging-format-interpolation:19::Use % formatting in logging functions and pass the % parameters as arguments
5-
logging-format-interpolation:20::Use % formatting in logging functions and pass the % parameters as arguments
1+
logging-format-interpolation:16::Use lazy % formatting in logging functions
2+
logging-format-interpolation:17::Use lazy % formatting in logging functions
3+
logging-format-interpolation:18::Use lazy % formatting in logging functions
4+
logging-format-interpolation:19::Use lazy % formatting in logging functions
5+
logging-format-interpolation:20::Use lazy % formatting in logging functions
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
"""Test logging-format-interpolation for Python 3.6"""
1+
"""Test logging-fstring-interpolation for Python 3.6"""
22
import logging as renamed_logging
33

44

5-
renamed_logging.info(f'Read {renamed_logging} from globals') # [logging-format-interpolation]
5+
renamed_logging.info(f'Read {renamed_logging} from globals') # [logging-fstring-interpolation]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
logging-format-interpolation:5::Use % formatting in logging functions and pass the % parameters as arguments
1+
logging-fstring-interpolation:5::Use lazy % formatting in logging functions

tests/functional/l/logging_fstring_interpolation_py36.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Test logging-format-interpolation for Python 3.6"""
1+
"""Test logging-fstring-interpolation for Python 3.6"""
22
# pylint: disable=invalid-name
33

44
from datetime import datetime
@@ -14,8 +14,8 @@
1414
may_14 = datetime(year=2018, month=5, day=14)
1515

1616
# Statements that should be flagged:
17-
renamed_logging.debug(f'{local_var_1} {local_var_2}') # [logging-format-interpolation]
18-
renamed_logging.log(renamed_logging.DEBUG, f'msg: {local_var_2}') # [logging-format-interpolation]
19-
renamed_logging.log(renamed_logging.DEBUG, f'pi: {pi:.3f}') # [logging-format-interpolation]
20-
renamed_logging.info(f"{local_var_2.upper()}") # [logging-format-interpolation]
21-
renamed_logging.info(f"{may_14:'%b %d: %Y'}") # [logging-format-interpolation]
17+
renamed_logging.debug(f'{local_var_1} {local_var_2}') # [logging-fstring-interpolation]
18+
renamed_logging.log(renamed_logging.DEBUG, f'msg: {local_var_2}') # [logging-fstring-interpolation]
19+
renamed_logging.log(renamed_logging.DEBUG, f'pi: {pi:.3f}') # [logging-fstring-interpolation]
20+
renamed_logging.info(f"{local_var_2.upper()}") # [logging-fstring-interpolation]
21+
renamed_logging.info(f"{may_14:'%b %d: %Y'}") # [logging-fstring-interpolation]
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
logging-format-interpolation:17::Use % formatting in logging functions and pass the % parameters as arguments
2-
logging-format-interpolation:18::Use % formatting in logging functions and pass the % parameters as arguments
3-
logging-format-interpolation:19::Use % formatting in logging functions and pass the % parameters as arguments
4-
logging-format-interpolation:20::Use % formatting in logging functions and pass the % parameters as arguments
5-
logging-format-interpolation:21::Use % formatting in logging functions and pass the % parameters as arguments
1+
logging-fstring-interpolation:17::Use lazy % formatting in logging functions
2+
logging-fstring-interpolation:18::Use lazy % formatting in logging functions
3+
logging-fstring-interpolation:19::Use lazy % formatting in logging functions
4+
logging-fstring-interpolation:20::Use lazy % formatting in logging functions
5+
logging-fstring-interpolation:21::Use lazy % formatting in logging functions
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
logging-not-lazy:10::Specify string format arguments as logging function parameters
2-
logging-not-lazy:11::Specify string format arguments as logging function parameters
3-
logging-not-lazy:12::Specify string format arguments as logging function parameters
4-
logging-not-lazy:13::Specify string format arguments as logging function parameters
5-
logging-not-lazy:14::Specify string format arguments as logging function parameters
6-
logging-not-lazy:15::Specify string format arguments as logging function parameters
1+
logging-not-lazy:10::Use lazy % formatting in logging functions
2+
logging-not-lazy:11::Use lazy % formatting in logging functions
3+
logging-not-lazy:12::Use lazy % formatting in logging functions
4+
logging-not-lazy:13::Use lazy % formatting in logging functions
5+
logging-not-lazy:14::Use lazy % formatting in logging functions
6+
logging-not-lazy:15::Use lazy % formatting in logging functions

tests/messages/func_bug113231.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
W: 20: Specify string format arguments as logging function parameters
2-
W: 21: Specify string format arguments as logging function parameters
1+
W: 20: Use lazy % formatting in logging functions
2+
W: 21: Use lazy % formatting in logging functions

0 commit comments

Comments
 (0)