Skip to content

Commit

Permalink
Handle missing attributes before Python 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Jul 28, 2022
1 parent 3be02b7 commit 1fdd8fd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
5 changes: 4 additions & 1 deletion datafiles/converters/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ class Dictionary(Converter):

@classmethod
def of_mapping(cls, key: type, value: type):
name = f"{key.__name__}{value.__name__}Dict"
try:
name = f"{key.__name__}{value.__name__}Dict"
except AttributeError: # Python < 3.10
name = "UntypedDict"
bases = (cls,)
return type(name, bases, {})

Expand Down
5 changes: 5 additions & 0 deletions datafiles/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@
sys.version_info < (3, 10),
reason="Union types (PEP 604) are not available in Python 3.9 and earlier",
)

xfail_without_type_names = pytest.mark.xfail(
sys.version_info < (3, 10),
reason="Types lack a __name__ in Python 3.9 and earlier",
)
3 changes: 2 additions & 1 deletion datafiles/tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from datafiles import converters, settings

from . import xfail_without_pep_604
from . import xfail_without_pep_604, xfail_without_type_names


@dataclass
Expand Down Expand Up @@ -95,6 +95,7 @@ def it_requires_dict_annotations_to_have_types(expect):
with expect.raises(TypeError, "Types are required with 'Dict' annotation"):
converters.map_type(Dict)

@xfail_without_type_names
def it_handles_typed_dict_annotations(expect):
converter = converters.map_type(MyTypedDict)
expect(converter.__name__) == "StringAnyDict"
Expand Down

0 comments on commit 1fdd8fd

Please sign in to comment.