Search code examples
pythonpython-3.xmypylintpylint

How to catch potentially undefined variables with pylint or mypy?


import time

if time.time() > 42:
    x = 1

print(x)

My IDE (PyCharm) warns me about x potentially being undefined:

enter image description here

But pylint and mypy do not say anything. Is there a way to make one of them complain about this situation too?


Solution

  • mypy has partial* support via the error code [possibly-undefined], which you can enable through one of the following:

    • The CLI when running mypy (mypy <...> --enable-error-code possibly-undefined);

    • A mypy configuration file setting which looks like

      [mypy]
      enable_error_code = possibly-undefined
      
    • The file-level configuration comment line

      # mypy: enable-error-code=possibly-undefined
      

    See a demonstration of your example in the mypy Playground.


    *Partial, because mypy treats annotated assignment statements as an implicit declaration of existence; it won't catch this example:

    import time
    
    x: int  # Annotated assignment statement with no value set
    if time.time() > 42:
        x = 1
    
    print(x)  # No errors caught here
    

    Note that, for all intents and purposes, this is good enough as full support. It's not possible for a type-checker to determine whether all execution paths can be met for (e.g.) independent conditional blocks; any more inference of [possibly-undefined] would result in a copious amount of false positives.