Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Altering Pydantic behavior/configuration globally #12556

Open
@Viicos

Description

@Viicos

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 orTypeAdapter, 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp