|
17 | 17 | - [Write to file](#write-to-file) |
18 | 18 | - [FAQ](#faq) |
19 | 19 | - [Does Tomli-W sort the document?](#does-tomli-w-sort-the-document) |
20 | | - - [Does Tomli-W support writing documents with comments or custom whitespace?](#does-tomli-w-support-writing-documents-with-comments-or-custom-whitespace) |
| 20 | + - [Does Tomli-W support writing documents with comments?](#does-tomli-w-support-writing-documents-with-comments) |
| 21 | + - [Can I customize insignificant whitespace?](#can-i-customize-insignificant-whitespace) |
21 | 22 | - [Why does Tomli-W not write a multi-line string if the string value contains newlines?](#why-does-tomli-w-not-write-a-multi-line-string-if-the-string-value-contains-newlines) |
22 | 23 | - [Is Tomli-W output guaranteed to be valid TOML?](#is-tomli-w-output-guaranteed-to-be-valid-toml) |
23 | 24 |
|
@@ -73,10 +74,29 @@ with open("path_to_file/conf.toml", "wb") as f: |
73 | 74 | No, but it respects sort order of the input data, |
74 | 75 | so one could sort the content of the `dict` (recursively) before calling `tomli_w.dumps`. |
75 | 76 |
|
76 | | -### Does Tomli-W support writing documents with comments or custom whitespace?<a name="does-tomli-w-support-writing-documents-with-comments-or-custom-whitespace"></a> |
| 77 | +### Does Tomli-W support writing documents with comments?<a name="does-tomli-w-support-writing-documents-with-comments"></a> |
77 | 78 |
|
78 | 79 | No. |
79 | 80 |
|
| 81 | +### Can I customize insignificant whitespace?<a name="can-i-customize-insignificant-whitespace"></a> |
| 82 | + |
| 83 | +Indent width of array content can be configured via the `indent` keyword argument. |
| 84 | +`indent` takes a non-negative integer, defaulting to 4. |
| 85 | + |
| 86 | +```python |
| 87 | +import tomli_w |
| 88 | + |
| 89 | +doc = {"fruits": ["orange", "kiwi", "papaya"]} |
| 90 | +expected_toml = """\ |
| 91 | +fruits = [ |
| 92 | + "orange", |
| 93 | + "kiwi", |
| 94 | + "papaya", |
| 95 | +] |
| 96 | +""" |
| 97 | +assert tomli_w.dumps(doc, indent=1) == expected_toml |
| 98 | +``` |
| 99 | + |
80 | 100 | ### Why does Tomli-W not write a multi-line string if the string value contains newlines?<a name="why-does-tomli-w-not-write-a-multi-line-string-if-the-string-value-contains-newlines"></a> |
81 | 101 |
|
82 | 102 | This default was chosen to achieve lossless parse/write round-trips. |
|
117 | 137 | If there's a chance that your input data is bad and you need output validation, |
118 | 138 | parse the output string once with `tomli.loads`. |
119 | 139 | If the parse is successful (does not raise `tomli.TOMLDecodeError`) then the string is valid TOML. |
| 140 | + |
| 141 | +Examples of bad input data that can lead to writing invalid TOML without an error being raised include: |
| 142 | + |
| 143 | +- A mapping where keys behave very much like strings, but aren't. E.g. a tuple of strings of length 1. |
| 144 | +- A mapping where a value is a subclass of a supported type, but which overrides the `__str__` method. |
| 145 | + |
| 146 | +Given proper input (a mapping consisting of non-subclassed [types returned by Tomli](https://github.com/hukkin/tomli?tab=readme-ov-file#how-do-toml-types-map-into-python-types)) |
| 147 | +the output should be valid TOML. |
0 commit comments