def foo():
while <condition>:
do something
def bar():
for i in range(5):
do something
Suppose I have two functions defined above in a file name test.py. Is there a way in python to write a function with the following behavior?
import test
def uses_while(fn: Callable) -> bool:
(what goes here?)
>>> uses_while(test.foo)
True
>>> uses_while(test.bar)
False
I essentially need to programatically check if a function makes use of a while loop, without inspecting the code by hand. I've thought of using pdb.getsourcelines(), but that doesn't work if there are comments or strings with the word 'while' inside. Any ideas?