-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `skip_none` attribute is an optional feature originally implemented to prevent null values from appearing in TOML serialized outputs. When set to True, any field in the class with a None value is excluded from the serialized output, ensuring that TOML files (or other formats) remain clean and free from null entries. Closes #421
- Loading branch information
Showing
9 changed files
with
121 additions
and
34 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
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
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,56 @@ | ||
from serde import serde | ||
from serde.toml import to_toml, from_toml | ||
from typing import Optional | ||
import pytest | ||
|
||
|
||
def toml_basics() -> None: | ||
@serde | ||
class Foo: | ||
v: Optional[int] | ||
|
||
f = Foo(10) | ||
assert "v = 10\n" == to_toml(f) | ||
assert f == from_toml(Foo, "v = 10\n") | ||
|
||
@serde | ||
class Bar: | ||
v: set[int] | ||
|
||
b = Bar({1, 2, 3}) | ||
to_toml(b) | ||
|
||
|
||
def test_skip_none() -> None: | ||
@serde | ||
class Foo: | ||
a: int | ||
b: Optional[int] | ||
|
||
f = Foo(10, 100) | ||
assert ( | ||
to_toml(f) | ||
== """\ | ||
a = 10 | ||
b = 100 | ||
""" | ||
) | ||
|
||
f = Foo(10, None) | ||
assert ( | ||
to_toml(f) | ||
== """\ | ||
a = 10 | ||
""" | ||
) | ||
|
||
|
||
def test_skip_none_container_not_supported_yet() -> None: | ||
@serde | ||
class Foo: | ||
a: int | ||
b: list[Optional[int]] | ||
|
||
f = Foo(10, [100, None]) | ||
with pytest.raises(TypeError): | ||
to_toml(f) |