-
-
Notifications
You must be signed in to change notification settings - Fork 47
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
Omit None
values in TOML instead of throwing TypeError
#85
Comments
I'm stupid, there's an |
Good spot! At this moment you can use I think it’s time to add a new Let me reopen this issue to keep track of it for the next release. |
I have made some changes, which I wrote about above. Now using @dataclass
class InnerDataClassWithOptionalField(DataClassTOMLMixin):
x: Optional[int] = None
@dataclass
class DataClass(DataClassTOMLMixin):
x: Optional[InnerDataClassWithOptionalField] = None
y: Optional[int] = None
obj = DataClass()
assert obj.to_dict() == {"x": None, "y": None}
assert obj.to_toml() == ""
obj = DataClass(InnerDataClassWithOptionalField())
assert obj.to_dict() == {"x": {"x": None}, "y": None}
assert obj.to_toml() == "[x]\n"
assert DataClass.from_toml("[x]\n") == obj |
Starting from 3.2 None values are skipped for TOML. |
Is your feature request related to a problem? Please describe.
Example:
Gives the following exception:
Describe the solution you'd like
It seems that TOML plainly does not represent
None
values, preferring instead to omit such keys (see toml-lang/toml#30 ). That would be perfectly fine sinceNone
in Python is used to indicate missing value. Thus I think the default behavior for TOML should be to just not serialize the None value instead of giving thisTypeError
.Describe alternatives you've considered
Currently it seems one needs to use another format e.g. yaml or json if the dataclass has any possibly
None
values.The text was updated successfully, but these errors were encountered: