Skip to content

Commit 1f6c88b

Browse files
authored
Merge pull request #347 from jacebrowning/fix-list-init
Fixed initialization of list attributes
2 parents 9d913e2 + 81fa5e9 commit 1f6c88b

File tree

7 files changed

+19
-17
lines changed

7 files changed

+19
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes
22

3+
## 2.3.3 (2025-02-23)
4+
5+
- Fixed list attributes being ignored during initialization with existing files.
6+
37
## 2.3.2 (2025-02-23)
48

59
- Dropped support for Python 3.8 (past end of life).

datafiles/manager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,18 @@ def get_or_create(self, *args, **kwargs) -> Model:
108108
def all(self, *, _exclude: str = "") -> Iterator[Model]:
109109
path = Path(self.model.Meta.datafile_pattern).expanduser()
110110
if path.is_absolute() or self.model.Meta.datafile_pattern[:2] == "./":
111-
log.debug(f"Detected static pattern: {path}")
111+
log.debug(f"Detected static path pattern: {path}")
112112
else:
113+
log.debug(
114+
f"Detected relative path pattern: {self.model.Meta.datafile_pattern}"
115+
)
113116
try:
114117
root = Path(inspect.getfile(self.model)).parent
115118
except (TypeError, OSError):
116119
level = log.DEBUG if "__main__" in str(self.model) else log.WARNING
117120
log.log(level, f"Unable to determine module for {self.model}")
118121
root = Path.cwd()
119122
path = root / self.model.Meta.datafile_pattern
120-
log.debug(f"Detected dynamic pattern: {path}")
121123

122124
pattern = alt_pattern = str(path.resolve())
123125
for field in dataclasses.fields(self.model):

datafiles/mapper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from cached_property import cached_property
1414

1515
from . import config, formats, hooks
16-
from .converters import Converter, List, map_type
16+
from .converters import Converter, map_type
1717
from .types import Missing, Trilean
1818
from .utils import display, get_default_field_value, recursive_update, write
1919

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

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

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

71+
log.debug(f"Detected relative path pattern: {path}")
7172
path = (root / path).resolve()
72-
log.debug(f"Detected dynamic pattern: {path}")
7373
return path
7474

7575
@property
@@ -244,7 +244,7 @@ def _set_value(instance, name, converter, data, first_load):
244244
default_value,
245245
)
246246

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

datafiles/model.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ def __post_init__(self):
2727
create = not self.datafile.manual
2828

2929
if path:
30-
log.debug(f"Datafile path: {path}")
31-
log.debug(f"Datafile exists: {exists}")
30+
log.debug(f"Resolved datafile path: {path} ({exists=})")
3231

3332
if exists:
3433
self.datafile.load(_first_load=True)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tool.poetry]
22

33
name = "datafiles"
4-
version = "2.3.2"
4+
version = "2.3.3"
55
description = "File-based ORM for dataclasses."
66

77
license = "MIT"

tests/test_instantiation.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
from dataclasses import dataclass, field
66
from typing import Dict
77

8-
import pytest
9-
108
from datafiles import Missing, datafile
119
from datafiles.utils import logbreak, write
1210

@@ -67,7 +65,6 @@ def it_wins_when_no_init_values(expect):
6765
expect(sample.foo) == 2
6866
expect(sample.bar) == "b"
6967

70-
@pytest.mark.xfail(reason="https://github.com/jacebrowning/datafiles/issues/344")
7168
def it_loses_against_init_values(expect):
7269
write(
7370
"tmp/sample.yml",

tests/test_loading.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88

9-
from datafiles import datafile
9+
from datafiles import Missing, datafile
1010
from datafiles.utils import dedent, logbreak, read, write
1111

1212
from . import xfail_with_pep_563
@@ -384,7 +384,7 @@ def with_matching_types(expect):
384384
""",
385385
)
386386

387-
sample = SampleWithList(None)
387+
sample = SampleWithList(Missing)
388388

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

@@ -396,7 +396,7 @@ def with_conversion(expect):
396396
""",
397397
)
398398

399-
sample = SampleWithList(None)
399+
sample = SampleWithList(Missing)
400400

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

@@ -452,7 +452,7 @@ def with_matching_types(expect):
452452
""",
453453
)
454454

455-
sample = SampleWithSet(None)
455+
sample = SampleWithSet(Missing)
456456

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

@@ -464,7 +464,7 @@ def with_conversion(expect):
464464
""",
465465
)
466466

467-
sample = SampleWithSet(None)
467+
sample = SampleWithSet(Missing)
468468

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

0 commit comments

Comments
 (0)