Skip to content

can't structure dataclass with PEP563 postponed annotations or forward references #195

Closed

Description

@anthrotype
  • cattrs version: 1.8.0
  • Python version: 3.10.0
  • Operating System: macOS 12.0.1

Description

I would like to use cattrs with dataclasses (for this project I can't use attrs classes). I get the following errors when I try to structure a dataclass with circular, nested definitions that contain string literals to be post-evaluated (e.g. via typing.get_type_hints).
It seems that this currently only works with attrs classes, but not dataclasses.

What I Did

from __future__ import annotations
from typing import List
from dataclasses import dataclass, field
from cattr import Converter, GenConverter


@dataclass
class Node:
    children: List[Node] = field(default_factory=list)


tree = Node([Node([Node(), Node()]), Node()])

c = GenConverter()
data = c.unstructure(tree)
# Raises: NotAnAttrsClassError: <class '__main__.Node'> is not an attrs-decorated class.
c.structure(data, Node)


c = Converter()
data = c.unstructure(tree)
# Raises: StructureHandlerNotFoundError: Unsupported type: List[Node]. Register a structure hook for it.
c.structure(data, Node)

If I remove from __future__ import annotations and type the nested children: List["Node"] I get this other error:

StructureHandlerNotFoundError: Unsupported type: ForwardRef('Node'). Register a structure hook for it.

Activity

  1. anthrotype commented on Dec 7, 2021

    @anthrotype
    ContributorAuthor

    Note the same happens with the newly released cattrs 1.9.0

  2. Tinche commented on Dec 7, 2021

    @Tinche
    Member

    Hm, for attrs classes I use attr.resolve_types, and that doesn't exist for dataclasses. I can try using typing.get_type_hints directly, but that won't cover some of the use cases (like when you need to resolve them yourself, in a special way).

  3. anthrotype commented on Dec 7, 2021

    @anthrotype
    ContributorAuthor

    using typing.get_type_hints directly

    yeah, I suppose that's the way to go. You can have a resolve_types in _compat that delegates to attrs.resolve_types for attrs classes or does the same thing for dataclasses.

    won't cover some of the use cases (like when you need to resolve them yourself, in a special way)

    not sure what you mean by that

  4. Tinche commented on Dec 7, 2021

    @Tinche
    Member

    So for example cattrs doing resolve_types on a class can fail. That's why resolve_types takes some arguments so you can help it before you actually use the class.

    Also resolve_types is a stateful change - it updates the fields. get_type_hints just returns the annotations. It'll probably be enough to start though.

  5. Tinche commented on Dec 12, 2021

    @Tinche
    Member

    I just pushed support for this to the main branch, feel free to test it out.

  6. anthrotype commented on Dec 13, 2021

    @anthrotype
    ContributorAuthor

    Thanks, it works now!

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions