jacebrowning / jacebrowning/datafiles

TypedDict is not supported

Open
#266 1 comment 1 reaction 0 assignees View on GitHub
bug
Dominant language
Python
Stars
213
Forks
23
Avg merge
1d 22h
Merged PRs (30d)
5

Description

Hi there,

I'm new to datafiles but found it very useful.
However, my codebase has a TypedDict, which causes an error.

```
from datafiles import datafile
from dataclasses import dataclass
from typing import TypedDict # for Python 3.8 or newer
# from typing_extensions import TypedDict # for Python 3.7 or older

class Job(TypedDict):
title: str
salary: int

@datafile("{self.name}.yml")
class Person:
name: str
nationality: str
job: Job

Person("Bob", "UK", {"name":"CEO", "salary": 999999999})
```

I got the following error:
```
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
[](https://localhost:8080/#) in ()
15 job: Job
16
---> 17 Person("Bob", "UK", {"name":"CEO", "salary": 999999999})

3 frames
[/usr/local/lib/python3.7/dist-packages/datafiles/model.py](https://localhost:8080/#) in modified_init(self, *args, **kwargs)
81 with hooks.disabled():
82 init(self, *args, **kwargs)
---> 83 Model.__post_init__(self)
84
85 cls.__init__ = modified_init

[/usr/local/lib/python3.7/dist-packages/datafiles/model.py](https://localhost:8080/#) in __post_init__(self)
16 log.debug(f"Initializing {self.__class__} object")
17
---> 18 self.datafile = create_mapper(self)
19
20 if settings.HOOKS_ENABLED:

[/usr/local/lib/python3.7/dist-packages/datafiles/mapper.py](https://localhost:8080/#) in create_mapper(obj, root)
286 self_name = f"self.{field.name}"
287 if pattern is None or self_name not in pattern:
--> 288 attrs[field.name] = map_type(resolve(field.type, obj), name=field.name) # type: ignore
289
290 return Mapper(

[/usr/local/lib/python3.7/dist-packages/datafiles/converters/__init__.py](https://localhost:8080/#) in map_type(cls, name, item_cls)
165 return Enumeration.of_type(cls)
166
--> 167 raise TypeError(f"Could not map type: {cls}")

TypeError: Could not map type:
```

Contributor guide

Open the contributing guide

Research direction

Start in datafiles/mapper.py at the create_mapper call shown in the traceback, then follow map_type in datafiles/converters/__init__.py where the TypedDict error is raised. Run the provided TypedDict example while tracing that path; done means the example no longer raises the mapping TypeError and the TypedDict fields are handled.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.