python-attrs / python-attrs/cattrs

How to deserialize multidict to data model?

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

Nobody has claimed this yet.

question
Dominant language
Python
Stars
1.1k
Forks
159
Avg merge
12h 21m
Merged PRs (30d)
6

Description

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')

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.

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.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
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.