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

Null reference error when pydantic dataclass wrapped class inherits from stdlib dataclass without adding new field #4565

Closed
5 of 15 tasks
joelliusczar opened this issue Sep 26, 2022 · 3 comments · Fixed by #4878
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@joelliusczar
Copy link

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

Hi all,

I am encountering an exception when I have a pydantic dataclass that inherits from a stdlib dataclass but has no additional fields. (I use the stdlib dataclass for internal usages and pydantic for validation.)
I don't know if my use case is dumb, regardless here is the error trace I encounter for the example code below.

Traceback (most recent call last):
  File "/Users/username/Documents/Code/py/pydantic_bug/pydantic_dc_bug.py", line 22, in <module>
    class ValidatedDerivedC(UnvalidatedDataclass):
  File "pydantic/dataclasses.py", line 200, in pydantic.dataclasses.dataclass.wrap
    # https://bugs.python.org/issue33453 for details.
AttributeError: 'NoneType' object has no attribute '__bases__'

This is not a showstopper for me.
I've discovered that using @pydantic.dataclasses.dataclass (no parentheses) instead of @pydantic.dataclasses.dataclass() avoids the error.
Adding a field to the derived class also avoids the error.

I am mostly adding this bug report for awareness.

If you need any additional help, I sorta understand why this is happening, but at the moment I'm not certain how to fix it without breaking other stuff.

Thank you for your work on this library. It is much appreciated!

Example Code

import dataclasses

import pydantic

@dataclasses.dataclass
class UnvalidatedDataclass:
    a: int = 0

#okay
@pydantic.dataclasses.dataclass
class ValidatedDerivedA(UnvalidatedDataclass):
    ...

#also okay
@pydantic.dataclasses.dataclass()
class ValidatedDerivedB(UnvalidatedDataclass):
    b: int = 0


#AttributeError: 'NoneType' object has no attribute '__bases__'
@pydantic.dataclasses.dataclass()
class ValidatedDerivedC(UnvalidatedDataclass):
    ...

Python, Pydantic & OS Version

pydantic version: 1.10.2
pydantic compiled: True
install path: /Users/username/Documents/Code/py/pydantic_bug/env/lib/python3.9/site-packages/pydantic
python version: 3.9.10 (main, Jan 15 2022, 11:40:36)  [Clang 13.0.0 (clang-1300.0.29.3)]
platform: macOS-12.3.1-arm64-arm-64bit
optional deps. installed: ['typing-extensions']

Affected Components

@joelliusczar joelliusczar added bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable labels Sep 26, 2022
@dolfinus
Copy link
Contributor

dolfinus commented Oct 1, 2022

Should be fixed in #4576

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

Confirmed, also confirmed this is new in v1.10.

@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