Skip to content

Commit cd3dc8e

Browse files
Joshua BambrickThe etils Authors
authored andcommitted
Wrap pydantic schema in no_info_after_validator_function
PiperOrigin-RevId: 642561049
1 parent f82afde commit cd3dc8e

5 files changed

Lines changed: 43 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Changelog follow https://keepachangelog.com/ format.
88

99
## [Unreleased]
1010

11+
## [1.9.2] - 2024-06-12
12+
13+
* `epath`:
14+
* Support pydantic serialization of epath.Path
15+
1116
## [1.9.1] - 2024-06-04
1217

1318
* `epath`:

etils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
# A new PyPI release will be pushed everytime `__version__` is increased
1818
# When changing this, also update the CHANGELOG.md
19-
__version__ = '1.9.1'
19+
__version__ = '1.9.2'
2020

2121
# Do NOT add anything to this file. This is left empty on purpose.
2222
# Users should import subprojects directly.

etils/epath/abstract_path.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,10 @@ def __get_pydantic_core_schema__(cls, source_type, handler):
242242
pydantic_core.CoreSchema
243243
244244
"""
245+
from pydantic_core import core_schema # pylint: disable=g-import-not-at-top # pytype: disable=import-error
246+
245247
del source_type # Unused in this implementation.
246-
return handler(pathlib.PurePosixPath)
248+
249+
return core_schema.no_info_after_validator_function(
250+
cls, handler(pathlib.PurePosixPath)
251+
)

etils/epath/abstract_path_test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2024 The etils Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Tests pydantic serialization of etils.epath.Path."""
16+
17+
from etils import epath
18+
import pydantic
19+
20+
21+
def test_pydantic_serialize():
22+
23+
class _Model(pydantic.BaseModel):
24+
path_field: epath.Path
25+
26+
model = _Model(path_field=epath.Path("/example/path"))
27+
serialized = model.model_dump_json()
28+
deserialized = _Model.model_validate_json(serialized)
29+
assert isinstance(deserialized.path_field, epath.Path)
30+
assert model == deserialized

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ dev = [
134134
"torch",
135135
"optree", # For `etree.optree`
136136
"dataclass_array", # To test lazy_imports
137+
"pydantic", # To test pydantic serialization of epath.Path
137138
]
138139
docs = [
139140
"sphinx-apitree[ext]",

0 commit comments

Comments
 (0)