8

Can you catch import / name and other errors in python using a (linting) tool or a compilation step?

The other option is to make sure all possible code paths are tested (This is not always feasible, especially for large existing code bases and other reasons)

Here are some examples.

  1. Missing import - caught by pylint, although as a syntax error instead of an import error.
def test():
    print("Time now is ..", datetime.datetime())

pylint output:

E0602: Undefined variable 'datetime' (undefined-variable)
  1. Import present, but incorrect method used. This passes both pylint and py_compile.
from datetime import datetime
def test():
    print("Time now is ..", datetime.today2())

Edit: To add one more option.

Doing an import * shows some errors, but not errors in statements which are inside the functions.

This error is reported

from datetime import datetime
print("today2", datetime.today2())

Error :

Python 3.7.0 (default, Aug 22 2018, 15:22:56)
>>> from test import *
...
    print("today2", datetime.today2())
AttributeError: type object 'datetime.datetime' has no attribute 'today2'
>>>

This is not.

from datetime import datetime
def test():
    print("Time now is ..", datetime.today2())
2
  • 1
    You can create a doctest and run it whenever you run the program. It would catch all failures though, instead of specific ones. Otherwise you will probably have to add try except to the individual functions. Scope is tricky with python so functions won't fail at import time because at run time the variables needed within the function may exist. Commented Apr 23, 2019 at 14:19
  • Thank you. So far, it looks like a test for each function is the only option, if I want to be absolutely sure that all the errors are captured. Even in that case, it wouldn't be complete error checking. Commented Apr 23, 2019 at 14:38

2 Answers 2

3
+50

In my experience, flake8 does a great job of catching missing imports and name errors. In order to catch missing imports, you must not use wildcard imports like "from foo import *", since it cannot guess which names that will create. Also, it cannot do these detections while syntax errors exist, so you have to fix those first.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I do use flake8 and have plans to do run "python -m pycompile" on all the python files in the directory, Good to know these are the tools that have a best chance of catching errors.
I'd just like to know what errors python -m compileall will catch, if any. The documentation is sorely lacking on that.
3

Unlike c++ which is a compiled language, python is an interpreted language. Which means it doesn't have compile phase. It interpreted code line by line.

According to that, you didn't find the errors until you reach them in runtime.

If you want that errors appears, you should somehow path throw every line of your code. The best approach is using test libraries with 100% test coverage.

For more information look at this question and it's answers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.