Skip to content

Commit

Permalink
Fix handling of custom types with future annotations enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Sep 27, 2019
1 parent d60304d commit 7aece1f
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 41 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 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
Loading

0 comments on commit 7aece1f

Please sign in to comment.