-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor indexer to not allow possibility of misinterpreting None
failing 40 test wip commit all unit tests passing changes found during manual testing of workflows fix remaining unit tests
- Loading branch information
Showing
40 changed files
with
582 additions
and
640 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,47 @@ | ||
from typing import Any, Optional | ||
|
||
from numpy import integer | ||
from pydantic import BaseModel, computed_field, field_serializer | ||
from pydantic import BaseModel, ConfigDict, field_validator | ||
from snapred.meta.Config import Config | ||
from snapred.meta.Enum import StrEnum | ||
|
||
VERSION_START = Config["version.start"] | ||
VERSION_NONE_NAME = Config["version.friendlyName.error"] | ||
VERSION_DEFAULT_NAME = Config["version.friendlyName.default"] | ||
|
||
# VERSION_DEFAULT is a SNAPRed-internal "magic" integer: | ||
# * it is implicitely set during `Config` initialization. | ||
VERSION_DEFAULT = Config["version.default"] | ||
|
||
class VersionState(StrEnum): | ||
DEFAULT = Config["version.friendlyName.default"] | ||
LATEST = "latest" | ||
NEXT = "next" | ||
|
||
|
||
Version = int | VersionState | ||
|
||
|
||
class VersionedObject(BaseModel): | ||
# Base class for all versioned DAO | ||
|
||
# In pydantic, a leading double underscore activates | ||
# the `__pydantic_private__` feature, which limits the visibility | ||
# of the attribute to the interior scope of its own class. | ||
__version: Optional[int] = None | ||
version: Version | ||
|
||
@classmethod | ||
def parseVersion(cls, version, *, exclude_none: bool = False, exclude_default: bool = False) -> int | None: | ||
v: int | None | ||
# handle two special cases | ||
if (not exclude_none) and (version is None or version == VERSION_NONE_NAME): | ||
v = None | ||
elif (not exclude_default) and (version == VERSION_DEFAULT_NAME or version == VERSION_DEFAULT): | ||
v = VERSION_DEFAULT | ||
# parse integers | ||
elif isinstance(version, int | integer): | ||
if int(version) >= VERSION_START: | ||
v = int(version) | ||
else: | ||
raise ValueError(f"Given version {version} is smaller than start version {VERSION_START}") | ||
# otherwise this is an error | ||
else: | ||
raise ValueError(f"Cannot initialize version as {version}") | ||
return v | ||
@field_validator("version", mode="before") | ||
def validate_version(cls, value: Version) -> Version: | ||
if value in VersionState.values(): | ||
return value | ||
|
||
@classmethod | ||
def writeVersion(cls, version) -> int | str: | ||
v: int | str | ||
if version is None: | ||
v = VERSION_NONE_NAME | ||
elif version == VERSION_DEFAULT: | ||
v = VERSION_DEFAULT_NAME | ||
elif isinstance(version, int | integer): | ||
v = int(version) | ||
else: | ||
raise ValueError("Version is not valid") | ||
return v | ||
if isinstance(value, str): | ||
raise ValueError(f"Version must be an int or {VersionState.values()}") | ||
|
||
def __init__(self, **kwargs): | ||
version = kwargs.pop("version", None) | ||
super().__init__(**kwargs) | ||
self.__version = self.parseVersion(version) | ||
if value is None: | ||
raise ValueError("Version must be specified") | ||
|
||
@field_serializer("version", check_fields=False, when_used="json") | ||
def write_user_defaults(self, value: Any): # noqa ARG002 | ||
return self.writeVersion(self.__version) | ||
if value < VERSION_START: | ||
raise ValueError(f"Version must be greater than {VERSION_START}") | ||
|
||
# NOTE some serialization still using the dict() method | ||
def dict(self, **kwargs): | ||
res = super().dict(**kwargs) | ||
res["version"] = self.writeVersion(res["version"]) | ||
return res | ||
return value | ||
|
||
@computed_field | ||
@property | ||
def version(self) -> int: | ||
return self.__version | ||
# NOTE: This approach was taken because 'field_serializer' was checking against the | ||
# INITIAL value of version for some reason. This is a workaround. | ||
# | ||
def model_dump_json(self, *args, **kwargs): # noqa ARG002 | ||
if self.version in VersionState.values(): | ||
raise ValueError(f"Version {self.version} must be flattened to an int before writing to JSON") | ||
return super().model_dump_json(*args, **kwargs) | ||
|
||
@version.setter | ||
def version(self, v): | ||
self.__version = self.parseVersion(v, exclude_none=True) | ||
model_config = ConfigDict(use_enum_values=True, validate_assignment=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.