What library or framework can be used to parse Python file and extract base classes as well as methods inside them

125 views Asked by At
class A:
    def m1():
        // in a.m1
        pass
class B(A):
    def m2():
        // in b.m2

Parsing above code shall give me following info - class names - B, base -> A, method names -> a.m1, b.m2

I have looked into Jedi, but I don't see any Api to extract above information.

1

There are 1 answers

0
neozen On

You might find the standard library pyclbr module useful to determine classes, methods, and parent classes in a module. here's its docs

Its _main function should help with some example usage and help get you to a solution (Running python3 -m inspect pyclbr will show you the source code of the pyclbr module so you can see _main).

Here's the output of the _main function being run against pyclbr itself:

$ python3 -m pyclbr pyclbr
class _Object [] 53
  def __init__ 55
class Function [<__main__.Class object at 0x1047e56d0>] 68
  def __init__ 70
class Class [<__main__.Class object at 0x1047e56d0>] 78
  def __init__ 80
def _nest_function 89
def _nest_class 94
def readmodule 100
def readmodule_ex 112
def _readmodule 122
class _ModuleBrowser [<__main__.Class object at 0x1047e6410>] 186
  def __init__ 187
  def visit_ClassDef 195
  def visit_FunctionDef 220
  def visit_AsyncFunctionDef 230
  def visit_Import 233
  def visit_ImportFrom 248
def _create_tree 269
def _main 275

You'll probably need to access the super attributes of the Class and readmodule_ex returns to determine the base class info you want to output.

Pros:

  • doesn't actually execute the module code the way inspect requires one to (so, in theory, you're safer running it against code you don't trust)
  • quite fast

Cons:

  • sometimes reports classes / functions from imported modules. (For my purposes this hasn't mattered)