Repository: sdispater/tomlkit
Pull request #239 (2022-09-17 05:45:16)

Add Python 3.11 release candidate 2 to the testing
Python 3.11 will be on average 22% faster than Python 3.10 so let’s give it a spin... https://www.python.org/download/pre-releases

Related to #237


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/.github/workflows/tests.yml b/.github/workflows/tests.yml
index cb8c77c..d84d1b9 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -14,17 +14,20 @@ jobs:
     runs-on: ubuntu-latest
 
     steps:
-      - uses: actions/checkout@v2
-      - uses: actions/setup-python@v2
+      - uses: actions/checkout@v3
+      - uses: actions/setup-python@v4
+        with:
+          python-version: 3.x
       - uses: pre-commit/action@v2.0.3
   Tests:
     needs: Linting
     name: ${{ matrix.os }} / ${{ matrix.python-version }}
     runs-on: ${{ matrix.os }}-latest
     strategy:
+      fail-fast: false
       matrix:
         os: [Ubuntu, MacOS, Windows]
-        python-version: [3.6, 3.7, 3.8, 3.9, "3.10"]
+        python-version: [3.6, 3.7, 3.8, 3.9, "3.10", "3.11.0-rc - 3.11"]
 
     steps:
       - uses: actions/checkout@v3
@@ -44,8 +47,11 @@ jobs:
 
       - name: Install Poetry
         shell: bash
+        if: !startsWith(matrix.python-version, '3.11')
         run: |
           curl -fsSL https://install.python-poetry.org | python - -y --version 1.1.15
+      - if: startsWith(matrix.python-version, '3.11')
+        run: curl -fsSL https://install.python-poetry.org | python - -y --version 1.2.1
       - name: Update PATH
         if: ${{ matrix.os != 'Windows' }}
         run: echo "$HOME/.local/bin" >> $GITHUB_PATH
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)