Skip to content

Commit 588ecce

Browse files
authored
fix: delete inline table element does not leave trailing sep (#267)
1 parent 7134436 commit 588ecce

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
### Fixed
77

88
- Parse empty table name if it is quoted. ([#258](https://github.com/sdispater/tomlkit/issues/258))
9+
- Fix a bug that remove last element of an Inline Table leaves a comma. ([#259](https://github.com/sdispater/tomlkit/issues/259))
910
- Fix the `unwrap()` method for `Container` children values which sometimes returns an internal object if the table is an out-of-order table. ([#264](https://github.com/sdispater/tomlkit/issues/264))
1011

1112
## [0.11.6] - 2022-10-27

tests/test_items.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ def test_trim_comments_when_building_inline_table():
824824
assert table.as_string() == '{foo = "bar", baz = "foobaz"}'
825825

826826

827-
def test_deleting_inline_table_elemeent_does_not_leave_trailing_separator():
827+
def test_deleting_inline_table_element_does_not_leave_trailing_separator():
828828
table = api.inline_table()
829829
table["foo"] = "bar"
830830
table["baz"] = "boom"
@@ -845,6 +845,22 @@ def test_deleting_inline_table_elemeent_does_not_leave_trailing_separator():
845845
assert table.as_string() == '{baz = "boom"}'
846846

847847

848+
def test_deleting_inline_table_element_does_not_leave_trailing_separator2():
849+
doc = parse('a = {foo = "bar", baz = "boom"}')
850+
table = doc["a"]
851+
assert table.as_string() == '{foo = "bar", baz = "boom"}'
852+
853+
del table["baz"]
854+
assert table.as_string() == '{foo = "bar" }'
855+
856+
del table["foo"]
857+
assert table.as_string() == "{ }"
858+
859+
table["baz"] = "boom"
860+
861+
assert table.as_string() == '{ baz = "boom"}'
862+
863+
848864
def test_booleans_comparison():
849865
boolean = Bool(True, Trivia())
850866

tomlkit/items.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,14 @@ def append(self, key, _item):
17191719

17201720
def as_string(self) -> str:
17211721
buf = "{"
1722+
last_item_idx = next(
1723+
(
1724+
i
1725+
for i in range(len(self._value.body) - 1, -1, -1)
1726+
if self._value.body[i][0] is not None
1727+
),
1728+
None,
1729+
)
17221730
for i, (k, v) in enumerate(self._value.body):
17231731
if k is None:
17241732
if i == len(self._value.body) - 1:
@@ -1741,7 +1749,7 @@ def as_string(self) -> str:
17411749
f"{v_trivia_trail}"
17421750
)
17431751

1744-
if i != len(self._value.body) - 1:
1752+
if last_item_idx is not None and i < last_item_idx:
17451753
buf += ","
17461754
if self._new:
17471755
buf += " "

0 commit comments

Comments
 (0)