tomlkit icon indicating copy to clipboard operation
tomlkit copied to clipboard

Missing keys in repro of tomlkit.container.OutOfOrderTableProxy

Open ewjoachim opened this issue 5 months ago • 3 comments

Hello, Here's a strange one.

From this toml:

a.b.c = "d"
a.b.e = "f"

When we access value["a"], it behaves correctly but the repr is missing the "c" key.

import tomlkit

print(tomlkit.__version__)
# 0.13.3

a = tomlkit.parse("""
a.b.c = "d"
a.b.e = "f"
""")
assert a == {"a": {"b": {"c": "d", "e": "f"}}}  # ok
value = a["a"]
expected = {"b": {"c": "d", "e": "f"}}
assert value == expected  # ok

print(value)
# {'b': {'e': 'f'}}
print(expected)
# {'b': {'c': 'd', 'e': 'f'}}

assert repr(value) == repr(expected)
# Traceback (most recent call last):
#   File "/Users/joachim/code/raincoat2/oops.py", line 19, in <module>
#     assert repr(value) == repr(expected)
#            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# AssertionError

As far as I can tell, this doesn't happen on a, only on a["a"]. I've tried to simplify the issue as much as possible.

ewjoachim avatar Aug 01 '25 17:08 ewjoachim

If it helps to understand the issue: it also occurs in conjunction with pydantic:

from __future__ import annotations

import pydantic
import tomlkit

print(tomlkit.__version__)

a = tomlkit.parse("""
a.b.c = "d"
a.b.e = "f"
""")
value = a["a"]


class B(pydantic.BaseModel):
    c: str
    e: str


class A(pydantic.BaseModel):
    b: B


expected = {"b": {"c": "d", "e": "f"}}
A.model_validate(expected)
assert value == expected
A.model_validate(value)
# pydantic_core._pydantic_core.ValidationError: 1 validation error for A
# b.c
#   Field required [type=missing, input_value={'e': 'f'}, input_type=Table]
#     For further information visit https://errors.pydantic.dev/2.11/v/missing

But in that case, it makes sense to use unwrap, and the problem disappears.

ewjoachim avatar Aug 01 '25 17:08 ewjoachim

I thought it was a perfect opportunity to do a bit of git bisect, but it came back with this commit of -1700+1700 lines, so not very helpful, I guess... Well, I tried :)

ewjoachim avatar Aug 01 '25 20:08 ewjoachim

Ok, it was a nice rabbit hole, but (and I say it with immense respect for maintainers) every ten minutes I spend trying to understand the hierarchy of object inheriting dict which behaves both like an object and like a native dict, I have to roll for sanity, and I'm running low. I'll leave it to the pros :) Good luck !

ewjoachim avatar Aug 01 '25 21:08 ewjoachim