[code-quality] Static checker for common Python programming errors

Steven D'Aprano steve+python at pearwood.info
Tue Nov 18 13:31:10 CET 2014


On Mon, Nov 17, 2014 at 05:18:03PM +0000, Stefan Bucur wrote:

> I wanted first to tap into people's experience and get a sense of what
> common pitfalls in the language & its standard library such a static
> checker should look for.
[...]
> * Proper Unicode handling (for 2.x)
>   - encode() is not called on str object
>   - decode() is not called on unicode object
> * Check for integer division by zero
> * Check for None object dereferences

This is a very interesting question, and quite hard to answer too. I 
presume that there will be a way to disable the check for individual 
lines of code, because these are not always wrong.


* Check for `is` comparisons against arbitrary objects, especially 
  int and str literals.

* Equality comparisons against None (should use `is`).

* Calling str.find() and str.rfind() without checking for a 
  return result of -1.

* Calling re.match() and similar without checking for a result 
  of None.

* alist = alist.sort() and similar.

* Calling print for its return value, e.g.:
  print("template %s") % obj

* Mutable default values in functions and methods.

* Modifying a sequence or iterator while iterating over it, e.g.
  for i, x in enumerate(alist): 
      if condition(x): 
          del alist[i]


I've done all of these :-(


-- 
Steven


More information about the code-quality mailing list