Skip to content

Commit

Permalink
Merge pull request #175 from sdispater/feature/default-supertable
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Feb 8, 2022
2 parents c75413e + bd1e498 commit 1c22797
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
@@ -1,6 +1,10 @@
# Change Log

## [Unreleased]
## [0.9.2] - 2022-02-08

### Changed

- When a table's only child is a table or array of table, it is created as a super table. ([#175](https://github.com/sdispater/tomlkit/issues/175))

## [0.9.1] - 2022-02-07

Expand Down Expand Up @@ -241,7 +245,8 @@
- Fixed handling of super tables with different sections.
- Fixed raw strings escaping.

[unreleased]: https://github.com/sdispater/tomlkit/compare/0.9.1...master
[unreleased]: https://github.com/sdispater/tomlkit/compare/0.9.2...master
[0.9.2]: https://github.com/sdispater/tomlkit/releases/tag/0.9.2
[0.9.1]: https://github.com/sdispater/tomlkit/releases/tag/0.9.1
[0.9.0]: https://github.com/sdispater/tomlkit/releases/tag/0.9.0
[0.8.0]: https://github.com/sdispater/tomlkit/releases/tag/0.8.0
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "tomlkit"
version = "0.9.1"
version = "0.9.2"
description = "Style preserving TOML library"
authors = ["Sébastien Eustace <sebastien@eustace.io>"]
license = "MIT"
Expand Down
10 changes: 10 additions & 0 deletions tests/test_api.py
Expand Up @@ -382,3 +382,13 @@ def test_value_rejects_values_with_appendage(raw):
"""Values that appear valid at the beginning but leave chars unparsed are rejected."""
with pytest.raises(tomlkit.exceptions.ParseError):
tomlkit.value(raw)


def test_create_super_table_with_table():
data = {"foo": {"bar": {"a": 1}}}
assert dumps(data) == "[foo.bar]\na = 1\n"


def test_create_super_table_with_aot():
data = {"foo": {"bar": [{"a": 1}]}}
assert dumps(data) == "[[foo.bar]]\na = 1\n"
2 changes: 1 addition & 1 deletion tomlkit/__init__.py
Expand Up @@ -25,7 +25,7 @@
from .api import ws


__version__ = "0.9.1"
__version__ = "0.9.2"
__all__ = [
"aot",
"array",
Expand Down
8 changes: 8 additions & 0 deletions tomlkit/items.py
Expand Up @@ -90,6 +90,14 @@ def item(
key=lambda i: (isinstance(i[1], dict), i[0] if _sort_keys else 1),
):
val[k] = item(v, _parent=val, _sort_keys=_sort_keys)
only_child = len(value) == 1 and val[next(iter(value))]
if (
table_constructor is Table
and only_child
and isinstance(only_child, (AoT, Table))
):
# The table becomes super table if the only child is a table or AoT.
val._is_super_table = True

return val
elif isinstance(value, (list, tuple)):
Expand Down

0 comments on commit 1c22797

Please sign in to comment.