Comments (8)
I did a few tests and it looks good.
from mashumaro.
Why don't use hooks? Сan you show an example in which the hooks wouldn't be enough?
from mashumaro.
Theoretically, I could add a flag in the field / datacalss config to specify whether such options should be passed to the hook calls somehow. This flag could be turned off by default just to maintain better performance in most cases when the options aren't needed.
from mashumaro.
The main thing we use this for is a flag to indicate whether the 'to_dict' output would skip attributes that are set to 'None'. I though that an 'options' dictionary would be more flexible in that we could implement other options. Another thing that we need in a couple of places is converting field names to and from names with hyphens and underscores. At the moment either an options dictionary or an 'omit_none' or 'keep_none' flag would handle our requirements.
from mashumaro.
Seems like omit_none
flag sounds reasonable. I'll try my best to add it soon.
from mashumaro.
@gshank I'm working on the configuration class that can change the code generation by adding omit_none
flag to to_dict
method signature. It's not the final version but you can test it: https://github.com/Fatal1ty/mashumaro/tree/config-class
from typing import Optional
from mashumaro import DataClassDictMixin
from mashumaro.config import TO_DICT_ADD_OMIT_NONE_FLAG
class BaseModel(DataClassDictMixin):
class Config:
code_generation_options = [
TO_DICT_ADD_OMIT_NONE_FLAG,
]
class Model(BaseModel):
a: Optional[str] = None
print(Model().to_dict(omit_none=True)) # {}
from mashumaro.
Thanks! I'll take a look. If you're interested I put the code that I was using into https://github.com/gshank/mashumaro/tree/callout_options. Your method might be better though.
from mashumaro.
These changes are in master branch already, so I close this issue.
from mashumaro.
Related Issues (20)
- 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
- Not parsing Generics correctly HOT 2
- Unserializable field in 3.12 if defined as a Generic TypeVar with mixin bounds HOT 6
- Allow propagation of class based discriminator settings to subclasses HOT 3
- Reject extra keys on deserialization HOT 7
- Investigate support for recursive Union types HOT 2
- Supports `numpy.ndarray` type for `orjson` HOT 2
- Add support for PEP 695
- Union type of [int | float] not serialized correctly HOT 2
- Add an alternative way to assign a field alias with annotations
- Add support for PEP 696
- Annotated SerializationStrategy used as a field serialization strategy leads to RecursionError
- Suppress warning about Union type
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.