Skip to content

Commit

Permalink
Loop through all converter subclasses when resolving string annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Apr 18, 2021
1 parent cdf6922 commit ca9848e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
# 0.13.1 (2021-04-18)

- Fixed handling of string annotations for extended types.

# 0.13 (2021-04-17)

- Added support for generic types.
Expand Down
4 changes: 2 additions & 2 deletions datafiles/converters/__init__.py
Expand Up @@ -6,7 +6,7 @@
import log
from ruamel.yaml.scalarfloat import ScalarFloat

from ..utils import cached
from ..utils import cached, subclasses
from ._bases import Converter
from .builtins import Boolean, Float, Integer, String
from .containers import Dataclass, Dictionary, List, Set
Expand Down Expand Up @@ -119,7 +119,7 @@ def map_type(cls, *, name: str = '', item_cls: Optional[type] = None):

if isinstance(cls, str):
log.debug(f'Searching for class matching {cls!r} annotation')
for cls2 in Converter.__subclasses__():
for cls2 in subclasses(Converter):
if cls2.__name__ == cls:
register(cls, cls2)
log.debug(f'Registered {cls2} as new converter')
Expand Down
5 changes: 5 additions & 0 deletions datafiles/tests/test_converters.py
Expand Up @@ -112,6 +112,11 @@ def it_handles_string_type_annotations(expect):
converter = converters.map_type('float')
expect(converter.TYPE) == float

def it_handles_string_type_annotations_for_extensions(expect):
converter = converters.map_type('Number')
expect(converter.TYPE) == float
expect(converter.__name__) == 'Number'

def it_rejects_unknown_types(expect):
with expect.raises(
TypeError,
Expand Down
6 changes: 6 additions & 0 deletions datafiles/utils.py
Expand Up @@ -18,6 +18,12 @@
cached = lru_cache()


def subclasses(cls):
return set(cls.__subclasses__()).union(
[s for c in cls.__subclasses__() for s in subclasses(c)]
)


def get_default_field_value(instance, name):
for field in dataclasses.fields(instance):
if field.name == name:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
@@ -1,7 +1,7 @@
[tool.poetry]

name = "datafiles"
version = "0.13"
version = "0.13.1"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down

0 comments on commit ca9848e

Please sign in to comment.