diff --git a/conservancy_beancount/data.py b/conservancy_beancount/data.py index cd04b2391640e4a739c0633df03f301087e1c4b6..a7f4ba704448f6dcb4755edbf3bd8dc95953b008 100644 --- a/conservancy_beancount/data.py +++ b/conservancy_beancount/data.py @@ -567,7 +567,7 @@ class PostingMeta(Metadata): that may want to "split" and manipulate the metadata multiple times. """ retval = type(self)(self.txn, self.index, self.post) - retval.meta = retval.meta.new_child() + retval.meta = self.meta.new_child() return retval diff --git a/tests/test_data_posting_meta.py b/tests/test_data_posting_meta.py index 3ba0d5cfd745561548cfc799fe1ba084a440ec3d..d2d46373897a659641bfe75b71553d72cd963877 100644 --- a/tests/test_data_posting_meta.py +++ b/tests/test_data_posting_meta.py @@ -144,6 +144,26 @@ def test_mutable_copy(): assert all(post.meta is None for post in txn.postings) assert meta.date == txn.date +def test_double_detached(): + txn = testutil.Transaction(filename='f', lineno=140, postings=[ + ('Income:Donations', -19), + ]) + meta1 = data.PostingMeta(txn, 0).detached() + meta1['metakey'] = 'meta' + meta1['layerkey'] = 'one' + meta2 = meta1.detached() + meta2['layerkey'] = 'two' + expected = { + 'filename': 'f', + 'lineno': 140, + 'metakey': 'meta', + 'layerkey': 'two', + } + assert dict(meta2) == expected + expected['layerkey'] = 'one' + assert dict(meta1) == expected + assert not any(post.meta for post in txn.postings) + # The .get() tests are arguably testing the stdlib, but they're short and # they confirm that we're using the stdlib as we intend. def test_get_with_meta_value(simple_txn):