Skip to content

Commit

Permalink
Merge pull request #164 from jacebrowning/hotfix/v0.8.1
Browse files Browse the repository at this point in the history
Fix loading of missing nested dataclass attributes
  • Loading branch information
jacebrowning committed Mar 31, 2020
2 parents 7171f96 + d369dea commit f6f7715
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.8.1 (2020-03-30)

- Fixed loading of `Missing` nested dataclasses attributes.

# 0.8 (2020-03-28)

- Updated the `@datafile(...)` decorator to be used as a drop-in replacement for `@dataclass(...)`.
Expand Down
2 changes: 1 addition & 1 deletion datafiles/converters/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def to_python_value(cls, deserialized_data, *, target_object):

new_value = cls.DATACLASS(**data) # pylint: disable=not-callable

if target_object is None:
if target_object is None or target_object is Missing:
value = new_value
else:
value = target_object
Expand Down
18 changes: 9 additions & 9 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.8"
version = "0.8.1"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down
27 changes: 25 additions & 2 deletions tests/test_instantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from dataclasses import dataclass, field

from datafiles import datafile
from datafiles.utils import write
from datafiles import Missing, datafile
from datafiles.utils import logbreak, write


@datafile('../tmp/sample.yml', manual=True)
Expand Down Expand Up @@ -171,3 +171,26 @@ def when_file_exists(expect):
expect(sample.a) == 1.2
expect(sample.b) == 3.4
expect(sample.c) == 9.9


def describe_missing_attributes():
def when_dataclass(expect):
@dataclass
class Name:
value: str

@datafile("../tmp/samples/{self.key}.yml")
@dataclass
class Sample:

key: int
name: Name
value: float = 0.0

sample = Sample(42, Name("Widget"))

logbreak("Loading missing 'name' dataclass")

sample2 = Sample(42, Missing) # type: ignore

expect(sample2.name.value) == "Widget"

0 comments on commit f6f7715

Please sign in to comment.