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

Dataclass structuring fails with misleading error message. #521

Open
brettc opened this issue Mar 14, 2024 · 0 comments
Open

Dataclass structuring fails with misleading error message. #521

brettc opened this issue Mar 14, 2024 · 0 comments

Comments

@brettc
Copy link

brettc commented Mar 14, 2024

  • 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant