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

Improve model validation code #200

Merged
merged 2 commits into from
Nov 4, 2024
Merged
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
16 changes: 8 additions & 8 deletions src/pyobo/struct/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ def validate_identifier(cls, values): # noqa
prefix, identifier = values.get("prefix"), values.get("identifier")
if not prefix or not identifier:
return values
norm_prefix = bioregistry.normalize_prefix(prefix)
if norm_prefix is None:
resource = bioregistry.get_resource(prefix)
if resource is None:
raise ExpansionError(f"Unknown prefix: {prefix}")
values["prefix"] = norm_prefix
values["identifier"] = bioregistry.standardize_identifier(norm_prefix, identifier).strip()
# if not bioregistry.is_valid_identifier(norm_prefix, values["identifier"]):
# raise ValueError(f"non-standard identifier: {norm_prefix}:{norm_identifier}")
values["prefix"] = resource.prefix
values["identifier"] = resource.standardize_identifier(identifier)
# if not resource.is_valid_identifier(values["identifier"]):
# raise ValueError(f"non-standard identifier: {resource.prefix}:{values['identifier']}")
return values

@classmethod
Expand All @@ -60,7 +60,7 @@ def auto(cls, prefix: str, identifier: str) -> "Reference":
from ..api import get_name

name = get_name(prefix, identifier)
return cls(prefix=prefix, identifier=identifier, name=name)
return cls.model_validate({"prefix": prefix, "identifier": identifier, "name": name})

@property
def bioregistry_link(self) -> str:
Expand Down Expand Up @@ -116,7 +116,7 @@ def _materialize(
return None
if name is None and auto:
return cls.auto(prefix=prefix, identifier=identifier)
return cls(prefix=prefix, identifier=identifier, name=name)
return cls.model_validate({"prefix": prefix, "identifier": identifier, "name": name})

@property
def _escaped_identifier(self):
Expand Down
Loading