Skip to content

Commit

Permalink
Add setting to disable list diff minimization
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Aug 25, 2020
1 parent a331b1f commit 263f51f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 71 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

- Added support to treat `Mapping` type annotations as dictionaries.
- Fixed handling of default values for `dataclass` attributes.
- Added `MINIMIZE_LIST_DIFFS` setting to control the semantic representation of empty lists.

# 0.10 (2020-07-03)

Expand Down
6 changes: 5 additions & 1 deletion datafiles/converters/containers.py
Expand Up @@ -5,6 +5,7 @@

import log

from .. import settings
from ..utils import Missing, get_default_field_value
from ._bases import Converter

Expand Down Expand Up @@ -81,7 +82,10 @@ def to_preserialization_data(cls, python_value, *, default_to_skip=None):
if data == default_to_skip:
data.clear()

return data or [None]
if settings.MINIMIZE_LIST_DIFFS:
return data or [None]

return data


class Dictionary(Converter):
Expand Down
2 changes: 2 additions & 0 deletions datafiles/settings.py
Expand Up @@ -6,4 +6,6 @@

INDENT_YAML_BLOCKS = True

MINIMIZE_LIST_DIFFS = True

YAML_LIBRARY = 'ruamel.yaml'
7 changes: 6 additions & 1 deletion datafiles/tests/test_converters.py
Expand Up @@ -7,7 +7,7 @@
import pytest
from ruamel.yaml.scalarstring import LiteralScalarString

from datafiles import converters
from datafiles import converters, settings


@dataclass
Expand Down Expand Up @@ -313,6 +313,11 @@ def when_dataclass_with_default(expect):
)
expect(data) == {'flag': True}

def when_empty_list_and_diff_minimization_disabled(expect, monkeypatch):
monkeypatch.setattr(settings, 'MINIMIZE_LIST_DIFFS', False)
data = StringList.to_preserialization_data([])
expect(data) == []


def describe_register():
def with_new_type(expect):
Expand Down
26 changes: 23 additions & 3 deletions docs/settings.md
@@ -1,6 +1,6 @@
<h1>Settings</h1>

For libraries that wish to temporarily alter any of the previously described behavior, a handful of settings can be set at the module level. All boolean settings default to `True` unless otherwise noted.
For clients that wish to temporarily alter any of the previously described behaviors, a handful of settings can be controlled at the module level. All values default to `True` unless otherwise noted.

# `HIDE_TRACEBACK_IN_HOOKS`

Expand All @@ -14,7 +14,7 @@ datafiles.settings.HIDE_TRACEBACK_IN_HOOKS = False

# `HOOKS_ENABLED`

When running unit tests for a library using `datafiles`,
When running unit tests for a client using `datafiles`,
it can be helpful to disable automatic loading/saving of models for performance and to avoid writing files to disk:

```python
Expand All @@ -36,7 +36,7 @@ items:
- 3
```

To make it easier to upgrade to this version, a library can disable this functionality:
To make it easier to upgrade to this version, a client can disable this functionality:

```python
import datafiles
Expand All @@ -51,6 +51,26 @@ items:
- 1
- 2
- 3

# `MINIMIZE_LIST_DIFFS`

When serializing lists, `datafiles` intentionally deviates from the semantic representation of an empty list to optimize for the use case of storing YAML files in version control.

By storing any empty list of `items` as:

```yaml
items:
-
```

adding or removing an item always results in a one-line change. Where as adding items to `items: []` produces a noisier diff and requires knowledge of the YAML specification to edit files by hand.

To disable this behavior:

```python
import datafiles
datafiles.settings.MINIMIZE_LIST_DIFFS = False
```

# `YAML_LIBRARY`
Expand Down
129 changes: 64 additions & 65 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
@@ -1,7 +1,7 @@
[tool.poetry]

name = "datafiles"
version = "0.11b2"
version = "0.11b3"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down

0 comments on commit 263f51f

Please sign in to comment.