1

The output from pylint states:

nltk/nltk/tag/perceptron.py:203: [W1202(logging-format-interpolation), PerceptronTagger.train] Use % formatting in logging functions and pass the % parameters as arguments

But looking at the code https://github.com/nltk/nltk/blob/develop/nltk/tag/perceptron.py#L203, there isn't any usage of % string formatting:

logging.info("Iter {0}: {1}/{2}={3}".format(iter_, c, n, _pc(c, n)))

Am I missing something or misinterpreting the W1202 message?

How should the code line be change such that the W1202 goes away when pylinting?

alvas
  • 115,346
  • 109
  • 446
  • 738
  • 3
    It's saying you *should* use them - not that you *are* using them :) – Shadow Sep 12 '18 at 00:52
  • Does this answer your question? [PyLint message: logging-format-interpolation](https://stackoverflow.com/questions/34619790/pylint-message-logging-format-interpolation) – pfnuesel Nov 19 '20 at 07:54

1 Answers1

2

Message Interpretation:

It's saying that you should pass the parameters as arguments and pretend that you were using the (old) % formatting style (but pass them in as args instead of using % and wrapping the args in parens). It is not saying that you're using % for string formatting in the log message.

Why is it like this:

The reason you'd want to pass this way is so that the string only goes through templated substitution if the message is actually created by some log handler. String operations are always expensive from a performance standpoint. If you're doing a ton of logging at the DEBUG level, but rarely use level DEBUG in your application, for instance, this can add up fast.

How to fix it:

I think if it were changed to logging.info("Iter %d: %d/%d=%d", iter_, c, n, _pc(c, n))) the message would go away (assuming they are digits, hard to tell from the example).

Matt Messersmith
  • 12,939
  • 6
  • 51
  • 52
  • 1
    Exactly, it's just an outdated warning message I guess. – robinsax Sep 12 '18 at 00:47
  • @robinsax: It's not outdated. The message is saying you *should* use `%` formatting with the `%` arguments as parameters, it's not saying what you *are* doing except that, whatever it is, it's not what you're supposed to do. – ShadowRanger Sep 12 '18 at 00:50
  • @robinsax there is nothing outdated about it. It's trying to save you performance by only concatinating the string when it needs to log. If it doesn't need to log, it leaves the string alone. By using format like in the question you are creating the final string for the sole purpose of throwing it away. – Shadow Sep 12 '18 at 00:51
  • Right, misread it. I realize the benefit, I just thought it referred specifically to the `%` formatting, which it isn't. – robinsax Sep 12 '18 at 00:53