Skip to content

Commit

Permalink
Require 'Dict' annotations to have types
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Sep 13, 2020
1 parent 847ad0f commit 63bc043
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.11.1 (2020-09-12)

- Fixed error message for `Dict` annotations lacking types.

# 0.11 (2020-09-10)

- Added support to treat `Mapping` type annotations as dictionaries.
Expand Down
10 changes: 8 additions & 2 deletions datafiles/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ def map_type(cls, *, name: str = '', item_cls: Optional[type] = None):
value = map_type(item_cls)
else:
log.warn("Schema enforcement not possible with 'Dict' annotation")
key = map_type(cls.__args__[0])
value = map_type(cls.__args__[1])
try:
key = map_type(cls.__args__[0])
value = map_type(cls.__args__[1])
except TypeError as e:
assert '~' in str(e), f'Unhandled error: ' + str(e)
raise TypeError(
"Types are required with 'Dict' annotation"
) from None

converter = Dictionary.of_mapping(key, value)

Expand Down
4 changes: 4 additions & 0 deletions datafiles/tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def it_handles_dict_annotations(expect):
converter = converters.map_type(Dict[str, int])
expect(converter.__name__) == 'StringIntegerDict'

def it_requires_dict_annotations_to_have_types(expect):
with expect.raises(TypeError, "Types are required with 'Dict' annotation"):
converters.map_type(Dict)

def it_handles_abstract_mapping_types(expect):
converter = converters.map_type(Mapping[str, int])
expect(converter.__name__) == 'StringIntegerDict'
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mkdocs==1.0.4
Pygments==2.6.1
Pygments==2.7.0
19 changes: 9 additions & 10 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]

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

license = "MIT"
Expand Down

0 comments on commit 63bc043

Please sign in to comment.