pythonumlclass-diagrampyreverse

pyreverse not connecting classes in UML class diagram


I'm trying to create a UML diagram of a python project that shows inheritance relationships between classes using pyreverse.

Here's a small example to reproduce

Foo.py:

class Foo():
    def print_foo(self):
        print("hi")

Bar.py:

from Foo import Foo

class Bar(Foo):
    pass

__init__.py: empty

To generate the diagram, I'm running pyreverse -ASmy -o png .

The resulting diagrams look like this:

classes.png:

Class diagram

packages.png:

Package diagram

I would expect the class diagram to show an arrow connecting the two classes, but I can't figure out what I'm missing. Any ideas what's wrong here?

Output of pylint --version:

pylint 2.14.5
astroid 2.12.10
Python 3.10.5 (main, Jun  6 2022, 18:49:26) [GCC 12.1.0]

Solution

  • If you'd put all your classes in the same file, it works well:

    enter image description here

    When separating the files as you did, we obtain the result that you show. This suggest that it has to do with the import module name.

    If you replace the absolute module name with a relative module name:

    from .Foo import Foo
    
    ...
    

    Everything works as designed:

    enter image description here

    P.S: I'm not a python expert. So I could not tell if from Foo is incorrect and from .Foo is the way to go because it's a submodule of the current module, or if this is a pyreverse bug. But the pyreverse authors are sometimes here, and maybe they can make a statement. On this other question on composition, the reason was a bug that was expected to be solved in 2.14, but maybe it's a different issue ;-)