Skip to content

Ignore additional properties from input when deserializing to TypedDict #565

@ericbn

Description

@ericbn

Hi 👋

I have the intention to use apischema to map between an input dict and an output dict using a TypedDict that defines the output format and aliases and conversions that need to happen from the input. Basically mapping between what I receive from one API to what I should send to another API. Not sure if that's one use case that fits apischema's purpose.

To achieve this, I'd like to be able to ignore additional properties from the input. I see this works as I expected for dataclasses, but not for TypeDicts:

from dataclasses import dataclass
from typing import TypedDict

from apischema import deserialize


@dataclass
class PersonDataclass:
    first_name: str


class PersonTypedDict(TypedDict):
    first_name: str


data = {'first_name': 'John', 'last_name': 'Smith'}
deserialize(PersonDataclass, data, additional_properties=True)  # PersonDataclass(first_name='John')
deserialize(PersonTypedDict, data, additional_properties=True)  # {'first_name': 'John', 'last_name': 'Smith'}

I expected the output of the second deserialize to be {'first_name': 'John'} instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions