fzyzcjy / grafana_dashboard_python

Write Grafana dashboards in Python, without losing thousands of dashboards in the zoo
MIT License
50 stars 7 forks source link

Too high Pydantic version #1

Closed thevops closed 11 months ago

thevops commented 11 months ago

My virtual environment, after pip install grafana_dashboard:

> pip freeze
annotated-types==0.5.0
click==8.1.4
grafana-dashboard==0.1.1
markdown-it-py==3.0.0
mdurl==0.1.2
pydantic==2.0.2
pydantic_core==2.1.2
Pygments==2.15.1
rich==13.4.2
typer==0.9.0
typing_extensions==4.7.1

Error:

> grafana_dashboard json-to-python --json-path ./mydashboard.json --python-path ./mydashboard.py
Traceback (most recent call last):
  File "/tmp/.venv/bin/grafana_dashboard", line 5, in <module>
    from grafana_dashboard.grafana_dashboard import run
  File "/tmp/.venv/lib/python3.11/site-packages/grafana_dashboard/grafana_dashboard.py", line 5, in <module>
    import grafana_dashboard.converter.json_to_python
  File "/tmp/.venv/lib/python3.11/site-packages/grafana_dashboard/converter/json_to_python.py", line 8, in <module>
    from grafana_dashboard.model.dashboard_types_gen import Dashboard
  File "/tmp/.venv/lib/python3.11/site-packages/grafana_dashboard/model/dashboard_types_gen.py", line 11, in <module>
    from grafana_dashboard.model import timeseriespanelcfg_types_gen, piechartpanelcfg_types_gen
  File "/tmp/.venv/lib/python3.11/site-packages/grafana_dashboard/model/timeseriespanelcfg_types_gen.py", line 124, in <module>
    class TimeZone(MyBaseModel):
  File "/tmp/.venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 95, in __new__
    private_attributes = inspect_namespace(
                         ^^^^^^^^^^^^^^^^^^
  File "/tmp/.venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 279, in inspect_namespace
    raise TypeError("To define root models, use `pydantic.RootModel` rather than a field called '__root__'")
TypeError: To define root models, use `pydantic.RootModel` rather than a field called '__root__'

Temporary solution (downgrade pydantic):

pip install pydantic==1.10.2

I think that something has changed in pydantic library.

By the way, nice tool :) thanks

fzyzcjy commented 11 months ago

You are welcome! Oops, pydantic v2 seems incompatible with v1. Feel free to PR to change it (should be a good first issue), e.g. fixing version to 1.x

skoenig commented 2 weeks ago

Sorry to revive this old issue, but still the same problem occur when I create a virtualenv with pyenv, using python 3.10 like this:

pyenv local 3.10
pyenv virtualenv 3.10 grafanalib
pyenv activate grafanalib
pip install grafana_dashboard
...
grafana_dashboard json-to-python --json-path ~/workspace/ionos/helm-charts/charts/base-cluster-service/dashboards/container-image-versions-overview.json --python-path . 
Traceback (most recent call last):
  File "/home/skoenig/.pyenv/versions/grafanalib/bin/grafana_dashboard", line 5, in <module>
    from grafana_dashboard.grafana_dashboard import run
  File "/home/skoenig/.pyenv/versions/3.10.14/envs/grafanalib/lib/python3.10/site-packages/grafana_dashboard/grafana_dashboard.py", line 5, in <module>
    import grafana_dashboard.converter.json_to_python
  File "/home/skoenig/.pyenv/versions/3.10.14/envs/grafanalib/lib/python3.10/site-packages/grafana_dashboard/converter/json_to_python.py", line 8, in <module>
    from grafana_dashboard.model.dashboard_types_gen import Dashboard
  File "/home/skoenig/.pyenv/versions/3.10.14/envs/grafanalib/lib/python3.10/site-packages/grafana_dashboard/model/dashboard_types_gen.py", line 11, in <module>
    from grafana_dashboard.model import timeseriespanelcfg_types_gen, piechartpanelcfg_types_gen
  File "/home/skoenig/.pyenv/versions/3.10.14/envs/grafanalib/lib/python3.10/site-packages/grafana_dashboard/model/timeseriespanelcfg_types_gen.py", line 124, in <module>
    class TimeZone(MyBaseModel):
  File "/home/skoenig/.pyenv/versions/3.10.14/envs/grafanalib/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 93, in __new__
    private_attributes = inspect_namespace(
  File "/home/skoenig/.pyenv/versions/3.10.14/envs/grafanalib/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 349, in inspect_namespace
    raise TypeError("To define root models, use `pydantic.RootModel` rather than a field called '__root__'")
TypeError: To define root models, use `pydantic.RootModel` rather than a field called '__root__'

Seems like the upper version limit for Pydantic defined in #3 is not taking effect:

pip list
Package           Version
----------------- -------
annotated-types   0.7.0
click             8.1.7
grafana-dashboard 0.1.1
markdown-it-py    3.0.0
mdurl             0.1.2
pip               24.0
pydantic          2.7.4
pydantic_core     2.18.4
Pygments          2.18.0
rich              13.7.1
setuptools        70.0.0
shellingham       1.5.4
typer             0.12.3
typing_extensions 4.12.2
wheel             0.43.0

If I downgrade to the Pydantic version as defined in setup.py with pip install 'pydantic>=1.10.2,<2', I get the dashboard defined in Python code.

fzyzcjy commented 2 weeks ago

Hmm, what about using the code on master branch, does it work for you?