lucsorel / py2puml

Generate PlantUML class diagrams to document your Python application.
https://pypi.org/project/py2puml/
MIT License
221 stars 35 forks source link

typing.Literal recognition is unsupported #91

Open seppzer0 opened 6 months ago

seppzer0 commented 6 months ago

Hi!

First of all, really liked the idea of the project. Auto-generating plantuml documentation straight from Python code is quite nice.

I've attempted to check out py2puml on my project, but happened to encounter this error:

ValueError: typing.Literal['val1', 'val2'] seems to be an invalid type annotation

I'm assuming py2puml is currently unable to recognize literals (which essentially are a form of enums), and based on this PR it also can't yet work with Pydantic models, which subjectively is even more concerning.

Is there a chance for the missing type (and potentially other types I haven't gotten an issue with yet) to be added to py2puml?

Again, really like the idea, can see myself using this very often if it works out :)

lucsorel commented 6 months ago

hello @seppzer0 , thank you for your interest in this project :slightly_smiling_face:

Indeed, py2puml does not handle Literal (nor LiteralString) types yet. Could you provide examples of some python code involving literal types and the plantuml output one would expect, please?

Do not hesitate to use links to plantuml.com UML samples.

Also mind that I have limited time to maintain this project (I feel sorry for the number of issues that are waiting), I just cannot handle coding them all (even reviewing pull requests).

seppzer0 commented 6 months ago

@lucsorel ,

Could you provide examples of some python code involving literal types and the plantuml output one would expect, please?

Sure. Below is an example of Python code:

class AssetsCommand(BaseModel):
    codename: str
    base: str
    chroot: Literal["full", "minimal"]
    clean_assets: bool
    rom_only: bool
    ksu: bool

...which in PlantUML format should be outputed as something like this:

class "AssetsCommand" as commands.assets.AssetsCommand {
  base : str
  chroot : Literal['full', 'minimal']
  clean_assets : bool
  codename : str
  ksu : bool
  rom_only : bool
}

should the the literal values be converted in an enum?

Literal is a perfectly valid Python type. In my opinion, Literal in Python code should be recognized as Literal, and Enum should be recognized as Enum. Even if both are very similar on what they do.

if so, what name should the enum type have?

Answered above.

what would happen if some different type annotations involve the same literal values but in different orders?

If I understood this correctly, let's say we have Literal["val1", "val2"] and Literal["val2", "val1"] usages.

Both usages can be recognized as essentially the same type, but I would say that if they are recognized as technically different types, it would be ok. Bear in mind that this is just my personal opinion, and I'm not sure if this can cause an issue in the future.

or should the literal types be converted in a union type (def func(mode: Literal['read', 'write']): -> (def func(mode: str):)?

I would say definitely no. Once again I think that preserving the original type (even with different literal values order in it) is important.

lucsorel commented 6 months ago

Thank you for your answers, with which I agree. I misunderstood the comparison between the literals and an enum; but leaving chroot : Literal['full', 'minimal'] in the PlantUML generated contents makes sense to me.

I cannot say if or when I could do that, reviewing pull requests would take me less time if you feel like giving it a try.