Skip to content

Commit

Permalink
Merge pull request #185 from jacebrowning/mapping-as-dict
Browse files Browse the repository at this point in the history
Treat all mapping types as dictionaries
  • Loading branch information
jacebrowning committed Jul 30, 2020
2 parents 0fadac0 + 2ea740a commit 7029a10
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions datafiles/converters/__init__.py
@@ -1,7 +1,7 @@
import dataclasses
from enum import Enum
from inspect import isclass
from typing import Any, Dict, Optional, Union
from typing import Any, Dict, Mapping, Optional, Union

import log
from ruamel.yaml.scalarfloat import ScalarFloat
Expand Down Expand Up @@ -64,7 +64,7 @@ def map_type(cls, *, name: str = '', item_cls: Optional[type] = None):
else:
converter = List.of_type(converter)

elif cls.__origin__ == dict:
elif isclass(cls.__origin__) and issubclass(cls.__origin__, Mapping):
if item_cls:
key = map_type(str)
value = map_type(item_cls)
Expand Down
6 changes: 5 additions & 1 deletion datafiles/tests/test_converters.py
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from enum import Enum
from typing import ByteString, Dict, List, Optional
from typing import ByteString, Dict, List, Mapping, Optional

import pytest
from ruamel.yaml.scalarstring import LiteralScalarString
Expand Down 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_handles_abstract_mapping_types(expect):
converter = converters.map_type(Mapping[str, int])
expect(converter.__name__) == 'StringIntegerDict'

def it_handles_dataclasses(expect):
converter = converters.map_type(MyDataclass)
expect(converter.__name__) == 'MyDataclassConverter'
Expand Down

0 comments on commit 7029a10

Please sign in to comment.