Skip to content

Dataclass structuring fails with misleading error message. #521

Open
@brettc

Description

@brettc
  • cattrs version: 23.2.3
  • Python version: 3.11.8
  • Operating System: OSX

Description

I'm using cattrs to destructure and restructure a complex hierarchy of classes.
It is mostly working.
But I've boiled down one of the failures to this small example.

What I Did

from __future__ import annotations
from dataclasses import dataclass
from cattrs.preconf.json import make_converter

CONV = make_converter()

@dataclass(kw_only=True)
class A:
    a: str = ""

@dataclass(kw_only=True)
class B:
    b: str = ""

@dataclass(kw_only=True)
class C:
    c: int = 3
    a_or_b: AorB | None = None

AorB = A | B

def test_c_reconstruction():
    c = C(a_or_b=B())
    s = CONV.dumps(c)
    #  attr.exceptions.NotAnAttrsClassError: <class 'tests.test_cattrs_fail.A'> is not an attrs-decorated class.
    c2 = CONV.loads(s, C)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions