Skip to content

Commit

Permalink
chore: revert pydantic sugar to 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoppenheimer committed Jul 11, 2023
1 parent 07aa8e7 commit 4c1b9b2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
6 changes: 3 additions & 3 deletions lib/charms/data_platform_libs/v0/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ class MergedDataBag(ProviderDataBag, RequirerDataBag):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 2
LIBPATCH = 3

PYDEPS = ["ops>=2.0.0", "pydantic>=1.10"]
PYDEPS = ["ops>=2.0.0", "pydantic>=1.10,<2"]

G = TypeVar("G")
T = TypeVar("T", bound=BaseModel)
Expand Down Expand Up @@ -231,7 +231,7 @@ def write(relation_data: RelationDataContent, model: BaseModel):
relation_data: pointer to the relation databag
model: instance of pydantic model to be written
"""
for key, value in model.model_dump(exclude_none=True).items():
for key, value in model.dict(exclude_none=True).items():
relation_data[key.replace("_", "-")] = (
str(value) if isinstance(value, str) or isinstance(value, int) else json.dumps(value)
)
Expand Down
14 changes: 8 additions & 6 deletions lib/charms/data_platform_libs/v0/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
)
from ops.framework import EventBase, EventSource, Object
from ops.model import Relation, Unit
from pydantic import BaseModel, field_validator, model_validator
from pydantic import BaseModel, root_validator, validator

# The unique Charmhub library identifier, never change it
LIBID = "156258aefb79435a93d933409a8c8684"
Expand All @@ -39,7 +39,9 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 2
LIBPATCH = 3

PYDEPS = ["pydantic>=1.10,<2"]

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -272,7 +274,7 @@ class KafkaDependenciesModel(BaseModel):
upgrade_supported: str
version: str

@field_validator("dependencies", "upgrade_supported")
@validator("dependencies", "upgrade_supported", each_item=True)
@classmethod
def dependencies_validator(cls, value):
"""Validates values with dependencies for multiple special characters."""
Expand All @@ -291,7 +293,7 @@ def dependencies_validator(cls, value):

return value

@model_validator(mode="before")
@root_validator(skip_on_failure=True)
@classmethod
def version_upgrade_supported_validator(cls, values):
"""Validates specified `version` meets `upgrade_supported` requirement."""
Expand Down Expand Up @@ -476,7 +478,7 @@ def stored_dependencies(self) -> Optional[BaseModel]:
if not (deps := self.peer_relation.data[self.charm.app].get("dependencies", "")):
return None

return type(self.dependency_model).model_validate_json(deps)
return type(self.dependency_model)(**json.loads(deps))

@property
def upgrade_stack(self) -> Optional[List[int]]:
Expand Down Expand Up @@ -596,7 +598,7 @@ def _on_upgrade_created(self, event: RelationCreatedEvent) -> None:

logger.info("Setting charm dependencies to relation data...")
self.peer_relation.data[self.charm.app].update(
{"dependencies": self.dependency_model.model_dump_json()}
{"dependencies": json.dumps(self.dependency_model.dict())}
)

def _on_pre_upgrade_check_action(self, event: ActionEvent) -> None:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ops >= 2.1.1
pydantic>=1.10
pydantic>=1.10,<2
6 changes: 3 additions & 3 deletions tests/unit/test_data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ops.charm import ActionEvent, RelationEvent
from ops.testing import Harness
from pydantic import BaseModel, ValidationError, field_validator
from pydantic import BaseModel, ValidationError, validator

from charms.data_platform_libs.v0.data_models import (
BaseConfigModel,
Expand Down Expand Up @@ -52,7 +52,7 @@ class CharmConfig(BaseConfigModel):
float_config: float
low_value_config: int

@field_validator("low_value_config")
@validator("low_value_config")
@classmethod
def less_than_100(cls, value: int):
if value >= 100:
Expand Down Expand Up @@ -170,7 +170,7 @@ def test_action_params_parsing_ko(self):
with self.assertLogs(level="ERROR") as logger:
self.assertFalse(self.harness.charm._set_server_action(mock_event))
self.assertTrue("validation error" in logger.output[0])
self.assertTrue("Field required" in logger.output[0])
self.assertTrue("field required" in logger.output[0])

def test_relation_databag_io(self):
relation_id = self.harness.add_relation("database", "mongodb")
Expand Down

0 comments on commit 4c1b9b2

Please sign in to comment.