pythonooppylintcompositepyreverse

Pyreverse doesn't detect that the parent class is a Composite... why?


Suppose I have the following directory structure:

Folder PATH listing
Volume serial number is 986C-80E1
C:.
    test.py
    __init__.py
    
No subfolders exist 

__init__.py is blank. In test.py. I have the following code:

class Employee:
    def __init__(self, identifier, name, address):
        self.address = None

class Manager(Employee):
    pass

class Secretary(Employee):
    pass

class Address:
    def __init__(self, street_num, street_name):
        self.street_num = street_num
        self.street_name = street_name

The idea here is to implement simple one-to-many composition. That is, all Employee subtypes also contain an Address instance.

I then run pyreverse -S -o uml.png . to generate a UML class diagram and get the following:

enter image description here

pyreverse doesn't recognize that there is a composite-component relationship between Employee and Address. However, if I refactor test.py to the following:

class Employee:
    def __init__(self, identifier, name):
        self.address = None

class Manager(Employee):
    pass

class Secretary(Employee):
    pass

class Address:
    def __init__(self, street_num, street_name):
        self.street_num = street_num
        self.street_name = street_name

bob = Manager(1, "Bob")
bob.address =  Address(1, "main ave")

I get that Address has an association with Manager.

enter image description here

Is this not technically incorrect? The association, as far as I understand, should be with the parent class Employee. Why does pyreverse consider that Address is only a component of Manager?


Solution

  • pyreverse detects that you have instantiated Manager and this is why it considers Address to be part of Manager. If you make a type hint in your constructor, then pyreverse should detect the correct relationship between Employee and Address.