Skip to content

Commit b3198dd

Browse files
committed
update to use pydantic v2
1 parent d9784c3 commit b3198dd

4 files changed

Lines changed: 22 additions & 22 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ classifiers = [
2323

2424
dependencies = [
2525
"build~=0.9",
26-
"pydantic~=1.10",
26+
"pydantic~=2.1",
2727
"toml~=0.10",
2828
"typer[all]~=0.7",
2929
"wheel~=0.38"

src/dac/_input/config.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import inspect
33
import sys
44
from pathlib import Path
5-
from typing import Dict, Optional
5+
from typing import Optional
66

77
import pandera as pa
8-
from pydantic import BaseModel, root_validator, validator
8+
from pydantic import BaseModel, field_validator, model_validator
99

1010
from dac._file_helper import temporarily_copied_file
1111
from dac._input.pyproject import PyProjectConfig
@@ -18,13 +18,15 @@ class PackConfig(BaseModel):
1818
wheel_dir: Path
1919
pyproject: PyProjectConfig
2020

21-
@validator("data_path", "load_path", "schema_path", "wheel_dir")
21+
@field_validator("data_path", "load_path", "schema_path", "wheel_dir")
22+
@classmethod
2223
def path_exists(cls, path: Path) -> Path: # pylint: disable=no-self-argument,no-self-use
2324
if path is not None and not path.exists():
2425
raise ValueError((f"Path {path.as_posix()} is not valid"))
2526
return path
2627

27-
@validator("load_path")
28+
@field_validator("load_path")
29+
@classmethod
2830
def load_contains_expected_function(cls, path: Path) -> Path: # pylint: disable=no-self-argument,no-self-use
2931
try:
3032
sys.path.append(path.parent.as_posix())
@@ -46,7 +48,8 @@ def load_contains_expected_function(cls, path: Path) -> Path: # pylint: disable
4648
raise ValueError((f"{path.as_posix()} does not contain the required `def load()`")) from e
4749
return path
4850

49-
@validator("schema_path")
51+
@field_validator("schema_path")
52+
@classmethod
5053
def schema_contains_expected_class(cls, path: Path) -> Path: # pylint: disable=no-self-argument,no-self-use
5154
try:
5255
sys.path.append(path.parent.as_posix())
@@ -67,32 +70,28 @@ def schema_contains_expected_class(cls, path: Path) -> Path: # pylint: disable=
6770
raise ValueError((f"{path.as_posix()} does not contain the required `class Schema(pa.SchemaModel)`")) from e
6871
return path
6972

70-
@root_validator
71-
def schema_match_data( # pylint: disable=no-self-argument,no-self-use
72-
cls, values: Dict[str, Path]
73-
) -> Dict[str, Path]:
73+
@model_validator(mode="after")
74+
def schema_match_data(self) -> "PackConfig":
7475
try:
75-
sys.path.append(values["load_path"].parent.as_posix())
76-
load_module = importlib.import_module(name=values["load_path"].stem)
76+
sys.path.append(self.load_path.parent.as_posix())
77+
load_module = importlib.import_module(name=self.load_path.stem)
7778
except Exception as e:
7879
raise ValueError(
7980
"Validation of the schema against the data has failed because the load module could not be imported"
8081
) from e
8182

8283
try:
83-
if values.get("data_path", None) is not None:
84-
with temporarily_copied_file(
85-
src=values["data_path"], dst=values["load_path"].parent / values["data_path"].name
86-
):
84+
if self.data_path is not None:
85+
with temporarily_copied_file(src=self.data_path, dst=self.load_path.parent / self.data_path.name):
8786
data = load_module.load()
8887
else:
8988
data = load_module.load()
9089
except Exception as e:
9190
raise ValueError("`load()` failed due to the following error:" "\n" f"{e}") from e
9291

9392
try:
94-
sys.path.append(values["schema_path"].parent.as_posix())
95-
schema_module = importlib.import_module(name=values["schema_path"].stem)
93+
sys.path.append(self.schema_path.parent.as_posix())
94+
schema_module = importlib.import_module(name=self.schema_path.stem)
9695
except Exception as e:
9796
raise ValueError(
9897
"Validation of the schema against the data has failed because the schema module could not be imported"
@@ -107,4 +106,4 @@ def schema_match_data( # pylint: disable=no-self-argument,no-self-use
107106
"Validation of the schema against the data has failed for unexpected reasons:" "\n" f"{e}"
108107
) from e
109108

110-
return values
109+
return self

src/dac/_input/pyproject.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
from typing import List
22

33
import toml # type: ignore
4-
from pydantic import BaseModel, validator
4+
from pydantic import BaseModel, field_validator
55

66

77
class PyProjectConfig(BaseModel):
88
project_name: str
99
project_version: str
1010
project_dependencies: str
1111

12-
@validator("project_name")
12+
@field_validator("project_name")
13+
@classmethod
1314
def valid_project_name(cls, name: str) -> str: # pylint: disable=no-self-argument,no-self-use
1415
if not name.isidentifier() or "\xb7" in name:
1516
raise ValueError(f"Invalid project name: {name} (hint: only '_' are allowed, no '-')")

test/unit_test/_cli/pack_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_if_load_returns_wrong_type_then_error_contains_meaningful_info():
6363
result = invoke_dac_pack(load=get_path_to_return_wrong_type_load().as_posix())
6464
assert result.exit_code != 0
6565
error_message = str(result.exception)
66-
assert "Backend not found" in error_message
66+
assert "expected pd.DataFrame" in error_message
6767

6868

6969
def test_if_load_miss_credentials_then_error_contains_meaningful_info():

0 commit comments

Comments
 (0)