I have run multiple times into the lack of write functionality in tomllib
, and would be willing to help merging and maintaining this feature.
When adding tomllib
, an API to write TOML was not included, as explained by PEP 680. To consider adding this feature, the points raised by the PEP should be addressed, which I will do below.
Please let me know if you think my response to the points raised by the PEP is sufficient. If the general feedback is positive, I can go ahead and start drafting a new PEP.
Addressing the PEP 680 motivation for not including a writer:
The ability to write TOML is not needed for the use cases that motivate this PEP: core Python packaging tools, and projects that need to read TOML configuration files.
While the ability to write TOML is not often needed for the mentioned use-cases, there are actually situations where it would be useful (eg. Install a static installation description file as part of the Python installation · Issue #107956 · python/cpython · GitHub).
This functionality is also used by build backends, such as pdm-backend, where having it available in the standard library would make bootstrapping easier for downstream distributors, and key but not core packaging tools, such as flit and hatch.
Use cases that involve editing an existing TOML file (as opposed to writing a brand new one) are better served by a style preserving library. TOML is intended as a human-readable and -editable configuration format, so it’s important to preserve comments, formatting and other markup. This requires a parser whose output includes style-related metadata, making it impractical to output plain Python types like
str
anddict
. Furthermore, it substantially complicates the design of the API.
This is true, however, having this functionality without style preservation would already be a huge improvement.
All the projects mentioned in my reply above (pdm-backend, flit, and hatch), for example, depend on tomli-w
, which is not style-preserving.
Even without considering style preservation, there are too many degrees of freedom in how to design a write API. For example, what default style (indentation, vertical and horizontal spacing, quotes, etc) should the library use for the output, and how much control should users be given over it? How should the library handle input and output validation? Should it support serialization of custom types, and if so, how? While there are reasonable options for resolving these issues, the nature of the standard library is such that we only get “one chance to get it right”.
IMO if we were to add this feature, I’d like to go for a KISS approach, where we provide a simple implementation that can be extended, similarly to the json
module.
Currently, no CPython core developers have expressed willingness to maintain a write API, or sponsor a PEP that includes one. Since it is hard to change or remove something in the standard library, it is safer to err on the side of exclusion for now, and potentially revisit this later.
That changed now, so we are on the mentioned “revisiting this later” step