Skip to content

Commit

Permalink
[Tidy] Bump to Pydantic V2 (#917)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
maxschulz-COL and pre-commit-ci[bot] authored Jan 31, 2025
1 parent 0bad37a commit 87e7653
Show file tree
Hide file tree
Showing 76 changed files with 2,215 additions and 2,029 deletions.
7 changes: 2 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ repos:
additional_dependencies: ["bandit[toml]"]

- repo: https://github.com/pre-commit/mirrors-mypy
# Upgrade to v1.11.1 not possible as it doesn't seem compatible with pydantic<2 plugin.
# Similar issue with previous v.1.11.X versions: https://github.com/pydantic/pydantic/issues/10000
# We need to revert the changes from the pre-commit autoupdate for now.
rev: v1.10.1
rev: v1.14.1
hooks:
- id: mypy
files: ^vizro-core/src/
Expand All @@ -72,7 +69,7 @@ repos:
# pydantic>=1.10.15 includes this fix which flags some genuine type problems. These will take a while to fix
# or ignore so for now we just pin to 1.10.14 which doesn't flag the problems.
# https://github.com/pydantic/pydantic/pull/8765
- pydantic==1.10.14
- pydantic==2.9.0

- repo: https://github.com/awebdeveloper/pre-commit-stylelint
rev: "0.0.2"
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ skip = "*.min.css.map,*.min.css,.vale/*, *assets/*,.github/*"
[tool.mypy]
# strict checks : strict = true
check_untyped_defs = true
disable_error_code = "call-arg" # https://github.com/python/mypy/issues/14850 seems to not fix it
disallow_any_generics = true
disallow_incomplete_defs = false
disallow_subclassing_any = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->


### Highlights ✨

- Vizro now uses pydantic V2 instead of V1 for its models. This is a big change, but it does not affect the API defined by the models.
However, when interacting with the VIzro models, e.g. in custom components or when calling pydantic V1 methods, code may break.
Please contact us in case you detect a bug or something breaks unexpectedly! ([#917](https://github.com/mckinsey/vizro/pull/917))


<!--
### Removed
- A bullet item for the Removed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX. ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Added
- A bullet item for the Added category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX. ([#1](https://github.com/mckinsey/vizro/pull/1))
-->

<!-- ### Changed
- Moved from pydantic V1 to V2. This is a big change, but should not affect anything user-facing. Please contact us in case of bug! ([#917](https://github.com/mckinsey/vizro/pull/917)) -->


<!--
### Deprecated
- A bullet item for the Deprecated category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX. ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Fixed
- A bullet item for the Fixed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX. ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Security
- A bullet item for the Security category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX. ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
28 changes: 15 additions & 13 deletions vizro-core/docs/pages/user-guides/custom-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ Add the custom action `open_offcanvas` as a `function` argument inside the [`Act
],
),
OffCanvas(
id="offcanvas",
content="OffCanvas content",
title="Offcanvas Title",
),
Expand Down Expand Up @@ -402,18 +403,12 @@ As mentioned above, custom components can trigger action. To enable the custom c

=== "app.py"
```py
from typing import Literal
from typing import Annotated, Literal

import dash_bootstrap_components as dbc
import vizro.models as vm
from dash import html
from pydantic import AfterValidator, Field, PlainSerializer
from vizro import Vizro

try:
from pydantic.v1 import Field, PrivateAttr
except ImportError:
from pydantic import PrivateAttr

from vizro.models import Action
from vizro.models._action._actions_chain import _action_validator_factory
from vizro.models.types import capture
Expand All @@ -423,9 +418,14 @@ As mentioned above, custom components can trigger action. To enable the custom c
class Carousel(vm.VizroBaseModel):
type: Literal["carousel"] = "carousel"
items: list
actions: list[Action] = []
# Here we set the action so a change in the active_index property of the custom component triggers the action
_set_actions = _action_validator_factory("active_index")
actions: Annotated[
list[Action],
# Here we set the action so a change in the active_index property of the custom component triggers the action
AfterValidator(_action_validator_factory("active_index")),
# Here we tell the serializer to only serialize the actions field
PlainSerializer(lambda x: x[0].actions),
Field(default=[]),
]

def build(self):
return dbc.Carousel(
Expand All @@ -437,6 +437,7 @@ As mentioned above, custom components can trigger action. To enable the custom c
# 2. Add new components to expected type - here the selector of the parent components
vm.Page.add_type("components", Carousel)


# 3. Create custom action
@capture("action")
def slide_next_card(active_index):
Expand All @@ -445,6 +446,7 @@ As mentioned above, custom components can trigger action. To enable the custom c

return "First slide"


page = vm.Page(
title="Custom Component",
components=[
Expand All @@ -459,9 +461,9 @@ As mentioned above, custom components can trigger action. To enable the custom c
vm.Action(
function=slide_next_card(),
inputs=["carousel.active_index"],
outputs=["carousel-card.children"]
outputs=["carousel-card.children"],
)
]
],
),
],
)
Expand Down
2 changes: 1 addition & 1 deletion vizro-core/examples/dev/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ def multiple_cards(data_frame: pd.DataFrame, n_rows: Optional[int] = 1) -> html.
components = [graphs, ag_grid, table, cards, figure, button, containers, tabs]
controls = [filters, parameters, selectors]
actions = [export_data_action, chart_interaction]
extensions = [custom_charts, custom_tables, custom_components, custom_actions, custom_figures]
extensions = [custom_charts, custom_tables, custom_actions, custom_figures, custom_components]

dashboard = vm.Dashboard(
title="Vizro Features",
Expand Down
26 changes: 12 additions & 14 deletions vizro-core/examples/scratch_dev/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@
import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro
from vizro.figures import kpi_card
from vizro.actions import export_data

df = px.data.iris()

tips = px.data.tips

# Create a layout with five rows and four columns. The KPI card is positioned in the first cell, while the remaining cells are empty.
page = vm.Page(
title="KPI card",
layout=vm.Layout(grid=[[0, 0, -1, -1]] + [[-1, -1, -1, -1]] * 2),
title="Page 1",
components=[
vm.Figure(
figure=kpi_card( # For more information, refer to the API reference for kpi_card
data_frame=tips,
value_column="tip",
value_format="${value:.2f}",
icon="folder_check",
title="KPI card I",
)
)
vm.Graph(figure=px.bar(df, x="sepal_width", y="sepal_length")),
vm.Button(
text="Export data",
actions=[
vm.Action(function=export_data()),
vm.Action(function=export_data()),
],
),
],
controls=[vm.Filter(column="day", selector=vm.RadioItems())],
)

dashboard = vm.Dashboard(pages=[page])
Expand Down
9 changes: 2 additions & 7 deletions vizro-core/examples/visual-vocabulary/custom_components.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
"""Contains custom components used inside the dashboard."""

from typing import Literal
from urllib.parse import quote

import dash_bootstrap_components as dbc
import vizro.models as vm
from dash import dcc, html

try:
from pydantic.v1 import Field
except ImportError: # pragma: no cov
from pydantic import Field

from urllib.parse import quote
from pydantic import Field


class CodeClipboard(vm.VizroBaseModel):
Expand Down
2 changes: 1 addition & 1 deletion vizro-core/hatch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ VIZRO_LOG_LEVEL = "DEBUG"

[envs.lower-bounds]
extra-dependencies = [
"pydantic==1.10.16",
"pydantic==2.9.0",
"dash==2.18.0",
"plotly==5.12.0",
"pandas==2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion vizro-core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies = [
"dash-ag-grid>=31.0.0",
"pandas>=2",
"plotly>=5.12.0",
"pydantic>=1.10.16", # must be synced with pre-commit mypy hook manually
"pydantic>=2.9.0", # must be synced with pre-commit mypy hook manually
"dash_mantine_components~=0.15.1",
"flask_caching>=2",
"wrapt>=1",
Expand Down
Loading

0 comments on commit 87e7653

Please sign in to comment.