Python source analyzer which is able to detect TypeError when using `in` with dict.get()
Hi there! Recently I submitted such piece of code to one of my projects: if 'text' in d.get('a'): # do something Obviously that was expected to be something like: if 'text' in d.get('a', ''): # do something because .get() might return None, and checking if something is contained in None gives you TypeError. Maybe it was too late, I was trying to finish this piece of code asap and I expected flakes8 to detect this for me, so when PR became green I merged it. When I realized this piece of code is buggy it was too late :) As I don't want to repeat this mistake I decided to take measures. As I was using flakes8 at the moment and it didn't notice this bug - I thought "Hm, maybe I should use some better tool, maybe pyflakes is too simple to catch that". So I tried pylint and prospector. They do almost everything, I believe both tools can bring you coffee in the bed. But they cannot detect such a simple error. No success. And that makes me really unhappy. Do you guys know any Python code analyzer which is able to catch such problem? Is there a chance this can be implemented in pyflakes? If so - should I create bug on https://bugs.launchpad.net/pyflakes/+filebug? -- Best regards, Alexander Chekunkov
* Александр Чекунков <chekunkov@gmail.com> [2015-08-01 20:13:34 +0300]:
Hi there!
Recently I submitted such piece of code to one of my projects:
if 'text' in d.get('a'): # do something
Obviously that was expected to be something like:
if 'text' in d.get('a', ''): # do something
because .get() might return None, and checking if something is contained in None gives you TypeError. Maybe it was too late, I was trying to finish this piece of code asap and I expected flakes8 to detect this for me, so when PR became green I merged it. When I realized this piece of code is buggy it was too late :)
As I don't want to repeat this mistake I decided to take measures. As I was using flakes8 at the moment and it didn't notice this bug - I thought "Hm, maybe I should use some better tool, maybe pyflakes is too simple to catch that".
pyflakes is indeed rather simple (compared to pylint). In return it's much faster and reporting less noise ;) Of course your best bet is to write unittests - static analysis will never be a substitute for them :)
So I tried pylint and prospector. They do almost everything, I believe both tools can bring you coffee in the bed. But they cannot detect such a simple error. No success. And that makes me really unhappy. Do you guys know any Python code analyzer which is able to catch such problem? Is there a chance this can be implemented in pyflakes? If so - should I create bug on https://bugs.launchpad.net/pyflakes/+filebug?
I'm not too aware on how pyflakes works internally, but this seems more like a candidate for pylint than for pyflakes for me. Note it's not *that* simple - the tool has to find out that: - d is a dictionary - dict.get can possibly return None - NoneType doesn't implement __contains__. See https://bitbucket.org/logilab/pylint/issues/589/ for the third part. And all that without running any of the code. I'm guessing Claudiu Popa of pylint will chime in later as well (he reads this list as well), and he'll probably be able to tell you which one of those is missing. Florian -- http://www.the-compiler.org | me@the-compiler.org (Mail/XMPP) GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc I love long mails! | http://email.is-not-s.ms/
participants (2)
-
Florian Bruhin
-
Александр Чекунков