-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add Version checks to all relevant models. #180
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
from typing import Any, List, Optional | ||
|
||
from pydantic import Field, validator | ||
from semantic_version import Version as SemVer | ||
|
||
|
||
def example(a: float, b: float = 1.0) -> float: | ||
"""[summary] | ||
|
@@ -69,3 +72,55 @@ def get_substring_between(source: str, start: str, end: str) -> Optional[str]: | |
return None | ||
|
||
return source[index_start:index_end] | ||
|
||
|
||
class HVersion(SemVer): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstrings for the purpose of these classes |
||
@classmethod | ||
def coerce(cls, *args, **kwargs): | ||
version = super().coerce(*args, **kwargs) | ||
return cls(**version.__dict__) | ||
|
||
|
||
class FMVersion(HVersion): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite like this FM-related class here, inside a "neutral" utils namespace. |
||
def __str__(self) -> str: | ||
"""Add zero to minor version if it is less than 10.""" | ||
version = f"{self.major}" | ||
if self.minor is not None: | ||
version = f"{version}.{self.minor:02d}" | ||
|
||
return version | ||
|
||
|
||
class DIMRVersion(HVersion): | ||
def __str__(self) -> str: | ||
"""Strip patch version.""" | ||
version = f"{self.major}" | ||
if self.minor is not None: | ||
version = f"{version}.{self.minor}" | ||
|
||
return version | ||
|
||
|
||
def get_version_validator(*field_name: str): | ||
"""Get a validator to check the Version number.""" | ||
|
||
def check_version(cls, v: Any, field: Field): | ||
"""Validate (semantic) version numbers.""" | ||
|
||
if isinstance(v, str): | ||
version = field.default.__class__.coerce(v) | ||
elif isinstance(v, (FMVersion, DIMRVersion)): | ||
version = v | ||
elif v is None: | ||
return field.default | ||
else: | ||
raise ValueError(f"Invalid version specified: {v}") | ||
|
||
if version < field.default or version >= field.default.next_major(): | ||
raise ValueError( | ||
f"Input with version {v} isn't a version support by HYDROLIB-core, which supports >={field.default}" | ||
) | ||
|
||
return version | ||
|
||
return validator(*field_name, allow_reuse=True, pre=True)(check_version) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the attempt, yet I find it reads quite confusing.