Skip to content

Commit

Permalink
Delay setting of 'datafile' so that root is set properly
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Jan 13, 2019
1 parent 765b6ab commit 40bb5a7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions datafiles/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,20 @@ def _set_dataclass_value(self, data, name, converter):
nested_data[field.name] = None # type: ignore
dataclass = converter.to_python_value(nested_data, target=dataclass)

# TODO: Figure out why datafile wasn't set
if not hasattr(dataclass, 'datafile'):
# TODO: Find a way to avoid this circular import
try:
datafile = dataclass.datafile
except AttributeError:
from .models import build_datafile

log.warn(f"{dataclass} was missing 'datafile'")
dataclass.datafile = build_datafile(dataclass)
log.warn(f"{dataclass} has not yet been patched to have 'datafile'")
datafile = build_datafile(dataclass)

for name2, converter2 in dataclass.datafile.attrs.items():
for name2, converter2 in datafile.attrs.items():
_value = nested_data.get( # type: ignore
# pylint: disable=protected-access
name2,
dataclass.datafile._get_default_field_value(name2),
datafile._get_default_field_value(name2),
)
value = converter2.to_python_value(_value, target=getattr(dataclass, name2))
log.debug(f"'{name2}' as Python: {value!r}")
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.1a12"
version = "0.1a13"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down

0 comments on commit 40bb5a7

Please sign in to comment.