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

Nested dataclass definitions not working with pydantic #4877

Closed
5 of 15 tasks
and3rson opened this issue Dec 26, 2022 · 4 comments · Fixed by #4878
Closed
5 of 15 tasks

Nested dataclass definitions not working with pydantic #4877

and3rson opened this issue Dec 26, 2022 · 4 comments · Fixed by #4878
Labels
bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable

Comments

@and3rson
Copy link

and3rson commented Dec 26, 2022

Initial Checks

  • I have searched GitHub for a duplicate issue and I'm sure this is something new
  • I have searched Google & StackOverflow for a solution and couldn't find anything
  • I have read and followed the docs and still think this is a bug
  • I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)

Description

When defining dataclasses inside dataclasses, and then using those top-level dataclasses in pydantic models, I'm sometimes getting a TypeError: non-default argument '...' follows default argument.

Removing definitions of child dataclasses makes the code work properly.

Example Code

from typing import Optional, List
from dataclasses import dataclass, field

from pydantic import BaseModel


@dataclass
class Foo:
    a: Optional[int] = field(default=42)
    b: List = field(default_factory=list)

    @dataclass
    class Bar:
        pass


@dataclass
class Model1:
    foo: Foo
print(Model1(foo=Foo()))  # Model(foo=Foo(a=42, b=[]))


class Model2(BaseModel):
    foo: Foo
print(Model2(foo=Foo()))  # TypeError: non-default argument 'b' follows default argument
# Removing the definition of `Foo.Bar` makes this work properly.

Python, Pydantic & OS Version

pydantic version: 1.10.2
            pydantic compiled: True
                 install path: /home/anderson/src/***/.venv/lib/python3.9/site-packages/pydantic
               python version: 3.9.13 (main, Oct  5 2022, 17:32:54)  [GCC 12.2.0]
                     platform: Linux-6.0.9-arch1-1-x86_64-with-glibc2.36
     optional deps. installed: ['typing-extensions']

Affected Components

@and3rson and3rson added bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable labels Dec 26, 2022
@and3rson
Copy link
Author

Possibly related: #2424

@and3rson
Copy link
Author

Update: this is solved by using pydantic.dataclasses.dataclass instead. Still, the error is very confusing.

@PrettyWood
Copy link
Member

@and3rson This will be fixed in 1.10.3

@samuelcolvin
Copy link
Member

Closed via #4878

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants