EricWebsmith / pydantic_mermaid

convert Pydantic models to Mermaid charts
MIT License
15 stars 2 forks source link

Do not work with Literal #4

Open gameliee opened 4 months ago

gameliee commented 4 months ago

Thank you for the great project.

One thing I would love to be fixed, is that make the project works with Literal data types. Here is a minimal example

import sys
from typing import Literal

from pydantic import BaseModel
from pydantic_mermaid import MermaidGenerator, Relations

class OverdraftInterestRateHistory(BaseModel):
    """For update history rate"""
    id: int
    type: Literal['once', 'freq']

if __name__ == "__main__":
    current_module = sys.modules[__name__]
    mg = MermaidGenerator(current_module)
    chart = mg.generate_chart(relations=Relations.Inheritance)

    print(chart)

When running the above example, this error raised

$ python test.py 
Traceback (most recent call last):
  File "test.py", line 15, in <module>
    mg = MermaidGenerator(current_module)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\pydantic_mermaid\mermaid_generator.py", line 27, in __init__
    self.graph: MermaidGraph = PydanticParser()(module)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\pydantic_mermaid\pydantic_parser.py", line 102, in __call__
    graph.service_clients[class_name] = graph.service_clients[class_name] | _get_field_dependencies(
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\pydantic_mermaid\pydantic_parser.py", line 42, in _get_field_dependencies
    ans |= _get_dependencies(field.type_)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\pydantic_mermaid\pydantic_parser.py", line 32, in _get_dependencies
    return {v.__name__} - constrained_types
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\typing.py", line 760, in __getattr__
    raise AttributeError(attr)
AttributeError: __name__

I would love to contribute, just point me a direction. Thanks.

EricWebsmith commented 4 months ago

Oh, I see, the error happens in _get_dependencies. And we do not process this if it is literal. If you can detect literal and then return empty set, I would be happy to merge it.