Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
* master:
  Fix handling of custom types with future annotations enabled
  • Loading branch information
jacebrowning committed Sep 27, 2019
2 parents 5bee030 + 7aece1f commit e445e27
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

- Added an `INDENT_YAML_BLOCKS` setting to optionally use the old serialization behavior.

# 0.4.2 (2019-09-27)

- Fixed a `TypeError` when converting custom types with `from __future__ import annotations` enabled.

# 0.4.1 (2019-08-25)

- Fixed a `TypeError` when converting non-builtin attributes.
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ DEPENDENCIES := $(VIRTUAL_ENV)/.poetry-$(shell bin/checksum pyproject.toml poetr
install: $(DEPENDENCIES) .cache

$(DEPENDENCIES): poetry.lock
@ poetry config settings.virtualenvs.in-project true
@ poetry config virtualenvs.in-project true || poetry config settings.virtualenvs.in-project true
poetry install
@ touch $@

Expand Down
13 changes: 11 additions & 2 deletions datafiles/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
_REGISTRY: Dict[Union[type, str], type] = {}


def register(cls: type, converter: type):
def register(cls: Union[type, str], converter: type):
_REGISTRY[cls] = converter
_REGISTRY[cls.__name__] = converter
if not isinstance(cls, str):
_REGISTRY[cls.__name__] = converter


register(Integer.TYPE, Integer)
Expand Down Expand Up @@ -82,6 +83,14 @@ def map_type(cls, *, name: str = '', item_cls: Optional[type] = None):

raise TypeError(f'Unsupported container type: {cls.__origin__}')

if isinstance(cls, str):
log.debug(f'Searching for class matching {cls!r} annotation')
for cls2 in Converter.__subclasses__():
if cls2.__name__ == cls:
register(cls, cls2)
log.debug(f'Registered {cls2} as new converter')
return cls2

if not isclass(cls):
raise TypeError(f'Annotation is not a type: {cls!r}')

Expand Down
4 changes: 2 additions & 2 deletions datafiles/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def dictify(value: Any) -> Dict:
return {k: dictify(v) for k, v in value.items()}

if isinstance(value, str):
return value
return value # type: ignore

with suppress(TypeError):
return [dictify(x) for x in value]
return [dictify(x) for x in value] # type: ignore

return value

Expand Down
Loading

0 comments on commit e445e27

Please sign in to comment.