2

I want to automatically generate a UML class diagram based on my Python(3) source code. The challenge in this case is that not all information is available at the time of instantiation of the object. Some attributes have to be assigned later, therefore when the object is initially created, some attributes are of type None. Later they will have the type that is hinted at. Now I want the UML to show the type of the hinting not the type that is initially assigned at object creation.

The following source:

from typing import List

class Test:
    def __init__(self):
        self.a: int = 0
        self.b: List[str] = None

produces the following UML:

enter image description here

Now I want for attribute b to have the type List[str] in the UML and override what pylint thinks will be the type of b. Is there a way to achive this or do I have to manually draw my UML?

Thank you very much :).

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164

1 Answers1

1

Works now, maybe 12 months ago it didn't

enter image description here

Second off, that is an invalid type, run mypy and it will tell you. At best it should be Optional[List[str]]. Even better is to initialize it to []

As for what is going on behind the scenes-

Some tools that work directly with python source code will import or run the code and then manipulate live objects. Pydoc is an example.

As far as I can tell, pyreverse uses astroid, which just parse the code and doesn't attempt to run it, instead it makes certain "inferences". So it wouldn't be expected to pick up everything dynamic, such a variable whose type changes while the app is running.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164