Safer logging methods for f-strings and new-style formatting

Some other things that crossed my mind:

Should the ‘old’ logging methods be deprecated? I think so. If not, I doubt any f-string users would make the switch on their own (becuase the use of old methods wouldn’t trigger typing errors).

Instead of overload, a separate method is possible: debugs() where the s-suffix stands for “static” (i.e. no formatting will be applied). An advantage of this would be slightly better performance (no args/kwargs checks) and additional safety for non-typing users.

One could argue whether it’s better to have 2 explicitly separate methods for logging with/without formatting.

str is an immutable type so although I guess the f strings call str.format I cannot see any way to monkey patch it.

Not quite; as I understand it f-string literals get compiled with a special opcode to join the string fragments together, and then at runtime the .__format__() method gets called on the value of each expression inside braces in the string. However, object is an immutable C-level type, so you’d have to (at the very least) resort to serious hackery to make it work.

This WIP PEP may be a solution to this problem: GitHub - jimbaker/tagstr: This repo contains an issue tracker, examples, and early work related to PEP 999: Tag Strings

2 Likes