Repository navigation
Copy trivia in _new so arithmetic results cannot mutate the original item - #683
Conversation
Integer/Float/String/DateTime/Date/Time _new passed the original item's Trivia object to the result, so editing the result's comment or indentation rewrote the line still stored in the document. Table and InlineTable __copy__ already copy trivia; do the same on the six arithmetic paths.
for more information, see https://pre-commit.ci
pre-commit's ruff hook flags list(doc)[0] as SIM118. The test only wants the first key, so next(iter(doc)) is the same without building a list.
|
The The |
|
The only red job is |
Summary
The
_newconstructors ofInteger,Float,String,DateTime,DateandTimepass the original item'sTriviaobject to the result, so the result aliases the original's comment/indentation state. Editing the result then rewrites the line still stored in the document:Table.__copy__andInlineTable.__copy__in the same file already passself._trivia.copy()—Trivia.copy()exists for exactly this — and the container's_replace_atcopies trivia explicitly, so only the six arithmetic/replace()paths leak. This adds the same.copy()there; nothing changes in what gets rendered at creation time, only the aliasing is cut.New parametrized test
test_arithmetic_result_does_not_share_trivia(int/float/string/datetime/date/time): assertsresult.trivia is not original.triviaand that mutating the result's comment leaves the document byte-identical. All 6 cases fail on current master and pass here. Full suite:pytest tests/ --ignore=tests/test_toml_tests.py— 529 passed.Agent Drafting Metadata