Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port directed_test_schema.py to recent versions of Pydantic #2079

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions dv/uvm/core_ibex/scripts/directed_test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class Config: # noqa
# DConfig.VALIDATORS #
##################################

@pydantic.validator('ld_script', 'includes', pre=True)
@pydantic.field_validator('ld_script', 'includes', mode='before')
@classmethod
def _make_valid_paths(cls, v: Any) -> pathlib.Path:
return make_valid_pathlib_path(cls, v)

Expand Down Expand Up @@ -124,39 +125,41 @@ def yaml_file_must_exist(cls, v: pathlib.Path):
raise ValueError(f"Path object not found in filesystem : {v}")
return v

@pydantic.root_validator()
def test_config_must_exist(cls, values):
"""Check that if a test specifies a common config, it exists in the list available."""
configs = [c.config for c in values.get('configs')]
for test in values.get('tests'):
if test.config not in configs:
@pydantic.model_validator(mode='after')
def test_config_must_exist(self):
"""A test may only specify common configs in the available list."""
config_names = {c.config for c in self.configs}
for test in self.tests:
if test.config not in config_names:
raise ValueError(
f"Test '{test.test}' gave the config '{test.config}', but "
"this config does not exist in the file "
f"'{values.get('yaml')}'. Configs detected : {configs} \n")
return values
f"'{self.yaml}'. Configs detected : {self.configs} \n")
return self

@pydantic.root_validator()
def all_paths_must_exist(cls, values):
@pydantic.model_validator(mode='after')
def all_paths_must_exist(self):
"""Check that all fields specifying files exist on disk.

We need to check all fields recursively for pathlib.Path fields,
then ensure that those files exist, relative to the yaml file.
"""

def check_model_path_fields_exist(model):
for f in filter(lambda f: (f.type_ == pathlib.Path),
model.__fields__.values()):
p = validate_path_exists(getattr(model, f.name), values.get('yaml'))
setattr(model, f.name, p)
for k, f in model.__fields__.items():
if f.annotation != pathlib.Path:
continue

for c in values.get('configs'):
p = validate_path_exists(getattr(model, k), self.yaml)
setattr(model, k, p)

for c in self.configs:
check_model_path_fields_exist(c)

for t in values.get('tests'):
for t in self.tests:
check_model_path_fields_exist(t)

return values
return self


def import_model(directed_test_yaml: pathlib.Path) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion python-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pathlib3x # Backports some useful features
typing-utils # Ditto
typeguard ~= 2.13
portalocker
pydantic < 2.0
pydantic
svg.py

# Needed by dvsim.py (not actually used in Ibex)
Expand Down
Loading