-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add function to construct TOML dictionary for dataclass instance #12
Comments
Now that |
Although, if it is a method, calling that method when Maybe the original specialization syntax wasn't so bad after all: if You could also do things like We'd have to check whether overloading a method (like Perhaps having two differently named methods is better than overloading. One would be a class method that parses from scratch and the other an instance method that parses with existing data as defaults. That would fit better with the convention in Python that you can call class methods on instances as well. |
In theory we could post-process the dictionary returned by When using a custom dictionary factory, the post-processing would see nested dataclasses multiple times: when the nested dataclass is processed itself and once for every parent level. We can't just skip recursion into nested dictionaries, as unlike dictionaries were created from dataclasses, dictionaries created from mapping types do need recursive post-processing. Therefore, if we'd use |
The
Binder.bind()
method creates a dataclass instance from the data in TOML dictionary. It would be useful to support the opposite conversion as well, where we accept a dataclass instance and return the corresponding TOML dictionary.We do support conversion from dataclass instance to textual TOML representation with
format_template()
, but in the case of parsing it turned out to be useful to support dictionary input in addition to textual input, so when generating TOML, it's probably also useful to support dictionary output in addition to textual output.This could either be a standalone function or a method on an instanced
Binder
. I think it would be more efficient to use an instancedBinder
in the implementation, both to avoid code duplication and to not do redundant checks on the data class definition. However, as we have a binder cache already, we could have a standalone function forward the request to an instanced binder, if that simplifies the interface.Note that
dataclasses.asdict()
offers similar functionality, but it does not handle some conversions liketimedelta
, modules and dashes in key names. Perhaps we can useasdict()
with a custom dictionary factory, but probably not, as there is no accompanying list factory.The text was updated successfully, but these errors were encountered: