Skip to content

Commit 85f2716

Browse files
authored
Merge pull request #170 from jacebrowning/fix-notebooks
Restore custom list/dict types to fix notebooks
2 parents 2477c96 + 7642d6b commit 85f2716

17 files changed

+76
-48
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ install:
2121
script:
2222
- make test-repeat
2323
- make check
24+
- make notebooks
2425
- make mkdocs
2526

2627
after_success:

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ test-profile: install
8989
MKDOCS_INDEX := site/index.html
9090

9191
.PHONY: docs
92-
docs: mkdocs uml papermill ## Generate documentation and UML
92+
docs: mkdocs uml notebooks ## Generate documentation and UML
9393

9494
.PHONY: mkdocs
9595
mkdocs: install $(MKDOCS_INDEX)
@@ -116,12 +116,12 @@ docs/*.png: $(MODULES)
116116
- mv -f classes_$(PACKAGE).png docs/classes.png
117117
- mv -f packages_$(PACKAGE).png docs/packages.png
118118

119-
.PHONY: papermill
120-
papermill: install
119+
.PHONY: notebooks
120+
notebooks: install
121121
@ cd notebooks; for filename in *.ipynb; do \
122122
poetry run papermill $$filename $$filename; \
123123
done
124-
git config filter.nbstripout.extrakeys 'metadata.papermill cell.metadata.papermill'
124+
git config filter.nbstripout.extrakeys 'cell.metadata.execution cell.metadata.papermill metadata.papermill'
125125
poetry run nbstripout --keep-output notebooks/*.ipynb
126126

127127
# RELEASE #####################################################################

datafiles/formats.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import log
1111

12-
from . import settings
12+
from . import settings, types
1313

1414

1515
_REGISTRY: Dict[str, type] = {}
@@ -96,6 +96,13 @@ def deserialize(cls, file_object):
9696
def serialize(cls, data):
9797
from ruamel import yaml
9898

99+
yaml.representer.RoundTripRepresenter.add_representer(
100+
types.List, yaml.representer.RoundTripRepresenter.represent_list
101+
)
102+
yaml.representer.RoundTripRepresenter.add_representer(
103+
types.Dict, yaml.representer.RoundTripRepresenter.represent_dict
104+
)
105+
99106
if settings.INDENT_YAML_BLOCKS:
100107
f = StringIO()
101108
y = yaml.YAML()

datafiles/hooks.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
from functools import wraps
44

55
import log
6-
from ruamel.yaml.comments import CommentedMap, CommentedSeq
76

8-
from . import settings
7+
from . import settings, types
98
from .mapper import create_mapper
109

1110

@@ -49,10 +48,10 @@ def apply(instance, mapper):
4948
for attr_name in instance.datafile.attrs:
5049
attr = getattr(instance, attr_name)
5150
if isinstance(attr, list):
52-
attr = CommentedSeq(attr)
51+
attr = types.List(attr)
5352
setattr(instance, attr_name, attr)
5453
elif isinstance(attr, dict):
55-
attr = CommentedMap(attr)
54+
attr = types.Dict(attr)
5655
setattr(instance, attr_name, attr)
5756
elif not is_dataclass(attr):
5857
continue

datafiles/mapper.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@
1313

1414
from . import config, formats, hooks
1515
from .converters import Converter, List, map_type
16-
from .utils import (
17-
Missing,
18-
Trilean,
19-
display,
20-
get_default_field_value,
21-
recursive_update,
22-
write,
23-
)
16+
from .types import Missing, Trilean
17+
from .utils import display, get_default_field_value, recursive_update, write
2418

2519

2620
class Mapper:

datafiles/tests/test_utils.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,25 @@ def with_unchanged_immutables(expect):
7070
expect(old) == new
7171
expect(id(old['my_dict']['my_str'])) == previous_id
7272

73-
def with_updated_types(expect):
74-
old = {'x': 1}
75-
new = {'x': 1.0}
76-
previous_id = id(old['x'])
73+
def with_updated_numeric_types(expect):
74+
old = {'value': 1}
75+
new = {'value': 1.0}
76+
previous_id = id(old['value'])
7777

7878
recursive_update(old, new)
7979

8080
expect(old) == new
81-
expect(id(old['x'])) != previous_id
81+
expect(id(old['value'])) != previous_id
82+
83+
def with_updated_boolean_types(expect):
84+
old = {'value': 1}
85+
new = {'value': True}
86+
previous_id = id(old['value'])
87+
88+
recursive_update(old, new)
89+
90+
expect(old) == new
91+
expect(id(old['value'])) != previous_id
8292

8393
def describe_merge():
8494
def with_shoreter_list_into_longer(expect):

datafiles/types.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import dataclasses
2+
from typing import Optional
3+
4+
5+
Trilean = Optional[bool]
6+
Missing = dataclasses._MISSING_TYPE
7+
8+
9+
class List(list):
10+
"""Patchable `list` type."""
11+
12+
13+
class Dict(dict):
14+
"""Patchable `dict` type."""

datafiles/utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
from pathlib import Path
77
from pprint import pformat
88
from shutil import get_terminal_size
9-
from typing import Any, Dict, Optional, Union
9+
from typing import Any, Dict, Union
1010

1111
import log
1212

13-
14-
Trilean = Optional[bool]
15-
Missing = dataclasses._MISSING_TYPE
13+
from .types import Missing
1614

1715

1816
cached = lru_cache()
@@ -85,7 +83,7 @@ def _merge(old: Any, new: Any) -> Any:
8583

8684
return old
8785

88-
if new == old and isinstance(new, type(old)):
86+
if repr(new) == repr(old):
8987
return old
9088

9189
return new

notebooks/file_inference.ipynb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
"script:\n",
158158
" - make test-repeat\n",
159159
" - make check\n",
160-
" - make mkdocs\n",
160+
" - make docs\n",
161161
"\n",
162162
"after_success:\n",
163163
" - pip install coveralls\n",
@@ -237,7 +237,9 @@
237237
" - 3.8\n",
238238
"cache:\n",
239239
" pip: true\n",
240-
" directories: []\n",
240+
" directories:\n",
241+
" - ${VIRTUAL_ENV}\n",
242+
"\n",
241243
"before_install:\n",
242244
" - curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py\n",
243245
" | python\n",
@@ -249,7 +251,7 @@
249251
"script:\n",
250252
" - make test-repeat\n",
251253
" - make check\n",
252-
" - make mkdocs\n",
254+
" - make docs\n",
253255
"after_success:\n",
254256
" - pip install coveralls\n",
255257
" - coveralls\n",
@@ -283,7 +285,7 @@
283285
"name": "python",
284286
"nbconvert_exporter": "python",
285287
"pygments_lexer": "ipython3",
286-
"version": "3.7.2"
288+
"version": "3.8.2"
287289
}
288290
},
289291
"nbformat": 4,

notebooks/format_options.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@
256256
"name": "python",
257257
"nbconvert_exporter": "python",
258258
"pygments_lexer": "ipython3",
259-
"version": "3.7.2"
259+
"version": "3.8.2"
260260
}
261261
},
262262
"nbformat": 4,

0 commit comments

Comments
 (0)