How to deserialize multidict to data model?

Open
#407 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start with the Python reproduction in issue #407 and inspect cattrs's structuring path for multidict mappings and list-typed dataclass fields. Done means repeated keys produce all values in Model.a, a single key produces a one-item list, and the scalar b value remains correct; add or run a regression test if the repository provides a relevant test location.

Written by the indexing model from the issue text.

Description

question

multidict: https://github.com/aio-libs/multidict

import dataclasses
import typing
import cattrs
import multidict


@dataclasses.dataclass
class Model:
    a: typing.List[str]
    b: str


d = multidict.MultiDict([('a', '111'), ('b', '2'), ('a', '333')])
obj = cattrs.structure(d, Model)
print(obj)

d = multidict.MultiDict([('a', '111'), ('b', '2')])
obj = cattrs.structure(d, Model)
print(obj)

want:

Model(a=['111', '333'], b='222')
Model(a=['111'], b='222')

actual:

Model(a=['1', '1', '1'], b='222')
Model(a=['1', '1', '1'], b='222')
Dominant language
Python
Stars
1.1k
Forks
159
Avg merge
12h 21m
Merged PRs (30d)
6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-attrs/cattrs

All issues in python-attrs/cattrs

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.