2

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]
karpfen
  • 489
  • 8
  • 19

1 Answers1

4

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 ;-)

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • 1
    This is indeed primarily a problem with the import statement. The way it is written in the opening post, module `Bar.py` can only be loaded correctly if Python is invoked from within the directory where `Foo.py` and `Bar.py` are located. Using the relative import you explained would be one way to solve this. Absolute imports would be the other option. Nevertheless as the original example works fine as normal Python code, I opened an issue for this: https://github.com/PyCQA/pylint/issues/7686 – dudenr33 Oct 28 '22 at 19:31
  • @dudenr33 Thank you very much for this enlightening feedback. – Christophe Oct 28 '22 at 20:44