-
Notifications
You must be signed in to change notification settings - Fork 283
added default values for field decoding (#788) #792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
1d53e13
66ca70c
0c262c2
3259610
a736a41
a126c77
ea4fb88
279ab93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import datetime | ||
| import uuid | ||
| from dataclasses import dataclass, make_dataclass | ||
| from dataclasses import dataclass, make_dataclass, field | ||
| from typing import Annotated, Any, Callable, Literal, NamedTuple | ||
|
|
||
| import numpy as np | ||
|
|
@@ -1468,3 +1468,39 @@ class Team: | |
|
|
||
| # Test Any annotation | ||
| validate_full_roundtrip(teams, dict[str, Team], (expected_dict_dict, Any)) | ||
|
|
||
|
|
||
| def test_auto_default_supported_and_unsupported() -> None: | ||
| from dataclasses import dataclass, field | ||
|
|
||
| @dataclass | ||
| class Base: | ||
| a: int | ||
| b: int | ||
|
|
||
| @dataclass | ||
| class ExtraFieldSupported: | ||
| a: int | ||
| b: int | ||
| c: list[int] = field(default_factory=list) | ||
|
||
|
|
||
| @dataclass | ||
| class ExtraFieldUnsupported: | ||
| a: int | ||
| b: int | ||
| c: int | ||
|
|
||
| engine_val = [1, 2] | ||
|
|
||
| # Should succeed: c is a list (LTable), auto-defaults to [] | ||
| validate_full_roundtrip( | ||
| Base(1, 2), Base, (ExtraFieldSupported(1, 2, []), ExtraFieldSupported) | ||
| ) | ||
|
|
||
| # Should fail: c is a non-nullable int, no default, not supported | ||
| with pytest.raises( | ||
| ValueError, | ||
| match=r"Field 'c' \(type <class 'int'>\) without default value is missing in input: ", | ||
| ): | ||
| decoder = build_engine_value_decoder(Base, ExtraFieldUnsupported) | ||
| decoder(engine_val) | ||
Uh oh!
There was an error while loading. Please reload this page.