Skip to content

Regression in v1.9.0: dataclass as a field type on itself #3675

Closed
@veikman

Description

@veikman

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.0
            pydantic compiled: True
                 install path: /home/veikman/.local/lib/python3.9/site-packages/pydantic
               python version: 3.9.7 (default, Sep 10 2021, 14:59:43)  [GCC 11.2.0]
                     platform: Linux-5.11.0-38-generic-x86_64-with-glibc2.34
     optional deps. installed: ['typing-extensions']

Minimal reproducing script:

from __future__ import annotations

from typing import List

from pydantic.dataclasses import dataclass

@dataclass
class Node:
    children: List[Node]

Node.__pydantic_model__.update_forward_refs()

Node(children=[Node(children=[])])  # Case 1.
Node(children=[dict(children=[])])  # Case 2.

Case 1 in the sample script works as expected with Pydantic v1.8.2. In Pydantic 1.9.0, that upgrade being the only change to the environment, it produces this validation error:

children -> 0
  value is not a valid dict (type=type_error.dict)

Case 2 works with both versions of Pydantic, indicating that the validation error message as such is correct.

I conclude that Pydantic v1.9.0 breaks support for Pydantic dataclasses as field data types on themselves (recursive nesting), for the case of input in the form of dataclass instances (case 1), which was a useful feature for unit testing. I have found no announcement in the change log that this is intentional.

Activity

samuelcolvin

samuelcolvin commented on Jan 20, 2022

@samuelcolvin
Member

Thanks for reporting, yes I can confirm the change.

I'll to investigate soon. @PrettyWood do you have any idea why this has changed?

PrettyWood

PrettyWood commented on Jan 20, 2022

@PrettyWood
Collaborator

I just run git bisect, seems introduced in #2588. I'll try to dig after dinner
(probably due to https://github.com/samuelcolvin/pydantic/blob/master/pydantic/typing.py#L424)

EDIT: I'll check this weekend if I can. @uriyyo do you have time to have a look on this? You'll probably be way faster and more accurate to find the right fix ;)

uriyyo

uriyyo commented on Jan 21, 2022

@uriyyo
Contributor

@PrettyWood I will take a closer look today, hope will figure out how to fix this one.

added this to the v1.9.1 milestone on Apr 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @samuelcolvin@veikman@PrettyWood@uriyyo

      Issue actions

        Regression in v1.9.0: dataclass as a field type on itself · Issue #3675 · pydantic/pydantic