Comments (5)
Hi @tkukushkin
Iām currently on my vacation and will answer you next week. Before that you can read an example of using MultiDict with SerializationStrategy here https://github.com/Fatal1ty/mashumaro#third-party-generic-types
from mashumaro.
Hi! I've seen this example, but if I'm not mistaken, it is not about reading dataclass from multidict, but about reading some data to multidict.
Have a good vacation)
from mashumaro.
Ok, I'm here. The first thing I want to say is that different web frameworks have different multidict implementations for working with query string. The package multidict is not the only implementation. So, since there is no standard implementation that would dictate how dunder methods should work and what additional methods should exist, I'm not sure that mashumaro should have native support for multidict structures. However, you can make it work with multidict structures using a pre-deserialize hook. In the following example, I'll show you how to do this with multidict
package that has getall
method:
from dataclasses import dataclass
from typing import get_type_hints
import multidict
from mashumaro import DataClassDictMixin
from mashumaro.core.meta.helpers import get_type_origin
class DataClassMultiDictMixin(DataClassDictMixin):
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
list_fields = set()
for fname, ftype in get_type_hints(cls).items():
if issubclass(get_type_origin(ftype), list):
# you can check not only for lists but for other collection types
list_fields.add(fname)
cls.__list_fields__ = list_fields
@classmethod
def __pre_deserialize__(cls, d):
# d.copy() can be used if necessary
for name in cls.__list_fields__:
d[name] = d.getall(name)
return d
@dataclass
class Foo(DataClassMultiDictMixin):
x: float
y: list[int]
o = Foo.from_dict(multidict.MultiDict([("x", "1.2"), ("y", "2"), ("y", "3")]))
assert o == Foo(x=1.2, y=[2, 3])
The idea is simple. The string contains either a single value or a multiple value. Here we use the dataclass type hints to collect all keys that are supposed to have multiple values and get all the values in the hook. We could use this reflection at runtime in the hook but it's more performant to do it once on dataclass creation.
from mashumaro.
@tkukushkin can we close this issue now?
from mashumaro.
Iām closing this one. Feel free to reopen if you have further questions.
from mashumaro.
Related Issues (20)
- Switch to ruff from flake8
- Comprehension inlining on Python 3.12.0rc1 breaks some tests HOT 1
- Add support for Python 3.12
- Add support for PEP 696 HOT 1
- Can immutable dataclasses be hashed as dictionary keys? HOT 4
- Add an option to omit values equal to defaults on serialization
- Add an option to avoid collection data types copying
- Add support for LiteralString
- Generic dataclasses with Hashable typevar bound do not work HOT 3
- Benchmark is not working in master branch HOT 2
- omit_default=True evaluates default_factory value at declaration time which can cause a circular reference HOT 3
- Multiple aliases for field HOT 6
- "`dict[str, T]` as a field type is not supported" HOT 2
- InitVar with no default value HOT 6
- The types defined inside the function result in syntactically invalid generated code
- Allow for more complex logic for subclass Discrimnator field matching HOT 3
- TypedDicts not working with `from __future__ import annotations` HOT 2
- omit_default breaks IntFlag serialization HOT 1
- Using Union with int/float casts to whichever appears first HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mashumaro.