Skip to content

Commit 3ee6bd0

Browse files
[DPE-7309] Switch to Pydantic 2 and data_models v1 (#902)
* Switch to pydantic 2 * Apply suggestions from code review Co-authored-by: Marcelo Henrique Neppel <[email protected]> * Add optimizer_cpu_tuple_cost constraints * Remove JujuVersion warning * Sync to dpl repo --------- Co-authored-by: Marcelo Henrique Neppel <[email protected]>
1 parent e5c14a1 commit 3ee6bd0

File tree

8 files changed

+323
-865
lines changed

8 files changed

+323
-865
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ __pycache__/
88
coverage.xml
99
requirements.txt
1010
requirements-last-build.txt
11+
.last_refresh_unit_status.json
1112

1213
# PyCharm project folder.
1314
.idea/

lib/charms/data_platform_libs/v0/data_models.py renamed to lib/charms/data_platform_libs/v1/data_models.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023 Canonical Ltd.
1+
# Copyright 2024 Canonical Ltd.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -39,7 +39,7 @@
3939
4040
```python
4141
42-
from charms.data_platform_libs.v0.data_models import BaseConfigModel
42+
from charms.data_platform_libs.v1.data_models import BaseConfigModel
4343
4444
class MyConfig(BaseConfigModel):
4545
@@ -164,20 +164,27 @@ class MergedDataBag(ProviderDataBag, RequirerDataBag):
164164
LIBID = "cb2094c5b07d47e1bf346aaee0fcfcfe"
165165

166166
# Increment this major API version when introducing breaking changes
167-
LIBAPI = 0
167+
LIBAPI = 1
168168

169169
# Increment this PATCH version before using `charmcraft publish-lib` or reset
170170
# to 0 if you are raising the major API version
171-
LIBPATCH = 5
171+
LIBPATCH = 0
172172

173-
PYDEPS = ["ops>=2.0.0", "pydantic>=1.10,<2"]
173+
PYDEPS = ["ops>=2.0.0", "pydantic>=2,<3"]
174174

175175
G = TypeVar("G")
176176
T = TypeVar("T", bound=BaseModel)
177177
AppModel = TypeVar("AppModel", bound=BaseModel)
178178
UnitModel = TypeVar("UnitModel", bound=BaseModel)
179179

180-
DataBagNativeTypes = (int, str, float)
180+
DataBagNativeTypes = (
181+
int,
182+
str,
183+
float,
184+
Optional[int],
185+
Optional[str],
186+
Optional[float],
187+
)
181188

182189

183190
class BaseConfigModel(BaseModel):
@@ -233,7 +240,7 @@ def write(relation_data: RelationDataContent, model: BaseModel):
233240
relation_data: pointer to the relation databag
234241
model: instance of pydantic model to be written
235242
"""
236-
for key, value in model.dict(exclude_none=False).items():
243+
for key, value in model.model_dump(exclude_none=False).items():
237244
if value:
238245
relation_data[key.replace("_", "-")] = (
239246
str(value)
@@ -255,10 +262,10 @@ def read(relation_data: MutableMapping[str, str], obj: Type[T]) -> T:
255262
**{
256263
field_name: (
257264
relation_data[parsed_key]
258-
if field.outer_type_ in DataBagNativeTypes
265+
if field_info.annotation in DataBagNativeTypes
259266
else json.loads(relation_data[parsed_key])
260267
)
261-
for field_name, field in obj.__fields__.items()
268+
for field_name, field_info in obj.model_fields.items()
262269
# pyright: ignore[reportGeneralTypeIssues]
263270
if (parsed_key := field_name.replace("_", "-")) in relation_data
264271
if relation_data[parsed_key]

poetry.lock

Lines changed: 153 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pgconnstr = "^1.0.1"
1313
requests = "^2.32.3"
1414
tenacity = "^9.1.2"
1515
psycopg2 = "^2.9.10"
16-
pydantic = "^1.10.22"
16+
pydantic = "^2.11.4"
1717
jinja2 = "^3.1.6"
1818
pysyncobj = "^0.3.14"
1919
psutil = "^7.0.0"
@@ -23,7 +23,7 @@ charm-refresh = "^3.0.0.1"
2323
# data_platform_libs/v0/data_interfaces.py
2424
ops = ">=2.0.0"
2525
# data_platform_libs/v0/data_models.py
26-
pydantic = "^1.10"
26+
pydantic = "*"
2727
# grafana_agent/v0/cos_agent.py
2828
cosl = ">=0.0.50"
2929
# tls_certificates_interface/v2/tls_certificates.py

src/charm.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import psycopg2
2727
import tomli
2828
from charms.data_platform_libs.v0.data_interfaces import DataPeerData, DataPeerUnitData
29-
from charms.data_platform_libs.v0.data_models import TypedCharmBase
29+
from charms.data_platform_libs.v1.data_models import TypedCharmBase
3030
from charms.grafana_agent.v0.cos_agent import COSAgentProvider, charm_tracing_config
3131
from charms.operator_libs_linux.v2 import snap
3232
from charms.postgresql_k8s.v0.postgresql import (
@@ -43,7 +43,7 @@
4343
from charms.postgresql_k8s.v0.postgresql_tls import PostgreSQLTLS
4444
from charms.rolling_ops.v0.rollingops import RollingOpsManager, RunWithLock
4545
from charms.tempo_coordinator_k8s.v0.charm_tracing import trace_charm
46-
from ops import JujuVersion, main
46+
from ops import main
4747
from ops.charm import (
4848
ActionEvent,
4949
HookEvent,
@@ -261,8 +261,7 @@ def __init__(self, *args):
261261
deleted_label=SECRET_DELETED_LABEL,
262262
)
263263

264-
juju_version = JujuVersion.from_environ()
265-
run_cmd = "/usr/bin/juju-exec" if juju_version.major > 2 else "/usr/bin/juju-run"
264+
run_cmd = "/usr/bin/juju-exec"
266265
self._observer = ClusterTopologyObserver(self, run_cmd)
267266
self._rotate_logs = RotateLogs(self)
268267
self.framework.observe(self.on.cluster_topology_change, self._on_cluster_topology_change)

0 commit comments

Comments
 (0)