python-attrs / python-attrs/cattrs

Dataclass structuring fails with misleading error message.

Open
#521 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.1k
Forks
159
Avg merge
12h 21m
Merged PRs (30d)
6

Description

  • 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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in the issue, especially make_converter(), CONV.dumps(), and CONV.loads(s, C) in tests.test_cattrs_fail. Confirm the misleading NotAnAttrsClassError with the dataclass union example; done when the failure reports the relevant dataclass or union problem accurately.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.