Repository: sdispater/tomlkit
Pull request #34 (2018-11-28 04:57:58)

Pick the max when there are many indices for a key.
The test case demonstrates how it fails when appending a table.

codecov-io: # [Codecov](https://codecov.io/gh/sdispater/tomlkit/pull/34?src=pr&el=h1) Report
> Merging [#34](https://codecov.io/gh/sdispater/tomlkit/pull/34?src=pr&el=desc) into [master](https://codecov.io/gh/sdispater/tomlkit/commit/b10d0ccfc29ebf4a8e1340bd1ac546f1699ccdd7?src=pr&el=desc) will **increase** coverage by `0.1%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/sdispater/tomlkit/pull/34/graphs/tree.svg?width=650&token=ZE9lS2yLXi&height=150&src=pr)](https://codecov.io/gh/sdispater/tomlkit/pull/34?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master      #34     +/-   ##
=========================================
+ Coverage   91.95%   92.05%   +0.1%     
=========================================
  Files          10       10             
  Lines        2038     2040      +2     
=========================================
+ Hits         1874     1878      +4     
+ Misses        164      162      -2
```


| [Impacted Files](https://codecov.io/gh/sdispater/tomlkit/pull/34?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [tomlkit/container.py](https://codecov.io/gh/sdispater/tomlkit/pull/34/diff?src=pr&el=tree#diff-dG9tbGtpdC9jb250YWluZXIucHk=) | `87.5% <100%> (+0.06%)` | :arrow_up: |
| [tomlkit/parser.py](https://codecov.io/gh/sdispater/tomlkit/pull/34/diff?src=pr&el=tree#diff-dG9tbGtpdC9wYXJzZXIucHk=) | `96.26% <0%> (+0.31%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/sdispater/tomlkit/pull/34?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/sdispater/tomlkit/pull/34?src=pr&el=footer). Last update [b10d0cc...64c90dd](https://codecov.io/gh/sdispater/tomlkit/pull/34?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/tomlkit/container.py b/tomlkit/container.py
index e5843de..e1ff5fd 100644
--- a/tomlkit/container.py
+++ b/tomlkit/container.py
@@ -247,6 +247,9 @@ class Container(dict):
         item = _item(item)
 
         idx = self._map[key]
+        # Insert after the max index if there are many.
+        if isinstance(idx, tuple):
+            idx = max(idx)
         current_item = self._body[idx][1]
         if "\n" not in current_item.trivia.trail:
             current_item.trivia.trail += "\n"


Test patch:
diff --git a/tests/test_build.py b/tests/test_build.py
index f571607..078eba9 100644
--- a/tests/test_build.py
+++ b/tests/test_build.py
@@ -113,3 +113,18 @@ def test_add_remove():
     doc.remove("foo")
 
     assert doc.as_string() == ""
+
+
+def test_append_table_after_multiple_indices():
+    content = """
+    [packages]
+    foo = "*"
+
+    [settings]
+    enable = false
+
+    [packages.bar]
+    version = "*"
+    """
+    doc = parse(content)
+    doc.append("foobar", {"name": "John"})