[Python-ideas] String Format Callable Flag (Was: Efficient Debug Logging)

Eric V. Smith eric at trueblade.com
Sun Feb 19 11:21:07 EST 2017


On 2/19/2017 4:23 AM, Pavol Lisy wrote:
> On 2/19/17, Eric V. Smith <eric at trueblade.com> wrote:
>> On 2/18/2017 2:25 AM, Steven D'Aprano wrote:
>
>>> I see three problems:
>>>
>>> (1) It will be a bug magnet. People will accidently write
>>>
>>>     logging.debug('%03d %C03d', 1, expensive())
>>>
>>>
>>> and then not only will their code still be slow, but they'll have to
>>> debug mysterious
>>>
>>>     TypeError: 'int' object is not callable
>>>
>>> exceptions, but only *sometimes*. Most insideously, these Heisenbugs
>>> will only occur when they turn the log level all the way up to
>>> debugging, which will crash their program *before* logging the error!
>>
>> I think this is lessened with my proposal to use !c, but it's definitely
>> still an issue. Test your logging!
>
> 1. I think that error message could be understandable at least as this:
>     '{:g}'.format('a')
>     ValueError: Unknown format code 'g' for object of type 'str'
>
> with something like isinstance(arg, callable)

Sure, there would be a reasonable error message. The concern (as with 
logging anything), is that the error case logging is typically poorly 
tested.

> 2. Static checker could check bracket presence if there is !c format
> specifier and fire warning.

But it's valid to have a function that returns a callable, so these 
could be false positives. But linters can do whatever they want, I guess.

> 3. There could be another problem if return type is callable too...

I don't think that's a problem. You'd just print the callable, as you 
can do today:

 >>> def fn(): pass
...
 >>> format(fn, '')
'<function fn at 0xffee6108>'




More information about the Python-ideas mailing list