Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create free standing functions to match model methods #5213

Open
samuelcolvin opened this issue Mar 17, 2023 · 7 comments
Open

create free standing functions to match model methods #5213

samuelcolvin opened this issue Mar 17, 2023 · 7 comments
Assignees
Labels
feature request feedback wanted serialization how pydantic serializes data, often related to `model_dump`, etc.

Comments

@samuelcolvin
Copy link
Member

  • a new module with all the methods - what the hell do we call this module?
  • call from BaseModel methods
  • call from Validator methods V2: The validate general method #4669
  • add BareModel
  • remove the model_ prefix from the free standing functions
  • extra functions we don't want to put on a model, like dump_json_bytes

Incomplete list of functions:

  • validate(type[T], data: any) -> T
  • validate_json(type[T], data: str | bytes) -> T
  • construct(type[T], data) -> T
  • dump -> any
  • dump_json -> str
  • dump_json_bytes -> bytes
  • json_schema -> dict
  • json_schema_json -> bytes

These should work on models, BareModel, dataclasses, and instrances of Validator (#4669).

Name ideas for this module: standalone, methods (technically they're the opposite), free???

@samuelcolvin samuelcolvin added feature request feedback wanted serialization how pydantic serializes data, often related to `model_dump`, etc. labels Mar 17, 2023
@realDragonium
Copy link

Perhaps we should splits it up in a multiple modules which can have more specific names? Like 'serialization' and 'validation'?

imo something like, would have more clarity for me

from .model import construct
from .serialization import dump, dump_json, validate
from .validation import validate, validate_json

Compared to something like this

from .util import construct, dump, dump_json_bytes, validate, validate_json

But if you're not looking to split it up, the common theme i notice is model/dataclass. Serialization, creation and validation of such thing... so instead of having model_ as prefix of the method, we can just call the module model?

@dmontagu
Copy link
Contributor

I will say — I don't think method/methods would be crazy if they really were just unbound "methods" (i.e., functions that always expect to receive an instance of a BaseModel subclass as the first argument).

I like model or methods less as the module name if they are meant to work equivalently on arbitrary values/types though, which I suspect they are? (E.g., dump_json([1, 2, MyModel(x=1)])).

I think dataclasses dumps its "free-standing" functions in the top-level namespace (e.g., dataclasses.asdict, dataclasses.fields, etc.) — we could just put the pydantic equivalents as top-level exports (i.e., pydantic.validate, pydantic.dump, etc.)

@samuelcolvin
Copy link
Member Author

I think methods makes sense, even if technically it's a misnomer.

I think all functions should either take a type or an instance of model or dataclass.

@realDragonium
Copy link

I think all functions should either take a type or an instance of model or dataclass.

My concern with naming it methods is: would it be obvious enough for people that only those types of functions should be placed in there?

@adriangb adriangb self-assigned this Mar 20, 2023
@samuelcolvin
Copy link
Member Author

would it be obvious enough for people

yes, because we would document that.

I think methods is satisfactory unless we can think of something better.

Need to also have:

  • fields_set(m): m.__fields_set__ could be a property, we should also add a @property to BaseModel - model_fields_set(self): return self.__fields_set__
  • rebuild() - akin to .model_rebuild()

@Kludex
Copy link
Member

Kludex commented Aug 28, 2023

Is this still wanted? @samuelcolvin

@samuelcolvin
Copy link
Member Author

I think there's a chance we might still want this, especially if we end up with FastModel which internally stores data in rust types — there would be an even stronger reason to use a pydantic (fast) model over a dataclass, and people would reasonably want a variant with an unpolluted namespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request feedback wanted serialization how pydantic serializes data, often related to `model_dump`, etc.
Projects
None yet
Development

No branches or pull requests

5 participants