Skip to content

Commit

Permalink
Fixed initialization of list attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Feb 22, 2025
1 parent 4af5d9a commit 0cbe576
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
6 changes: 4 additions & 2 deletions datafiles/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,18 @@ def get_or_create(self, *args, **kwargs) -> Model:
def all(self, *, _exclude: str = "") -> Iterator[Model]:
path = Path(self.model.Meta.datafile_pattern).expanduser()
if path.is_absolute() or self.model.Meta.datafile_pattern[:2] == "./":
log.debug(f"Detected static pattern: {path}")
log.debug(f"Detected static path pattern: {path}")
else:
log.debug(
f"Detected relative path pattern: {self.model.Meta.datafile_pattern}"
)
try:
root = Path(inspect.getfile(self.model)).parent
except (TypeError, OSError):
level = log.DEBUG if "__main__" in str(self.model) else log.WARNING
log.log(level, f"Unable to determine module for {self.model}")
root = Path.cwd()
path = root / self.model.Meta.datafile_pattern
log.debug(f"Detected dynamic pattern: {path}")

pattern = alt_pattern = str(path.resolve())
for field in dataclasses.fields(self.model):
Expand Down
8 changes: 4 additions & 4 deletions datafiles/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from cached_property import cached_property

from . import config, formats, hooks
from .converters import Converter, List, map_type
from .converters import Converter, map_type
from .types import Missing, Trilean
from .utils import display, get_default_field_value, recursive_update, write

Expand Down Expand Up @@ -57,7 +57,7 @@ def path(self) -> Optional[Path]:

path = Path(self._pattern.format(self=self._instance)).expanduser()
if path.is_absolute() or self._pattern.startswith("./"):
log.debug(f"Detected static pattern: {path}")
log.debug(f"Detected static path pattern: {path}")
return path.resolve()

cls = self._instance.__class__
Expand All @@ -68,8 +68,8 @@ def path(self) -> Optional[Path]:
log.log(level, f"Unable to determine module for {cls}")
root = Path.cwd()

log.debug(f"Detected relative path pattern: {path}")
path = (root / path).resolve()
log.debug(f"Detected dynamic pattern: {path}")
return path

@property
Expand Down Expand Up @@ -244,7 +244,7 @@ def _set_value(instance, name, converter, data, first_load):
default_value,
)

if init_value != default_value and not issubclass(converter, List):
if init_value != default_value:
log.debug(f"Keeping non-default '{name}' init value: {init_value!r}")
return

Expand Down
3 changes: 1 addition & 2 deletions datafiles/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ def __post_init__(self):
create = not self.datafile.manual

if path:
log.debug(f"Datafile path: {path}")
log.debug(f"Datafile exists: {exists}")
log.debug(f"Resolved datafile path: {path} ({exists=})")

if exists:
self.datafile.load(_first_load=True)
Expand Down
3 changes: 0 additions & 3 deletions tests/test_instantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from dataclasses import dataclass, field
from typing import Dict

import pytest

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

Expand Down Expand Up @@ -67,7 +65,6 @@ def it_wins_when_no_init_values(expect):
expect(sample.foo) == 2
expect(sample.bar) == "b"

@pytest.mark.xfail(reason="https://github.com/jacebrowning/datafiles/issues/344")
def it_loses_against_init_values(expect):
write(
"tmp/sample.yml",
Expand Down
10 changes: 5 additions & 5 deletions tests/test_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest

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

from . import xfail_with_pep_563
Expand Down Expand Up @@ -384,7 +384,7 @@ def with_matching_types(expect):
""",
)

sample = SampleWithList(None)
sample = SampleWithList(Missing)

expect(sample.items) == [1.2, 3.4]

Expand All @@ -396,7 +396,7 @@ def with_conversion(expect):
""",
)

sample = SampleWithList(None)
sample = SampleWithList(Missing)

expect(sample.items) == [1.0, 2.3]

Expand Down Expand Up @@ -452,7 +452,7 @@ def with_matching_types(expect):
""",
)

sample = SampleWithSet(None)
sample = SampleWithSet(Missing)

expect(sample.items) == {1.2, 3.4}

Expand All @@ -464,7 +464,7 @@ def with_conversion(expect):
""",
)

sample = SampleWithSet(None)
sample = SampleWithSet(Missing)

expect(sample.items) == {1.0, 2.3}

Expand Down

0 comments on commit 0cbe576

Please sign in to comment.