2626
2727MSGS = {
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 (
0 commit comments