Code Monkey home page Code Monkey logo

pydantic_cant_use_config's Introduction

pydantic_cant_use_config

Investigate a problem with inheritance and Pydantic 2.7.0.

Problem

import dataclasses

from pydantic import BaseModel

@dataclasses.dataclass
class Parent:
    name: str

class Child(BaseModel):
    parent: Parent = Parent(name="unknown parent")
    name: str


if __name__ == "__main__":

    model_schema = Child.model_json_schema()
    print(model_schema)

When using Pydantic 2.7.0, the following error is raised (full stack trace in file):

pydantic.errors.PydanticUserError: Cannot use `config` when the type is a BaseModel, dataclass or TypedDict. These types can have their own config and setting the config via the `config` parameter to TypeAdapter will not override it, thus the `config` you passed to TypeAdapter becomes meaningless, which is probably not what you want.

When using Pydantic 2.6.4, the code works as expected:

(pydantic-cant-use-config-py3.11) C:\dev\pydantic_cant_use_config>python main.py
{'$defs': {'Parent': {'properties': {'name': {'title': 'Name', 'type': 'string'}}, 'required': ['name'], 'title': 'Parent', 'type': 'object'}}, 'properties': {'parent': {'allOf': [{'$ref': '#/$defs/Parent'}], 'default': {'name': 'unknown parent'}}, 'name': {'title': 'Name', 'type': 'string'}}, 'required': ['name'], 'title': 'Child', 'type': 'object'}

Setup

  • Windows 11
  • Poetry 1.8.2
  • Python 3.11.6
  • Pydantic 2.7.0 (fail) / 2.6.4 (works)

pydantic_cant_use_config's People

Contributors

josteinl avatar

Watchers

 avatar

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.