Skip to content

Commit

Permalink
Forward all dataclass arguments when no pattern is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Mar 1, 2020
1 parent 018721c commit 7d0723c
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 61 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
# 0.8 (unreleased)

- Updated the `@datafile(...)` decorator to be used as a drop-in replacement for `@dataclass(...)`.

# 0.7 (2020-02-20)

- Added a `YAML_LIBRARY` setting to control the underlying YAML library.
Expand Down
12 changes: 8 additions & 4 deletions datafiles/decorators.py
Expand Up @@ -13,16 +13,23 @@


def datafile(
pattern: Union[str, Callable],
pattern: Union[str, Callable, None] = None,
attrs: Optional[Dict[str, Converter]] = None,
manual: bool = Meta.datafile_manual,
defaults: bool = Meta.datafile_defaults,
auto_load: bool = Meta.datafile_auto_load,
auto_save: bool = Meta.datafile_auto_save,
auto_attr: bool = Meta.datafile_auto_attr,
**kwargs,
):
"""Synchronize a data class to the specified path."""

if pattern is None:
return dataclasses.dataclass(**kwargs)

if callable(pattern):
return dataclasses.dataclass(pattern) # type: ignore

def decorator(cls=None):
if dataclasses.is_dataclass(cls):
dataclass = cls
Expand All @@ -40,9 +47,6 @@ def decorator(cls=None):
auto_attr=auto_attr,
)

if callable(pattern):
return dataclasses.dataclass(pattern) # type: ignore

return decorator


Expand Down
8 changes: 8 additions & 0 deletions datafiles/tests/test_decorators.py
Expand Up @@ -30,3 +30,11 @@ class Sample:
cls = decorators.datafile(Sample)

expect(is_dataclass(cls)) == True

def it_forwards_arguments_dataclass_decorator(expect):
class Sample:
pass

cls = decorators.datafile(order=True)(Sample)

expect(is_dataclass(cls)) == True
122 changes: 67 additions & 55 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
@@ -1,7 +1,7 @@
[tool.poetry]

name = "datafiles"
version = "0.7"
version = "0.8b1"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Expand Up @@ -22,7 +22,7 @@ markers =

ignore =.cache,htmlcov,tml

runner = pytest --failed-first --maxfail=1 --disable-warnings
runner = pytest --failed-first --maxfail=1 --disable-warnings --no-flaky-report

clear = true
nobeep = true
Expand Down

0 comments on commit 7d0723c

Please sign in to comment.