Skip to content

Commit

Permalink
Merge pull request #205 from jacebrowning/add-dict-hooks
Browse files Browse the repository at this point in the history
Add hooks to dictionary values
  • Loading branch information
jacebrowning committed Apr 9, 2021
2 parents 13bebc4 + 32ae3ee commit de13464
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions datafiles/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ def apply(instance, mapper):
item.datafile = create_mapper(item, root=mapper)
apply(item, mapper)

elif isinstance(instance, dict):
for value in instance.values():
with suppress(AttributeError):
value.datafile = create_mapper(value, root=mapper)
apply(value, mapper)


def load_before(cls, method):
"""Decorate methods that should load before call."""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]

name = "datafiles"
version = "0.13b3"
version = "0.13b4"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ addopts =
--pdbcls=IPython.terminal.debugger:Pdb

-r sxX
--show-capture=log

--cov=datafiles
--cov-report=html
Expand Down
37 changes: 37 additions & 0 deletions tests/test_file_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,40 @@ def test_float_inference(expect):
- 4.0
"""
)


def test_nested_mutables(expect):
write(
'tmp/sample.yml',
"""
name: Test
roles:
category1:
- value1
- value2
category2:
- something
- else
""",
)

logbreak("Inferring object")
sample = auto('tmp/sample.yml')

logbreak("Updating attributes")
sample.roles['category1'].append('value3')

logbreak("Reading file")
expect(read('tmp/sample.yml')) == dedent(
"""
name: Test
roles:
category1:
- value1
- value2
- value3
category2:
- something
- else
"""
)

0 comments on commit de13464

Please sign in to comment.