From 74437300a1983fc645de219fe051e638b3f6d055 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Mon, 7 Feb 2022 19:58:12 +0800 Subject: [PATCH 1/3] Make supertables if the only child id a table-like item --- tomlkit/items.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tomlkit/items.py b/tomlkit/items.py index c655784..abbb05f 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -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)): From c41d4f9d4096902df3398972cdef7e45889ad738 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Tue, 8 Feb 2022 19:11:19 +0800 Subject: [PATCH 2/3] Bump version --- CHANGELOG.md | 9 +++++++-- pyproject.toml | 2 +- tomlkit/__init__.py | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae74e33..e5808b6 100644 --- a/CHANGELOG.md +++ b/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 @@ -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 diff --git a/pyproject.toml b/pyproject.toml index a38f9b8..a937af7 100644 --- a/pyproject.toml +++ b/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 "] license = "MIT" diff --git a/tomlkit/__init__.py b/tomlkit/__init__.py index 2d15e33..84566d4 100644 --- a/tomlkit/__init__.py +++ b/tomlkit/__init__.py @@ -25,7 +25,7 @@ from .api import ws -__version__ = "0.9.1" +__version__ = "0.9.2" __all__ = [ "aot", "array", From bd1e498ead359c736c64ea8625c3ef6d8f87e461 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Tue, 8 Feb 2022 19:17:26 +0800 Subject: [PATCH 3/3] add test case to cover the change --- tests/test_api.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_api.py b/tests/test_api.py index f4ecf35..88b9772 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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"