3

I'm using pyreverse to create UML class diagrams for my code base. On classes which are type-hinted using class variables (recommended according to (https://stackoverflow.com/a/59784221/14901431) the members appear twice since they are initialized in the init method.

Here is an example code:

class Example:
    var_a: int
    var_b: str
    
    def __init__(self, var_a, var_b) -> None:
        self.var_a = var_a
        self.var_b = var_b

This is the resulting UML diagram:

enter image description here

I have not seen any solution for this in the documentation. Any ideas on how to keep working with this type-hinting method and get UML diagrams without duplicates?

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
ira7bar
  • 31
  • 2

1 Answers1

2

This is a known issue of pyreverse:

  • Issue #8046 -> Opened in January, and still unresolved
  • Issue #8189 -> Opened in February, and still unresolved

There are two issues, depending on whether a value is assigned at class level or only a type annotation at class level. The co-author of pyreverse explains in the issue log the subtle difference: the value assignment at class level would cause two pair of variables exist: one at instance level and one at class level. This is why the first is issue is categorised as enhancement and the second as bug.

Once the issues are resolved, your code should produce a class with only two instance variables and the right annotation, whereas the following code would produce similar output to yours, but one of each duplicate would be marked as static (class variable):

class Example:
    var_a: int = 5 
    var_b: str = "six"
    
    def __init__(self, var_a, var_b) -> None:
        self.var_a = var_a
        self.var_b = var_b
        
    def test(self):
        print (self.var_a, self.var_b)
    
    @classmethod
    def classtest(cls):
        print (cls.var_a, cls.var_b)

The bad news is that there seem currently to be no ongoing work to solve those issues. So, meanwhile, the the workaround would probably be to comment out the definition at class level and embedded the type annotation in the init. I agree that this is far from ideal.

Christophe
  • 68,716
  • 7
  • 72
  • 138