7

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())
Saad
  • 3,340
  • 2
  • 10
  • 32
Rajesh Chamarthi
  • 18,568
  • 4
  • 40
  • 67
  • 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. – Error - Syntactical Remorse Apr 23 '19 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. – Rajesh Chamarthi Apr 23 '19 at 14:38

2 Answers2

3

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.

Aaron Bentley
  • 1,332
  • 8
  • 14
  • 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. – Rajesh Chamarthi May 03 '19 at 15:03
2

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.

MOHRE
  • 1,096
  • 4
  • 15
  • 28