1

AFAIK pylint's logging-format-interpolation tries to nudge me to use "%"-interpolation so it can delay execution to cases where it is strictly necessary, as e.g. described here.

However, I am not able to produce an example where this delay actually happens:

import logging

logging.basicConfig(level=logging.INFO)

def log_identity(s: str) -> str:
    logging.warning('warning %s', s)
    return s

logging.debug('%s', log_identity('c'))  # Expect delay, i.e. no warning c
logging.debug(f"{log_identity('d')}")  # Expect no delay, i.e. warning d

However, while the actual debug part never emits a message (as per expectations), the strings get interpolated both times:

WARNING:root:warning c
WARNING:root:warning d

Short of explicitly using .isEnabledFor (cf. here), is there a way of preventing evaluation of log messages using "%"-interpolation? If so, how does it look?

user1965813
  • 671
  • 5
  • 16
  • 2
    I think your example does not actually test that the string in the logging is evaluated or not. The function ``log_identity`` is executed and your warning logging too. This can't be avoided without using ``isEnabledFor``. But the actual formatting of ``'%s' % result`` is what the pylint's warning is trying to avoid unless required. – Pierre.Sassoulas Oct 21 '21 at 20:13

0 Answers0