Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.3k
Description
Initial Checks
- I have searched Google & GitHub for similar requests and couldn't find anything
- I have read and followedthe docs and still think this feature is missing
Description
Currently, Pydantic behavior can generally be controlled usingthe configuration. Pydantic is quite configurable: validation/serialization behavior, JSON Schema, behavior for specific types, etc. But this also means that we have to choose reasonable defaults for all these configuration values.
As an application/library author, you often want to set your own defaults, to be used in your entire code base. The canonical way to do so is todefine your ownBaseModel:
classMyBaseModel(BaseModel):model_config= {'validate_by_name':True}
However, there are limitations with this approach:
- Configuration isnot propagated for Pydantic model and dataclasses.
- If you happen to use dataclasses, TypedDicts or
TypeAdapter, you need specify this configuration every time. - For some settings, it makes sense to have it applied globally (e.g.
ignored_types).
It may be beneficial for end users to have a way to set the configuration globally. I believe this is particularly useful for applications (e.g. a FastAPI backend app). Unlike libraries, you "own" all of your code base. For instance, theDjango REST Framework libraries (and Django in general) usessettings to control the behavior globally, where you can provide your own defaults that can be overridden per viewset. In some way, Pydantic is no different when used with FastAPI or FastAPI-like framework.
However, there are a few design questions:
- Even if you are working on an application, you might be using a library making use of Pydantic internally. Changing the configuration globally will affect these libraries.
- We'll have to decide whether an explicit configuration set on some model can still be overridden globally.
Affected Components
- Compatibility between releases
- Data validation/parsing
- Data serialization -
.model_dump()and.model_dump_json() - JSON Schema
- Dataclasses
- Model Config
- Field Types - adding or changing a particular data type
- Function validation decorator
- Generic Models
- Other Model behaviour -
model_construct(), pickling, private attributes, ORM mode - Plugins and integration with other tools - mypy, FastAPI, python-devtools, Hypothesis, VS Code, PyCharm, etc.