cattrs icon indicating copy to clipboard operation
cattrs copied to clipboard

Support TypedDict?

Open ericbn opened this issue 3 years ago • 1 comments

  • cattrs version: 22.1.0
  • Python version: 3.9.13
  • Operating System: macOS 12.5

Description

I know cattrs does not currently support TypedDict. Is this something that would make sense as a new feature?

I want to convert an input dict into an output dict, ideally with a structure defined as a TypedDict. Each value in the input dict should be converted to the correspondent type in the TypedDict definition. The problem was described in https://stackoverflow.com/q/65196658/2654518.

What I Did

from typing import TypedDict

import cattrs


class Test(TypedDict):
    value: int


cattrs.structure({'value': '42', 'foo': 'bar'}, Test)

With TypedDict support, the output would be {'value': 42}, instead of {'value': '42', 'foo': 'bar'}.

ericbn avatar Aug 14 '22 23:08 ericbn

We don't support it currently, but we definitely should. Just haven't gotten around to it.

Tinche avatar Aug 15 '22 11:08 Tinche

This is in PR now (#364), very close.

Tinche avatar May 20 '23 17:05 Tinche

Fixed on main.

Tinche avatar May 22 '23 23:05 Tinche