22import inspect
33import sys
44from pathlib import Path
5- from typing import Dict , Optional
5+ from typing import Optional
66
77import pandera as pa
8- from pydantic import BaseModel , root_validator , validator
8+ from pydantic import BaseModel , field_validator , model_validator
99
1010from dac ._file_helper import temporarily_copied_file
1111from 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
0 commit comments