1
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.

close2zero
  • 85
  • 8
  • Does this answer your question? [What's the best way to generate a UML diagram from Python source code?](https://stackoverflow.com/questions/260165/whats-the-best-way-to-generate-a-uml-diagram-from-python-source-code) – pippo1980 Oct 27 '21 at 16:56
  • 1
    What do you want to do? If you just want to parse code, you could probably use https://github.com/davidhalter/parso/ or any other Python parser. Jedi is using Parso internally to do that. If you only deal with syntactically valid code, you can use the `ast` module in the `stdlib` as well. – Dave Halter Oct 28 '21 at 19:59
  • Yes David, something like Parso or Jedi which has api to get base class information as well. Unfortunately, I could find any Api inside Jedi that is able to parse base class or if there's a different way to do it, I would like to know. What I mean is that information about class B using Jedi has no information about class A. – close2zero Oct 29 '21 at 05:25

1 Answers1

1

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)
neozen
  • 103
  • 7