-
Notifications
You must be signed in to change notification settings - Fork 14
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
Adds minimal support for Array of Tables (AoT) #15
Conversation
Inline tables are a cool feature of the TOML, and very useful as elements of an array. However there are some circumstances that they are explicitly discouraged by spec itself. For example when dumping a list of dicts that in turn have lists inside of them, `tomli-w` will introduce line breaks inside of inline tables, which is not recommended by the spec This situation was first described in issue hukkin#12. Please notice this usage pattern is not that uncommon. We can find examples of this nesting in the wild, like the ones used in `mypy` configuration: https://mypy.readthedocs.io/en/stable/config_file.html#using-a-pyproject-toml-file
Codecov Report
@@ Coverage Diff @@
## master #15 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 2 2
Lines 92 105 +13
Branches 26 30 +4
=========================================
+ Hits 92 105 +13
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a bunch! Generally looking very good. I added a couple notes
Co-authored-by: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com>
There might be a chance any-not is slightly more costly than not-all (although if it exists, the performance difference should be negligible)
Thank you very much for the review @hukkin. I added the missing test and also fixed somethings regarding simply committing the suggestions via GitHub interface 😝 (I also reverted back to not-all instead of all-any, just because there might be a negligible performance penalty of flipping each Boolean but I admit that is very much a micro-optimisation that lacks benchmarking). |
tomli_w/_writer.py
Outdated
# In the following line we use `repr(obj)` as an approximation for the | ||
# TOML representation of an inline-table when `obj` is a dict, | ||
# (for the purposes of roughly estimating the line length) | ||
return len(repr(obj)) < LONG_LINE_HEURISTIC |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return len(repr(obj)) < LONG_LINE_HEURISTIC | |
return len(format_literal(obj)) < LONG_LINE_HEURISTIC |
Would this work? It's probably more expensive, but would probably allow us to be actually precise about the line length
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose it would (I haven't checked for infinite recursion though)
It is just important to have in mind that format_literal
will still not consider the indentation inside an array (so in the end the condition will be implicitly LONG_LINE_HEURISTIC + indentation_len
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added your suggestion, the tests work so I suspect the recursion is fine 😝
Please feel free to revert the last commit if you change your mind about the precision/cost tradeoff 😄.
# TOML representation of an inline-table when `obj` is a dict, | ||
# (for the purposes of roughly estimating the line length) | ||
return len(repr(obj)) < LONG_LINE_HEURISTIC | ||
max_len = LONG_LINE_HEURISTIC - (INDENT_LENGTH * nest_level) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I think a nest_level
argument (despite not necessary), makes more obvious and easier to understand.
Thanks a lot for this! Tell me if you need a release soon and I'll make one. Otherwise I may wait until Python 3.6 removal or another feature. |
Thanks @hukkin. I am waiting for this feature for my https://github.com/abravalheri/ini2toml project, but on the other hand I also have to wait for a couple of fixes in |
👍 the CI of this PR should push v0.4.0 to PyPI for you #19 Thanks once more! |
Inline tables are a cool feature of the TOML, and very useful as elements of an array.
However there are some circumstances that they are explicitly discouraged by spec itself.
For example when dumping a list of dicts that in turn have lists inside of them,
tomli-w
will introduce line breaks inside of inline tables, which is not recommended by the specThis situation was first described in issue #12.
Please notice this usage pattern is not that uncommon. We can find examples of this nesting in the wild, like the ones used in
mypy
configuration:https://mypy.readthedocs.io/en/stable/config_file.html#using-a-pyproject-toml-file