sourcery-ai / sourcery

Instant AI code reviews
https://sourcery.ai
MIT License
1.53k stars 66 forks source link

Taking into account logging optimisations #276

Open TheMythologist opened 1 year ago

TheMythologist commented 1 year ago

Checklist

Description

The in-built Python logging module has its own optimizations:

Formatting of message arguments is deferred until it cannot be avoided.

We should make use of these optimizations when using the logging module, instead of f-strings, string concatenation, string formatting (via the % operator) or str.format().

Note: The variable types may have to be taken into account for different variables, I'm not sure if just using %s will convert it into its __str__ format.

Code Before

import logging

a = "world"
b = "goodbye"
c = 1
d = 2

logging.info(f"Hello {a}")
logging.debug(b + " Tom!")
logging.warning(f"c = {c}")
logging.log("d = %d" % d)

Code After

import logging

a = "world"
b = "goodbye"
c = 1
d = 2

logging.info("Hello %s", a)
logging.debug("%s Tom!", b)
logging.warning("c = %d", c)
logging.log("d = %d", d)
Hellebore commented 1 year ago

Oh yeah these look useful!