konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.76k stars 106 forks source link

Nested TypedDict support #125

Open gitpushdashf opened 3 years ago

gitpushdashf commented 3 years ago

It seems like if a dataclass includes a typeddict, if you from_dict() it, it doesn't detect that typeddict type. Instead, it thinks it's a dict.

Is that even possible?

konradhalas commented 3 years ago

Hi @gitpushdashf - thank you for reporting this issue. dacite doesn't support TypedDict but probably it should. I will add it to the roadmap, but TBH it doesn't have high priority for me - you should probably use data class instead.

TheTeaCat commented 1 year ago

Hey there, I ran into this today as I've got some dataclasses with fields that reuse types from mypy_boto3, which are all TypedDicts.

The only way I can think of working around this, without having to write my own dataclasses for all the boto3 return values I'm using, is adding a plain dict to the types of these fields in a Union, so instead of:

@dataclass
class MyDataclass:
    my_field: TypedDictFromMypyBoto3

I have:

@dataclass
class MyDataclass:
    my_field: Union[TypedDictFromMypyBoto3, dict]

It'd be nice if dacite at least simply considered TypedDicts to be equivalent to a normal dict so I didn't have to mess up these type hints. TypedDicts don't do runtime validation afaik so this should be fairly reasonable behaviour.