2

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
user32882
  • 5,094
  • 5
  • 43
  • 82

1 Answers1

4

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.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • I suspected type hints might be the way to go. Do you mind providing an example of my first version of `test.py` where it actually works? Thanks. – user32882 Dec 12 '21 at 13:48
  • @user32882 I would be glad to do it, but I'm not a Python programmer. Maybe something like `def __init__(self, identifier, name, address: Address):` would help you, but I'm not sure whether this is syntactically correct. I know that type hint is the solution, but I'm not experienced with Python. – Lajos Arpad Dec 12 '21 at 14:02
  • Hi @Lajos. Thanks. In that case I'll hold off accepting the answer until I actually try it with type hints and make sure it works. If anyone else can provide a working example in the meantime I'd be curious to see what it looks like. – user32882 Dec 12 '21 at 14:04
  • @user32882 sure thing. Please do try this and let me know about the results. – Lajos Arpad Dec 12 '21 at 14:12