Say I have the following code in code.py:
from foo import bar
from foo2 import bar2
from foo3 import wrapper
class A(bar):
def m1(self,x):
return wrapper(x)
Then I have a reference to an A object in the var “obj” and I get the code for that object like so:
c = inspect.getsource(obj)
which returns:
class A(bar):
pass
I need to be able to parse “inspect.getsource(obj)” to know that the A class depends on the “from foo import bar” and “from foo3 import wrapper” at the top of the file returned by “inspect.getsourcefile(obj)”.
How can I achieve this?
The only way to get close that I know of is by using modulefinder, but it runs on the whole file and would give me all three imports, not just the one used by the code of interest.
~~~~~~~~~~~~~~~~~~~~~~~~~~
Edit: updated definition
~~~~~~~~~~~~~~~~~~~~~~~~~~
If you want to check if an object or a class inherits from a class in another module, using
inspectis probably the wrong way. You can instead use something like that:Or for an object: