How to detect an undefined method?

Manfred Lotz ml_news at posteo.de
Sun Mar 27 12:47:06 EDT 2022


On 3/27/22 18:12, Peter Otten wrote:
> On 27/03/2022 11:24, Manfred Lotz wrote:
>> Let's say I have a Python app and have used an undefined method somewhere. Let
>> us further assume I have not detected it thru my tests.
>>
>> Is there a way to detect it before deploying the app? pylint doesn't notice it.
>>
>>
>> Minimal example:
>>
>> #!/usr/bin/env python3
>>
>> import logging
>> from logging import Logger
>> from random import randrange
>>
>> def main():
>>      """
>>      Below logger.err gives
>>
>>      'Logger' object has no attribute 'err'
>>      """
>>
>>      logger = logging.getLogger('sample')
>>      logger.setLevel(logging.DEBUG)
>>      handler = logging.StreamHandler()
>>      logger.addHandler(handler)
>>
>>      num = randrange(0,1000)
>>      if num == 0:
>>          logger.err("got zero")
>>      else:
>>          logger.info(f'got a positive integer: {num}')
>>
>> if __name__ == "__main__":
>>      main()
> 
> mypy --strict will find that one. Be warned though that this is a
> slippery slope: you may end up fixing quite a few non-errors...
> 

Great. I wasn't aware of --strict. Regarding 'slippery slope': I anyway would
only fix what I believe is an error.


Thanks.

-- 
Manfred


More information about the Python-list mailing list