开发者

Python code checker for comparing a function as an attribute

I occasionally spend a considerable amount of time tracking down brainfarts in my code... while I normally run pylint against it, there are some things that slip past pylint. The easiest开发者_如何学Python problem for me to overlook is this...

# normally, variable is populated from parsed text, so it's not predictable
variable = 'fOoBaR'
if variable.lower == 'foobar':
    #       ^^^^^<------------------ should be .lower()
    do_something()

Neither pylint nor Python bark about this... is there a python code-checking tool that can flag this particular issue?


How do you propose a code-checker validate this? It's perfectly legitimate syntax. Rather than checking for this kind of mistake, it would be better to get into the habit of using better patterns.

Instead of:

variable = 'fOoBaR'
if variable.lower == 'foobar':
    #       ^^^^^<------------------ should be .lower()
    do_something()

Do this:

variable = 'fOoBaR'
sane_variable = variable.lower()
if sane_variable == 'foobar':
    do_something()

This way you're always explicitly calling .lower() on the value you're comparing against, instead of relying on an in-place method-call and comparison, which leads to the very pitfall you're experiencing.


@Mike Pennington I just want to first say that I also run into this a lot -.-

@eyquem 'lower()' is a function. 'lower' is a function pointer (if I'm not mistaken). Python will let you attempt to run this code, but it will not invoke the function.

I think the reason this is hard to catch is that you don't always know the type of the variable which you're calling methods on. For example, say I have 2 classes.

class Foo()
   def func(self):
      #do stuff
      pass

class Bar()
   self.func = "stuff"

If your code has a function in it that takes an argument 'baz' like so:

def myfunction(baz):
    print baz.func

def myfunction(baz):
    baz.func()

Either one of these could be valid depending on baz's type. There is literally no way of knowing if baz is of type 'Foo' or 'Bar', though.

EDIT: I meant with static analysis...


This is pylint ticket #65910

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜