Skip to content

Commit

Permalink
Upgraded to Pydantic 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
expobrain committed Mar 3, 2024
1 parent dbe0c87 commit 4e2b087
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
9 changes: 3 additions & 6 deletions komposer/types/base.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
from typing import cast

import stringcase
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict


def to_camel(string: str) -> str:
return cast(str, stringcase.camelcase(string))


class ImmutableBaseModel(BaseModel):
class Config:
allow_mutation = False
model_config = ConfigDict(frozen=True)


class CamelCaseImmutableBaseModel(BaseModel):
class Config:
allow_mutation = False
alias_generator = to_camel
model_config = ConfigDict(frozen=True, alias_generator=to_camel)
11 changes: 7 additions & 4 deletions komposer/types/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from typing import Any, Optional

from pydantic import validator
from pydantic import field_validator

from komposer.types.base import ImmutableBaseModel
from komposer.utils import load_yaml
Expand Down Expand Up @@ -44,20 +44,23 @@ class Context(ImmutableBaseModel):
deployment: DeploymentContext
ingress: IngressContext

@validator("project_name")
@field_validator("project_name")
@classmethod
def project_name_matches_kubernetes_name(cls, value: Optional[str]) -> Optional[str]:
if value is not None:
ensure_lowercase_kebab(value)

return value

@validator("branch_name")
@field_validator("branch_name")
@classmethod
def branch_name_matches_kubernetes_name(cls, value: str) -> str:
ensure_lowercase_kebab(value)

return value

@validator("repository_name")
@field_validator("repository_name")
@classmethod
def repository_name_matches_kubernetes_name(cls, value: str) -> str:
ensure_lowercase_kebab(value)

Expand Down
8 changes: 4 additions & 4 deletions komposer/types/kubernetes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from abc import ABC
from collections.abc import Iterable
from enum import Enum, unique
from io import StringIO
Expand Down Expand Up @@ -68,7 +69,7 @@ def from_context_with_name(
)


class Item(CamelCaseImmutableBaseModel):
class Item(CamelCaseImmutableBaseModel, ABC):
api_version: str
kind: str
metadata: Metadata
Expand Down Expand Up @@ -117,7 +118,7 @@ def from_env_file(

class EnvironmentVariable(CamelCaseImmutableBaseModel):
name: str
value: Optional[str]
value: Optional[str] = None

@staticmethod
def from_string(string: str) -> Iterable[EnvironmentVariable]:
Expand Down Expand Up @@ -255,5 +256,4 @@ class Ingress(Item):
class List(CamelCaseImmutableBaseModel):
api_version: Literal["v1"] = "v1"
kind: Literal["List"] = "List"
# We should be able to use Field(..., discriminator="kind") here
items: list[Item] = []
items: list[Union[ConfigMap, Deployment, Service, Ingress]] = []
2 changes: 1 addition & 1 deletion komposer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ def command_to_args(command: Optional[Union[str, list[str]]]) -> Optional[list[s


def as_json_object(type_: BaseModel) -> dict[str, Any]:
return cast(dict[str, Any], json.loads(type_.json(by_alias=True)))
return cast(dict[str, Any], json.loads(type_.model_dump_json(by_alias=True)))

0 comments on commit 4e2b087

Please sign in to comment.