lundi 3 février 2020

Is it bad practice to include non-validating methods in a pydantic model?

I'm using pydantic 1.3 to validate models for an API I am writing.

Is it common/good practice to include arbitrary methods in a class that inherits from pydantic.BaseModel?

I need some helper methods associated with the objects and I am trying to decide whether I need a "handler" class. These models are being converted to json and sent to a restful service that I am also writing.

My model looks like this:

class Foo(pydantic.BaseModel):
    name: str
    bar: int
    baz: int

Is it poor practice to do something like:

class Foo(pydantic.BaseModel):
    name: str
    bar: int
    baz: int

    add_one(self):
        self.bar += 1

It makes some sense to me, but I can't find an example of anyone doing this.

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire