-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor for pydantic v2 and better typing (#165)
* Refactor for pydantic v2 and better type hints Some more code modification to replace some deprecated methods in pydantic v2. I have also improved typing in the code. Some bugs were found along the way, including the `match` method of the SpinSystem class. * Bump package versions
- Loading branch information
1 parent
706db72
commit 1f55753
Showing
78 changed files
with
599 additions
and
681 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,3 +107,4 @@ Sandbox/ | |
|
||
# PDM | ||
.pdm-python | ||
.pdm-build |
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,19 +1,27 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Any | ||
from typing import Any | ||
|
||
from pydantic import BaseModel, ConfigDict, model_validator | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import MutableMapping | ||
|
||
def ensure_list(variable: Any | list[Any] | None) -> list[Any]: | ||
if isinstance(variable, list): | ||
return variable | ||
if variable is None: | ||
return [] | ||
return [variable] | ||
|
||
|
||
def to_lower(string: Any) -> Any: | ||
if isinstance(string, str): | ||
return string.lower() | ||
return string | ||
|
||
|
||
class BaseModelLowerCase(BaseModel): | ||
model_config = ConfigDict(str_to_lower=True) | ||
|
||
@model_validator(mode="before") | ||
@classmethod | ||
def to_lower_case( | ||
cls, values: MutableMapping[str, Any] | ||
) -> MutableMapping[str, Any]: | ||
return {k.lower(): v for k, v in values.items()} | ||
def key_to_lower(cls, model: dict[str, Any]) -> dict[str, Any]: | ||
return {to_lower(k): v for k, v in model.items()} |
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.