Comments (3)
Hi @m3talstorm
I guess there would be a performance hit on the first serialize, maybe add a lazy_omit_default config value?
Yes, there would be a performance hit. I do think that the new config option is not worth it in terms of cost and profit. Nevertheless, I'm sure there are workarounds or alternative approaches. The question is, do you really need to be able to pass an explicit sequence number to the constructor? Depending on the answer, there are some ways to avoid non-idempotent default_factory usage:
- change
sequence
type toOptional[int]
with defaultNone
and set it in post-init:
@dataclass(slots=True, frozen=False)
class Thing(DataClassJSONMixin, DataClassDictMixin):
class Config(BaseConfig):
omit_none = True
omit_default = True
sequence: Optional[int] = field(default=None)
SEQUENCE: ClassVar[int] = 0
def __post_init__(self):
if self.sequence is None:
self.sequence = sequence_incr()
- the same as above but instead of
Optional[int]
keep it asint
with default-1
(I suppose, sequence number must be positive):
@dataclass(slots=True, frozen=False)
class Thing(DataClassJSONMixin, DataClassDictMixin):
class Config(BaseConfig):
omit_none = True
omit_default = True
sequence: int = field(default=-1)
SEQUENCE: ClassVar[int] = 0
def __post_init__(self):
if self.sequence < 0:
self.sequence = sequence_incr()
- remove
sequence
from the constructor and set it in post-init instead:
@dataclass(slots=True, frozen=False)
class Thing(DataClassJSONMixin, DataClassDictMixin):
class Config(BaseConfig):
omit_none = True
omit_default = True
sequence: int = field(init=False)
SEQUENCE: ClassVar[int] = 0
def __post_init__(self):
self.sequence = sequence_incr()
from mashumaro.
@Fatal1ty Thank you for your response, I will try out your proposed solutions.
from mashumaro.
@m3talstorm Feel free to reopen this issue if you have further questions
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
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.