16

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?

Eddy Pronk
  • 6,527
  • 5
  • 33
  • 57
  • 3
    "if I had had a test-case for this". Isn't that the answer to your question? "Write a unit test." – S.Lott Sep 22 '10 at 14:10
  • @S.Lott I covered myself for this answer. So, no. :) – Eddy Pronk Sep 22 '10 at 22:31
  • You're missing the point. Given (a) the absence of a test and (b) the answers you have, you're going to have to start writing tests. – S.Lott Sep 23 '10 at 01:02
  • If I only had had enough reputation to edit that question, I could have had had the ability to fix the typo. – Chris Sep 25 '10 at 02:31

3 Answers3

7

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
  • 36,122
  • 25
  • 118
  • 131
  • 1
    -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 Sep 24 '10 at 05:14
  • 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 Sep 24 '10 at 10:18
  • +1 for being the only one in the answers to recommend unit testing (S. Lott is on it in the comments) – aaronasterling Sep 24 '10 at 10:21
  • 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 Sep 29 '10 at 20:48
  • @Darryl: C is statically typed. Doesn't mean there cannot be a warning but that warning may be incorrect. – Wernight Sep 30 '10 at 09:35
3

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

Wernight
  • 36,122
  • 25
  • 118
  • 131
  • 2
    That's because there is no error here. d.isoweekday == 5 is a valid statement. – dekomote Sep 22 '10 at 12:45
  • 4
    @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. – Annika Backstrom Sep 22 '10 at 12:51
  • 5
    @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 Sep 26 '10 at 02:31
3

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
  • 3,817
  • 1
  • 17
  • 13