Finding Python base classes with static analysis and Jedi

129 Views Asked by At

Note: I don't consider this a duplicate of questions such as List all base classes in a hierarchy of given class?, because I don't have the ability to instantiate classes at run-time. The focus here is on static analysis, not dynamic.

I'm using the Jedi tool for some static analysis on internal source code. Can I use it to help me identify all the classes that a given class inherits from?

For example, here's a script called utils.py:

class UtilBase:
    pass


class SampleUtil(UtilBase):
    pass

Here's the code to ingest it:

import jedi

script = jedi.Script('utils.py')
names = script.get_names()
sample = names[1]

# shows "utils"
print(sample.parent())

# shows "SampleUtil()"
print(sample.get_signatures()[0].to_string())

What I want is a way to get the Name "UtilBase" from sample.

0

There are 0 best solutions below