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

pydantic.dataclasses.dataclass does not inherit dataclasses.Field to the new dataclass object. #4353

Closed
5 of 16 tasks
cercide opened this issue Aug 8, 2022 · 1 comment
Closed
5 of 16 tasks
Labels
bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable

Comments

@cercide
Copy link

cercide commented Aug 8, 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

pydantic.dataclasses.dataclass does not inherit dataclasses.Field to the new dataclass.

The function pydantic.dataclasses._process_class. claims to create dynamically a new dataclass with the exact same fields as the base dataclass. However, dataclasses.dataclass expects any dataclass.Field declared as an attribute (see also dataclasses._get_field). Nevertheless, pydantic.dataclasses._process_class does not forward any field attribute to the new dataclass (line 144-154). Consequently, the new dataclass looses any field information on its attributes.

Related

#2384
#2382

Affected Python Versions

This bug affects Python 3.9.1. Other python versions remain untested. However, it is very likely that other python versions are affected too.

Solution

Forward the original fields to the new dataclass. The example below solves the issue.
pydantic/dataclasses.py line 144-156:

        _cls = type(
            # for pretty output new class will have the name as original
            _cls.__name__,
            (_cls,),
            {
                '__annotations__': resolve_annotations(_cls.__annotations__, _cls.__module__),
                '__post_init__': _pydantic_post_init,
                # attrs for pickle to find this class
                '__module__': __name__,
                '__qualname__': uniq_class_name,
                # BUGFIX: forward original fields to the new dataclass
                **_cls.__dataclass_fields__
            },
        )

Example Code

import pydantic.dataclasses
import dataclasses

@dataclasses.dataclass
class A:
    test: int = dataclasses.field(metadata={"key": "value"})

@dataclasses.dataclass
class B(A):
    pass

@pydantic.dataclasses.dataclass
class C(A):
    pass
 

test_field_A = A.__dataclass_fields__["test"]
test_field_B = B.__dataclass_fields__["test"]
test_field_C = C.__dataclass_fields__["test"]

assert test_field_A == test_field_B, "field test on class A and B do not match"
assert test_field_A == test_field_C, "field test on class A and C do not match"

Python, Pydantic & OS Version

             pydantic version: 1.9.1
            pydantic compiled: True
               python version: 3.9.2 (default, Feb 28 2021, 17:03:44)  [GCC 10.2.1 20210110]
     optional deps. installed: ['typing-extensions']

Affected Components

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

cercide commented Aug 9, 2022

commit 576e4a3 (#2557) solves the issue

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

No branches or pull requests

1 participant