3

I'm working on a huge spaghetti code project and currently I've found many bugs where a string value is being compared to a boolean variable which is wrong.

Is there a python library which I can use like pylint which can spit out all errors or possible errors? Especially when a variable is overwritten with a new value.

Also, I found same type of errors a month later in the code.

What library can I use to catch all possible runtime problems?

E.g.:

var= "true"
bool_var= True

if var == bool_var:
  print("yes")
else:
  print("no")

The above example should give a warning or an error!

jkdev
  • 11,360
  • 15
  • 54
  • 77
jason
  • 3,932
  • 11
  • 52
  • 123
  • 1
    That's gonna be a lot of work, but you could use mypy to typecheck your codebase. You'll need to add type annotations everywhere in your code (okay, you don't *need* to add them everywhere, but the more the better) – Right leg Sep 20 '19 at 08:33
  • @Rightleg That's not an option as comparisons are happening in-between the code and not at the output level. It's very bad code written over 2yrs. Each file has more than 10,000 lines of code. and functions are written at macroscopic view.It has very bad metric on pylint 0.04 – jason Sep 20 '19 at 08:38
  • 1
    Your question is both too broad and asking for software recommendation. – sanyassh Sep 20 '19 at 09:02
  • 3
    Generally the answer to this question is *no*. The dynamic typing of Python gives you a lot of power, but it's up to the programmer to use this power responsibly. I think your best bet is to add type annotations as Right leg suggests. – orlp Sep 20 '19 at 09:03
  • "Especially, when variable is overwritten with a new value.": I'm not sure what you precisely mean here, but this happens all the time. That's why a variable is called "variable". You probably mean "inadvertently assigned to a new value", but that's really hard to check for. – 9769953 Sep 20 '19 at 09:12
  • It sounds like your code has many issues, I would re-write it from scratch – Chris_Rands Sep 20 '19 at 09:36
  • @Chris_Rands Well, that's the plan for next quarter but in the meantime we need to add a few new features to the code but it has issues like the one mentioned above already. I was looking for a library which can resolve issues like the one mentioned above. – jason Sep 20 '19 at 09:42
  • While not an automated solution, "Working Effectively With Legacy Code" by Michael Feathers concerns techniques for precisely this kind of scenario (the code examples are all C++/Java, but the techniques and principles are applicable to Python too). – snakecharmerb Sep 20 '19 at 09:49

0 Answers0