tags:

views:

171

answers:

3

Are there any good modules that you can run against your code to catch coding errors? I expected pylint to catch mistakes in the use of default arguments to functions like this:

>>> def spam(eggs=[]):
...     eggs.append("spam")
...     return eggs

but was disappointed to find them unreported. I am looking for something beyond PEP8 formatting.

+3  A: 

I tried the first example and PyLint 0.18.1 gave me the warning:

W:  1:spam: Dangerous default value [] as argument
interjay
Actually, I linted someone's code after I noted this code error. Then I pylint'd the code. In between, he changed the code and I did not notice it. So -- operator error. Sorry.
hughdbrown
+1  A: 

That is not an error in your code if that is what you want to do. However, as specified in the accepted answer, an empty list is a "dangerous" default value in that it is easy to introduce hard-to-find problems.

Noctis Skytower
No, I really didn't want to do that. I wanted pylint to detect it -- and it did. Unfortunately, the codebase changed between when I detected the problem visually and when I ran pylint on the unbeknownst-to-me corrected code. See? It was working and I didn't realize it.
hughdbrown
+2  A: 

I bookmarked this site the other day, partly because it has a section titled "Python code quality tools" that lists many utilities for checking code, including pylint and five others.

I've been thinking of integrating all of them into our automated test framework...

Peter Hansen
+1 This is a great link.
hughdbrown