-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow vanilla Pydantic serialisation/deserialisation of discriminated…
… unions
- Loading branch information
1 parent
f49320c
commit 5d9ff6b
Showing
7 changed files
with
429 additions
and
2,201 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import pytest | ||
from pydantic import BaseModel, TypeAdapter | ||
|
||
from scanspec.specs import Line, Spec | ||
|
||
|
||
class Foo(BaseModel): | ||
spec: Spec | ||
|
||
|
||
simple_foo = Foo(spec=Line("x", 1, 2, 5)) | ||
nested_foo = Foo(spec=Line("x", 1, 2, 5) * Line("y", 1, 2, 5)) | ||
|
||
|
||
@pytest.mark.parametrize("model", [simple_foo, nested_foo]) | ||
def test_model_validation(model: Foo): | ||
# To/from Python dict | ||
as_dict = model.model_dump() | ||
deserialized = Foo.model_validate(as_dict) | ||
assert deserialized == model | ||
|
||
# To/from Json dict | ||
as_json = model.model_dump_json() | ||
deserialized = Foo.model_validate_json(as_json) | ||
assert deserialized == model | ||
|
||
|
||
@pytest.mark.parametrize("model", [simple_foo, nested_foo]) | ||
def test_type_adapter(model: Foo): | ||
type_adapter = TypeAdapter(Foo) | ||
|
||
# To/from Python dict | ||
as_dict = model.model_dump() | ||
deserialized = type_adapter.validate_python(as_dict) | ||
assert deserialized == model | ||
|
||
# To/from Json dict | ||
as_json = model.model_dump_json() | ||
deserialized = type_adapter.validate_json(as_json) | ||
assert deserialized == model |