How to detect an undefined method?

Peter Otten __peter__ at web.de
Sun Mar 27 12:12:35 EDT 2022


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...



More information about the Python-list mailing list