Repository: sdispater/tomlkit

Issue:
Table.update(TOMLDocument) is missing a nesting level
Trying to update a Table `entry` from a TOMLDocument should insert the top-level entries of the document into the table `entry`, however it inserts them at the top level. Test case:

```python
import tomlkit
import tomli


def test_parsed_subentry():
    example = "[entry]\nfoo = 'bar'"
    subentry = "bar = 23\n\n[subentry]\n'subkey' = 'subvalue'"

    doc_tomlkit = tomlkit.loads(example)
    doc_tomli = tomlkit.loads(example)

    doc_tomlkit["entry"].update(tomlkit.loads(subentry))
    doc_tomli["entry"].update(tomli.loads(subentry))

    doc_tomlkit_output = tomlkit.dumps(doc_tomlkit)
    doc_tomli_output = tomlkit.dumps(doc_tomli)

    print("## Inserted tomli-parsed subentry ##", doc_tomli_output, sep="\n")
    print("## Inserted tomlkit-parsed subentry ##", doc_tomlkit_output, sep="\n")

    assert tomli.loads(doc_tomlkit_output) == tomli.loads(doc_tomli_output)
```

Output: 
```toml
## Inserted tomli-parsed subentry ##
[entry]
foo = 'bar'
bar = 23

[entry.subentry]
subkey = "subvalue"

## Inserted tomlkit-parsed subentry ##
[entry]
foo = 'bar'
bar = 23

[subentry]
'subkey' = 'subvalue'
```

Discussion:
Secrus: If you are removing `py.typed` file, you might want to remove it from the `tool.poetry.include` section in `pyproject.toml`. 

Also, I don't think removing the marker file is a good choice. It will break people's `mypy` checks. I would rather encourage working on proper typing for `tomlkit` rather than going back to not-typed way.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6eccccb..c0563e6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
 
 ## [Unreleased]
 
+### Fixed
+
+- Clear the existing table header when it is adding to another table. ([#230](https://github.com/sdispater/tomlkit/issues/230))
+
 ## [0.11.4] - 2022-08-12
 
 ### Fixed
diff --git a/tomlkit/container.py b/tomlkit/container.py
index c3b85ad..4b40a13 100644
--- a/tomlkit/container.py
+++ b/tomlkit/container.py
@@ -194,7 +194,7 @@ class Container(_CustomDict):
         prev = self._previous_item()
         prev_ws = isinstance(prev, Whitespace) or ends_with_whitespace(prev)
         if isinstance(item, Table):
-            if item.name != key.key:
+            if not self._parsed:
                 item.invalidate_display_name()
             if self._body and not (self._parsed or item.trivia.indent or prev_ws):
                 item.trivia.indent = "\n"
diff --git a/tomlkit/items.py b/tomlkit/items.py
index 695d7f7..59addb9 100644
--- a/tomlkit/items.py
+++ b/tomlkit/items.py
@@ -205,7 +205,7 @@ def item(
                     v.items(),
                     key=lambda i: (isinstance(i[1], dict), i[0] if _sort_keys else 1),
                 ):
-                    i = item(_v, _parent=a, _sort_keys=_sort_keys)
+                    i = item(_v, _parent=table, _sort_keys=_sort_keys)
                     if isinstance(table, InlineTable):
                         i.trivia.trail = ""
 


Test patch:
diff --git a/tests/test_toml_document.py b/tests/test_toml_document.py
index f253706..b5a6f62 100644
--- a/tests/test_toml_document.py
+++ b/tests/test_toml_document.py
@@ -1001,3 +1001,32 @@ def test_remove_item_from_super_table():
 
     """
     assert doc.as_string() == dedent(expected)
+
+
+def test_nested_table_update_display_name():
+    content = """\
+    [parent]
+
+    [parent.foo]
+    x = 1
+    """
+
+    doc = parse(dedent(content))
+    sub = """\
+    [foo]
+    y = 2
+
+    [bar]
+    z = 3
+    """
+    doc["parent"].update(parse(dedent(sub)))
+    expected = """\
+    [parent]
+
+    [parent.foo]
+    y = 2
+
+    [parent.bar]
+    z = 3
+    """
+    assert doc.as_string() == dedent(expected)