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

misleading error message "Input should be a valid dictionary or instance" when using both pydantic dataclass and BaseModel #9383

Closed
1 task done
DetachHead opened this issue May 3, 2024 · 0 comments · Fixed by #9388
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@DetachHead
Copy link
Contributor

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

i'm converting a bunch of pydantic dataclasses to base models, and ran into this unhelpful error message that occurs when attempting to instantiate a class that both extends BaseModel and uses the pydantic @dataclass decorator:

pydantic_core._pydantic_core.ValidationError: 1 validation error for Foo
  Input should be a valid dictionary or instance of Foo [type=model_type, input_value=ArgsKwargs((), {'a': 1}), input_type=ArgsKwargs]
    For further information visit https://errors.pydantic.dev/2.7/v/model_type

the error message should instead say something like this, to explain that the the problem was not some sort of invalid input, but rather that the class itself incorrectly uses both BaseModel and @dataclass:

`Foo` cannot extend `BaseModel` and use the `@dataclass` decorator.

Example Code

from pydantic import BaseModel
from pydantic.dataclasses import dataclass


@dataclass
class Foo(BaseModel):
    a: int


Foo(a=1)

Python, Pydantic & OS Version

pydantic version: 2.7.1
        pydantic-core version: 2.18.2
          pydantic-core build: profile=release pgo=true
                 install path: C:\Users\user\project\.venv\Lib\site-packages\pydantic
               python version: 3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)]
                     platform: Windows-11-10.0.22621-SP0
             related packages: typing_extensions-4.11.0
                       commit: unknown
@DetachHead DetachHead added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels May 3, 2024
@DetachHead DetachHead changed the title "Input should be a valid dictionary or instance" error when using both pydantic dataclass and BaseModel misleading error message "Input should be a valid dictionary or instance" when using both pydantic dataclass and BaseModel May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant