Skip to content

Commit

Permalink
Merge 36e1abc into fe9f0a0
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Feb 16, 2020
2 parents fe9f0a0 + 36e1abc commit 66533b8
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

- Added a `YAML_LIBRARY` setting to control the underlying YAML library.
- Fixed ORM methods to handle relative file patterns.
- Updated the `@datafile` decorator to be able to be called without parentheses to act as `@dataclass`.

# 0.6 (2020-01-25)

Expand Down
4 changes: 3 additions & 1 deletion datafiles/converters/containers.py
Expand Up @@ -178,7 +178,9 @@ def to_preserialization_data(cls, python_value, *, default_to_skip=None):

with suppress(AttributeError):
if value == getattr(default_to_skip, name):
log.debug(f"Skipped default value for '{name}' attribute")
log.debug(
f"Skipped default value of {value!r} for {name!r} attribute"
)
continue

data[name] = converter.to_preserialization_data(value)
Expand Down
9 changes: 6 additions & 3 deletions datafiles/decorators.py
Expand Up @@ -2,7 +2,7 @@

import dataclasses
from pathlib import Path
from typing import TYPE_CHECKING, Dict, Optional
from typing import TYPE_CHECKING, Callable, Dict, Optional, Union

from .config import Meta
from .model import create_model
Expand All @@ -13,7 +13,7 @@


def datafile(
pattern: str,
pattern: Union[str, Callable],
attrs: Optional[Dict[str, Converter]] = None,
manual: bool = Meta.datafile_manual,
defaults: bool = Meta.datafile_defaults,
Expand All @@ -23,7 +23,7 @@ def datafile(
):
"""Synchronize a data class to the specified path."""

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

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

return decorator


Expand Down
2 changes: 1 addition & 1 deletion datafiles/mapper.py
Expand Up @@ -154,7 +154,7 @@ def _get_data(self, include_default_values: Trilean = None) -> Dict:
value == self._get_default_field_value(name)
and not include_default_values
):
log.debug(f"Skipped default value for '{name}' attribute")
log.debug(f"Skipped default value of {value!r} for {name!r} attribute")
data.pop(name)

else:
Expand Down
10 changes: 9 additions & 1 deletion datafiles/tests/test_decorators.py
Expand Up @@ -10,7 +10,7 @@ def it_turns_normal_class_into_dataclass(expect):
class Normal:
pass

cls = decorators.datafile("")(Normal)
cls = decorators.datafile("<pattern>")(Normal)

expect(is_dataclass(cls)) == True

Expand All @@ -22,3 +22,11 @@ class Existing:
cls = decorators.datafile("")(Existing)

expect(id(cls)) == id(Existing)

def it_maps_to_dataclass_without_paranthesis(expect):
class Sample:
pass

cls = decorators.datafile(Sample)

expect(is_dataclass(cls)) == True
12 changes: 12 additions & 0 deletions docs/types/containers.md
Expand Up @@ -72,4 +72,16 @@ bar:
qux: Hello, world!
```

For convenience, `@datafile` can also be used in place of `@dataclass`:

```
#!python hl_lines="4"
from datafiles import datafile
@datafile
class Nested:
qux: str
```

More examples can be found in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/develop/notebooks/nested_dataclass.ipynb).
87 changes: 50 additions & 37 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.7b3"
version = "0.7b4"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down

0 comments on commit 66533b8

Please sign in to comment.