tags:

views:

329

answers:

4

I found the following mistake in my code this week:

import datetime

d = datetime.date(2010,9,24)
if d.isoweekday == 5:
    pass

Yes, it should be d.isoweekday() instead.

I know, if I had had a test-case for this I would have been saved. Comparing a function with 5 is not very useful. Oh, I'm not blaming Python for this.

My question: Are there tools that can spot errors like this one?

+3  A: 

Check out pylint it may be able to get that. It does find many errors.

Wernight
It won't find this error.
dekomote
Nor does [pychecker](http://pychecker.sourceforge.net/).
Adam Backstrom
That's because there is no error here. d.isoweekday == 5 is a valid statement.
dekomote
@dekomote It didn't work the way Eddy intended, so it's a user error, which is what linters are supposed to catch. Not that they can catch everything, but it's incorrect to say there was no error.
Adam Backstrom
@dekomote: you're missing the point of pychecker and pylint. They aren't for finding invalid Python: they are for finding mistakes that people make. For example, they can tell you that you import a module but never use it, which is perfectly valid Python, but probably not what you wanted.
Ned Batchelder
A: 

You can use a debugger but I am not sure whether that would highlight this kind of error.

You can check this link as well: http://stackoverflow.com/questions/477193/suggestions-for-python-debugging-tools

anand
+3  A: 

Well, this is not an error in python per se because in Python, the functions are callable objects. You can make any object callable by implementing __call__. So d.isoweekday == 5 is valid statement. This will be False.

As for other errors, i suggest checking out pyflakes - http://divmod.org/trac/wiki/DivmodPyflakes

dekomote
+7  A: 

As an alternative, most Python projects are unit tested and system tested. If you have both (or even just unit tests) you'll find your problem along with pretty much any other issue.

As dekomote said, this is syntaxically valid. Python is not statically typed so this cannot be caught as an error. At most it could be a warning.

EDIT: Python is strongly typed just the type is checked at run time.

Wernight
-1 python is strongly typed. It is not _statically_ typed. This is not a small distinction. Further, it shouldn't even be a warning because the comparison is perfectly valid.
aaronasterling
Thanks for the correction. Well it doesn't change much unless a tool is capable of emulating an execution without running the program. Else it could only be a guess.
Wernight
+1 for being the only one in the answers to recommend unit testing (S. Lott is on it in the comments)
aaronasterling
Just because it's perfectly valid doesn't mean it isn't worthy of a warning. In C, `if (x=false)` is perfectly valid as well, but we're all glad the compiler warns us when it shows up in our code.
Darryl
@Darryl: C is statically typed. Doesn't mean there cannot be a warning but that warning may be incorrect.
Wernight
+100 for promoting unit testing.
Eddy Pronk