Giter VIP home page Giter VIP logo

Comments (3)

Fatal1ty avatar Fatal1ty commented on May 23, 2024

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:

  1. change sequence type to Optional[int] with default None 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()
  1. the same as above but instead of Optional[int] keep it as int 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()
  1. 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.

m3talstorm avatar m3talstorm commented on May 23, 2024

@Fatal1ty Thank you for your response, I will try out your proposed solutions.

from mashumaro.

Fatal1ty avatar Fatal1ty commented on May 23, 2024

@m3talstorm Feel free to reopen this issue if you have further questions

from mashumaro.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.