Skip to content

Commit

Permalink
Migrate to pydantic>=2.5 (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
spillai authored Jan 23, 2024
1 parent aa3e8ce commit aff1f67
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 19 deletions.
28 changes: 16 additions & 12 deletions agipack/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Dict, List, Optional, Tuple, Union

import yaml
from pydantic import Extra, validator
from pydantic import ConfigDict, field_validator
from pydantic.dataclasses import dataclass

logger = logging.getLogger(__name__)
Expand All @@ -28,13 +28,7 @@ def is_leaf(self) -> bool:
return not len(self.children)


class _ForbidExtrasConfig:
"""Pydantic config to forbid extra fields."""

extra = Extra.forbid


@dataclass(config=_ForbidExtrasConfig)
@dataclass(config=ConfigDict(extra="forbid"))
class ImageConfig:
"""AGIPack configuration for a docker target specified in `agibuild.yaml`
Expand Down Expand Up @@ -128,7 +122,7 @@ def is_base_image(self) -> bool:
"""Check if the base target is root / does not have a parent."""
return ":" in self.base

@validator("python", pre=True)
@field_validator("python", mode="before")
def validate_python_version(cls, python) -> str:
"""Validate the python version."""
if not isinstance(python, str):
Expand All @@ -137,7 +131,7 @@ def validate_python_version(cls, python) -> str:
raise ValueError(f"Python version must be >= 3.6 (found {python})")
return python

@validator("add", pre=True)
@field_validator("add", mode="before")
def validate_add(cls, items) -> Tuple[str, str]:
"""Validate the add command."""
for item in items:
Expand All @@ -150,7 +144,7 @@ def validate_add(cls, items) -> Tuple[str, str]:
raise ValueError(f"`add` {from_path} does not exist")
return items

@validator("command", pre=True)
@field_validator("command", mode="before")
def validate_command_version(cls, cmd) -> List[str]:
"""Validate the command."""
if isinstance(cmd, str):
Expand All @@ -159,6 +153,16 @@ def validate_command_version(cls, cmd) -> List[str]:
pass
return cmd

@field_validator("env", mode="before")
def validate_env(cls, env) -> Dict[str, str]:
"""Validate the environment variables."""
if not isinstance(env, dict):
raise ValueError(f"`env` must be a dictionary (type={type(env)})")
for key, value in env.items():
if isinstance(value, int):
env[key] = str(value)
return env


@dataclass
class AGIPackConfig:
Expand Down Expand Up @@ -204,7 +208,7 @@ def is_prod(self) -> bool:
"""Check if the configuration is for production."""
return self.prod

@validator("images")
@field_validator("images")
def validate_python_dependencies_for_nonbase_images(cls, images):
"""Validate that all images have the same python dependency as the base image."""
py_version = None
Expand Down
2 changes: 1 addition & 1 deletion agipack/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.19"
__version__ = "0.2.0"
2 changes: 1 addition & 1 deletion examples/generated/Dockerfile-base-cpu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# >>>>>>>>>>>>>>>>>>>>>>>>>>>
# Auto-generated by agi-pack (version=0.1.19).
# Auto-generated by agi-pack (version=0.2.0).
FROM debian:buster-slim AS base-cpu

# Setup environment variables
Expand Down
2 changes: 1 addition & 1 deletion examples/generated/Dockerfile-base-cu118
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# >>>>>>>>>>>>>>>>>>>>>>>>>>>
# Auto-generated by agi-pack (version=0.1.19).
# Auto-generated by agi-pack (version=0.2.0).
FROM nvidia/cuda:11.8.0-base-ubuntu22.04 AS base-gpu

# Setup environment variables
Expand Down
2 changes: 1 addition & 1 deletion examples/generated/Dockerfile-builder
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# >>>>>>>>>>>>>>>>>>>>>>>>>>>
# Auto-generated by agi-pack (version=0.1.19).
# Auto-generated by agi-pack (version=0.2.0).
FROM debian:buster-slim AS agipack-builder

# Setup environment variables
Expand Down
4 changes: 2 additions & 2 deletions examples/generated/Dockerfile-multistage
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# >>>>>>>>>>>>>>>>>>>>>>>>>>>
# Auto-generated by agi-pack (version=0.1.19).
# Auto-generated by agi-pack (version=0.2.0).
FROM debian:buster-slim AS base-cpu

# Setup environment variables
Expand Down Expand Up @@ -80,7 +80,7 @@ RUN apt-get -y autoclean \
&& rm -rf /tmp/reqs \
&& echo "pip cleanup complete"
# >>>>>>>>>>>>>>>>>>>>>>>>>>>
# Auto-generated by agi-pack (version=0.1.19).
# Auto-generated by agi-pack (version=0.2.0).
FROM base-cpu AS dev-cpu

# Install additional system packages
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ classifiers = [
dynamic = ["version"]
dependencies = [
"jinja2",
"pydantic<2",
"pydantic>=2.5",
"pyyaml",
"typer[all]"
]
Expand Down

0 comments on commit aff1f67

Please sign in to comment.