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

dataclass with methods cannot use init=False fields #4585

Closed
5 of 15 tasks
toslunar opened this issue Oct 4, 2022 · 3 comments · Fixed by #4878
Closed
5 of 15 tasks

dataclass with methods cannot use init=False fields #4585

toslunar opened this issue Oct 4, 2022 · 3 comments · Fixed by #4878
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@toslunar
Copy link

toslunar commented Oct 4, 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

Since pydantic 1.10.2, it fails to parse a builtin dataclass dataclass.field(init=False) if it has at least one method.
Similarly to #4498, the cause seems this change in #4484.

Example Code

import dataclasses
import pydantic

@dataclasses.dataclass
class Ok:
    a: int
    b: str = "b"
    c: float = dataclasses.field(init=False)

    def __post_init__(self):
        self.c = float(self.a)

@dataclasses.dataclass
class NotOkSince1_10_2:
    a: int
    b: str = "b"
    c: float = dataclasses.field(init=False)

    def __post_init__(self):
        self.c = float(self.a)

    @pydantic.validator("b")
    def check_b(cls, v):
        if not v:
            raise ValueError("b should not be empty")
        return v

    # ... or with a non-validator method
    # def foo(self):
    #     print("foo")


print(pydantic.parse_obj_as(Ok, {"a": 3}))  # Ok(a=3, b='b', c=3.0)
print(pydantic.parse_obj_as(NotOkSince1_10_2, {"a": 3}))  # TypeError: non-default argument 'c' follows default argument

Python, Pydantic & OS Version

pydantic version: 1.10.2
            pydantic compiled: True
                 install path: /Users/tos/.pyenv/versions/3.10.0/envs/p10/lib/python3.10/site-packages/pydantic
               python version: 3.10.0 (default, Oct 26 2021, 11:19:32) [Clang 11.0.0 (clang-1100.0.33.16)]
                     platform: macOS-11.7-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

Affected Components

@toslunar toslunar added bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable labels Oct 4, 2022
@samuelcolvin
Copy link
Member

@PrettyWood, any idea on this?

My instinct is that we have to call this a limitation of pydantic v1.10 and fix in v2.

@samuelcolvin samuelcolvin removed the unconfirmed Bug not yet confirmed as valid/applicable label Oct 5, 2022
@PrettyWood
Copy link
Member

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants