Skip to content

Commit 8f8e92a

Browse files
committed
Updated codebase to pydantic>=2.0
Fixes #63 Signed-off-by: Pedro Algarvio <[email protected]>
1 parent 0b3b1a5 commit 8f8e92a

File tree

24 files changed

+136
-152
lines changed

24 files changed

+136
-152
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ repos:
112112
additional_dependencies:
113113
- types-attrs
114114
- types-setuptools
115-
- pydantic>=1.8.2,<2.0
115+
- pydantic>=2.0,<3.0
116116
- types-aiofiles
117117
- aiostream
118118
- typing-extensions; python_version < "3.9"

changelog/63.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Updated codebase to `pydantic>=2.0,<3.0`

examples/tests/pipelines/test_mnist_network.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def test_pipeline(analytics_events_dump_directory: pathlib.Path):
6969
pytest.fail(f"Failed to find dumped events in {analytics_events_dump_directory}")
7070

7171
contents = [
72-
CollectedEvent.parse_obj(json.loads(i)) for i in dumpfile.read_text().strip().split("\n")
72+
CollectedEvent.model_validate(json.loads(i))
73+
for i in dumpfile.read_text().strip().split("\n")
7374
]
7475
for event in contents:
7576
assert "evaluation" in event.data

examples/tests/pipelines/test_mnist_notebook.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ def test_pipeline(analytics_events_dump_directory: pathlib.Path):
8989
pytest.fail(f"Failed to find dumped events in {analytics_events_dump_directory}")
9090

9191
contents = [
92-
CollectedEvent.parse_obj(json.loads(i)) for i in dumpfile.read_text().strip().split("\n")
92+
CollectedEvent.model_validate(json.loads(i))
93+
for i in dumpfile.read_text().strip().split("\n")
9394
]
9495
for event in contents:
9596
assert "evaluation" in event.data

requirements/base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ backoff
55
drain3
66
importlib-metadata>=3.4.0,<5.0.0; python_version < "3.8"
77
psutil
8-
pydantic>=1.8.2,<2.0
8+
pydantic>=2.0,<3.0
99
salt>=3005
1010
typing-extensions; python_version < "3.9.2"

src/saf/collect/beacons.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from typing import TypeVar
2020
from typing import Union
2121

22-
from pydantic import validator
22+
from pydantic import field_validator
2323

2424
from saf.models import CollectConfigBase
2525
from saf.models import CollectedEvent
@@ -60,7 +60,7 @@ def _convert_stamp(stamp: str) -> datetime:
6060
_stamp = datetime.strptime(stamp, "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=timezone.utc)
6161
return _stamp
6262

63-
@validator("stamp")
63+
@field_validator("stamp")
6464
@classmethod
6565
def _validate_stamp(cls: Type[BCE], value: Union[str, datetime]) -> datetime:
6666
if isinstance(value, datetime):

src/saf/collect/file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from saf.models import CollectedEvent
2323
from saf.models import PipelineRunContext
2424

25-
if sys.version_info < (3, 9, 2):
25+
if sys.version_info < (3, 12):
2626
from typing_extensions import TypedDict
2727
else:
2828
from typing import TypedDict # type: ignore[attr-defined,no-redef]

src/saf/forward/disk.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ async def forward(
5656
dest = config.path / config.filename
5757
dest.touch()
5858
async with aiofiles.open(dest, "a", encoding="utf-8") as wfh:
59-
wrote = await wfh.write(f"{event.json(indent=indent)}\n")
59+
wrote = await wfh.write(f"{event.model_dump_json(indent=indent)}\n")
6060
else:
6161
file_count = len(list(config.path.iterdir()))
6262
dest = config.path / f"event-dump-{file_count + 1}.json"
6363
async with aiofiles.open(dest, "w", encoding="utf-8") as wfh:
64-
wrote = await wfh.write(event.json(indent=indent))
64+
wrote = await wfh.write(event.model_dump_json(indent=indent))
6565
log.debug("Wrote %s bytes to %s", wrote, dest)

src/saf/forward/test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from typing import Optional
1919
from typing import Type
2020

21-
from pydantic import root_validator
21+
from pydantic import model_validator
2222

2323
from saf.models import CollectedEvent
2424
from saf.models import ForwardConfigBase
@@ -38,7 +38,7 @@ class TestForwardConfig(ForwardConfigBase):
3838
dump_event: bool = False
3939
add_event_to_shared_cache: bool = False
4040

41-
@root_validator
41+
@model_validator(mode="before")
4242
@classmethod
4343
def _check_mutually_exclusive_parameters(
4444
cls: Type[TestForwardConfig], values: dict[str, Any]
@@ -81,7 +81,7 @@ async def forward(
8181
elif config.path:
8282
if config.message:
8383
if config.dump_event:
84-
dump_text = json.dumps({config.message: event.dict()})
84+
dump_text = json.dumps({config.message: event.model_dump()})
8585
else:
8686
dump_text = config.message
8787
elif config.dump_event:

0 commit comments

Comments
 (0)