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

Cannot reuse a class with dataclass though works with BaseModel #4290

Closed
3 tasks done
tsuga opened this issue Jul 28, 2022 · 2 comments
Closed
3 tasks done

Cannot reuse a class with dataclass though works with BaseModel #4290

tsuga opened this issue Jul 28, 2022 · 2 comments
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@tsuga
Copy link

tsuga commented Jul 28, 2022

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.9.1
            pydantic compiled: True
                 install path: /usr/local/lib/python3.7/dist-packages/pydantic
               python version: 3.7.13 (default, Apr 24 2022, 01:04:09)  [GCC 7.5.0]
                     platform: Linux-5.4.188+-x86_64-with-Ubuntu-18.04-bionic
     optional deps. installed: ['typing-extensions']
import pydantic
from dataclasses import dataclass

@dataclass
class ClassA():
    foo: str
    bar: float

@dataclass
class ClassB():
    a1: ClassA
    a2: ClassA

pydantic_cls = pydantic.dataclasses.dataclass(ClassB)
schema = pydantic_cls.__pydantic_model__.schema()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
[<ipython-input-5-575c54138c2b>](https://localhost:8080/#) in <module>()
     14 
     15 pydantic_cls = pydantic.dataclasses.dataclass(ClassB)
---> 16 schema = pydantic_cls.__pydantic_model__.schema()

6 frames
/usr/local/lib/python3.7/dist-packages/pydantic/schema.cpython-37m-x86_64-linux-gnu.so in pydantic.schema.field_singleton_schema()

KeyError: <class 'pydantic.dataclasses.ClassA'>

However, the same structure with BaseModel works fine.

import pydantic
from pydantic import BaseModel

class ClassA(BaseModel):
    foo: str
    bar: float

class ClassB(BaseModel):
    a1: ClassA
    a2: ClassA

pydantic_cls = pydantic.dataclasses.dataclass(ClassB)
schema = pydantic_cls.__pydantic_model__.schema()  # no error :)
@tsuga tsuga added the bug V1 Bug related to Pydantic V1.X label Jul 28, 2022
@samuelcolvin
Copy link
Member

I wonder if this would be fixed by #2557?

I think honestly at this stage of pydantic V1, we might have to call this a limitation of pydantic.

@samuelcolvin
Copy link
Member

No answer, so I'll assume this is fixed by #2557.

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

No branches or pull requests

2 participants