Skip to content

Commit

Permalink
Updated Manager.get to be a bit more flexible with arguments
Browse files Browse the repository at this point in the history
* Fixes Manager.get and allows only passing arguments in pattern

* Changes from `make check`

* Adds `Manager.get` tests and ensures `Manager.get` complies
  • Loading branch information
bnorick committed Feb 11, 2023
1 parent 8f90d43 commit 91d1584
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 9 deletions.
55 changes: 47 additions & 8 deletions datafiles/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
from parse import parse
from ruamel.yaml.error import MarkedYAMLError

from . import hooks
from . import hooks, model

if TYPE_CHECKING:
from .model import Model


Trilean = Optional[bool]
Missing = dataclasses._MISSING_TYPE
_NOT_PASSED = object() # sentinel


class Splats:
Expand All @@ -34,22 +35,60 @@ def __init__(self, cls):
self.model = cls

def get(self, *args, **kwargs) -> Model:
fields = dataclasses.fields(self.model)
required = sum(1 for field in fields if isinstance(field.default, Missing))
missing_args = [Missing] * (required - len(args) - len(kwargs))
args = (*args, *missing_args)

with hooks.disabled():
instance = self.model(*args, **kwargs)
instance = self.model.__new__(self.model)

# We **must** initialize with a value all fields on the uninitialized
# instance which play a role in loading, e.g., those with placeholders
# in the pattern. Other init fields of the instance which are not passed
# as args or kwargs will be set to `dataclasses._MISSING_TYPE` and their
# values will be loaded over.
fields = [field for field in dataclasses.fields(self.model) if field.init]
pattern = self.model.Meta.datafile_pattern
args_iter = iter(args)
for field in fields:
placeholder = f"{{self.{field.name}}}"

try:
# we always need to consume an arg if it exists,
# even if it's not one with a placeholder
value = next(args_iter)
except StopIteration:
value = kwargs.get(field.name, _NOT_PASSED)

if placeholder in pattern:
if value is _NOT_PASSED:
raise TypeError(
f"Manager.get() missing required placeholder field argument: '{field.name}'"
)

if value is _NOT_PASSED:
if not isinstance(field.default, Missing):
value = field.default
elif not isinstance(field.default_factory, Missing):
value = field.default_factory()
else:
value = Missing
setattr(instance, field.name, value)

# NOTE: the following doesn't call instance.datafile.load because hooks are disabled currently
model.Model.__post_init__(instance)

try:
instance.datafile.load()
instance.datafile.load(_first_load=True)
except MarkedYAMLError as e:
log.critical(
f"Deleting invalid YAML: {instance.datafile.path} ({e.problem})"
)
instance.datafile.path.unlink()
instance.datafile.load()

# reconstruct the dataclass so that __init__ gets called
instance = dataclasses.replace(instance)

# make sure the mapper knows that it's actually been loaded
instance.datafile.modified = False

return instance

def get_or_none(self, *args, **kwargs) -> Optional[Model]:
Expand Down
43 changes: 42 additions & 1 deletion datafiles/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import pytest

from datafiles.manager import Manager
from datafiles.manager import Manager, Missing
from datafiles.model import create_model


Expand Down Expand Up @@ -37,6 +37,47 @@ def manager_home():
model = create_model(Nested, pattern="~/.{self.name}.json")
return Manager(model)

@pytest.fixture()
def manager_with_files():
files_dir = Path(__file__).parent / "files"
shutil.rmtree(files_dir, ignore_errors=True)
files_dir.mkdir(exist_ok=True)
model = create_model(MyClass, pattern="files/{self.foo}.yml")
model(foo=1, bar=2).datafile.save()
return Manager(model)

def describe_get():
@patch("datafiles.mapper.Mapper.load")
@patch("datafiles.mapper.Mapper.exists", True)
@patch("datafiles.mapper.Mapper.modified", False)
def when_partial_args_passed_init_args_missing(mock_load, expect, manager):
got = manager.get(1)
expect(got.foo) == 1
expect(got.bar) is Missing
expect(mock_load.called).is_(True)

with expect.raises(
TypeError,
"Manager.get() missing required placeholder field argument: 'foo'",
):
manager.get(bar=2)

@patch("datafiles.mapper.Mapper.exists", True)
@patch("datafiles.mapper.Mapper.modified", False)
def when_partial_args_passed_init_arg_missing_file_exists(
expect, manager_with_files
):
# demonstrates that `Manager.get` loads the value for bar, when it is not passed
expect(manager_with_files.get(1)) == MyClass(foo=1, bar=2)

@patch("datafiles.mapper.Mapper.exists", True)
@patch("datafiles.mapper.Mapper.modified", False)
def when_partial_kwargs_passed_init_arg_missing_file_exists(
expect, manager_with_files
):
# demonstrates that `Manager.get` loads the value for bar, when it is not passed
expect(manager_with_files.get(foo=1)) == MyClass(foo=1, bar=2)

def describe_get_or_none():
@patch("datafiles.mapper.Mapper.load")
@patch("datafiles.mapper.Mapper.exists", True)
Expand Down

0 comments on commit 91d1584

Please sign in to comment.