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

Use in-built type for parameter version #583

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies = [
"opentelemetry-distro",
"opentelemetry-exporter-otlp",
"pydantic",
"pydantic-extra-types",
"pyepics",
"pyzmq",
"requests",
Expand Down
27 changes: 7 additions & 20 deletions src/mx_bluesky/hyperion/parameters/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
BaseModel,
ConfigDict,
Field,
field_serializer,
field_validator,
model_validator,
)
from pydantic_extra_types.semantic_version import SemanticVersion
from scanspec.core import AxesPoints
from semver import Version

Expand All @@ -29,15 +29,7 @@
T = TypeVar("T")


class ParameterVersion(Version):
@classmethod
def _parse(cls, version):
if isinstance(version, cls):
return version
return cls.parse(version)


PARAMETER_VERSION = ParameterVersion.parse("5.1.0")
PARAMETER_VERSION = Version.parse("5.1.0")


class RotationAxis(StrEnum):
Expand Down Expand Up @@ -108,21 +100,16 @@ def __hash__(self) -> int:
return self.json().__hash__()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My pylance tells me we should use model_dump_json() instead of .json


features: FeatureFlags = Field(default=FeatureFlags())
parameter_model_version: ParameterVersion

@field_serializer("parameter_model_version")
def serialize_parameter_version(self, version: ParameterVersion):
return str(version)
parameter_model_version: SemanticVersion

@field_validator("parameter_model_version", mode="before")
@field_validator("parameter_model_version")
@classmethod
def _validate_version(cls, version_str: str):
version = ParameterVersion.parse(version_str)
def _validate_version(cls, version: Version):
assert (
version >= ParameterVersion(major=PARAMETER_VERSION.major)
version >= Version(major=PARAMETER_VERSION.major)
), f"Parameter version too old! This version of hyperion uses {PARAMETER_VERSION}"
assert (
version <= ParameterVersion(major=PARAMETER_VERSION.major + 1)
version <= Version(major=PARAMETER_VERSION.major + 1)
), f"Parameter version too new! This version of hyperion uses {PARAMETER_VERSION}"
return version

Expand Down
Loading