Skip to content

cattrs does not adhere to PEP 593 #127

@Dr-ZeeD

Description

@Dr-ZeeD
  • cattrs version: 1.3.0
  • Python version: 3.9.1
  • Operating System: macOS

Description

PEP 593 introduced Annotated in typing.
According to this PEP

If a library (or tool) encounters a typehint Annotated[T, x] and has no special logic for metadata x, it should ignore it and simply treat the type as T.
Thus, I expect cattrs to work without any issues when I use Annotated. However, that is not the case. In the following, I provide a minimal working example outlining what should happen and what actually happens.

What I Did

from typing import Annotated

from attr import define

from cattr import GenConverter

converter = GenConverter()


@define
class A:
    a: int


@define
class B:
    b: A


@define
class C:
    c: Annotated[A, "test"]


if __name__ == "__main__":
    a = A(a=0)

    # Test it without an annotation
    b = B(b=a)
    b_unstructured = converter.unstructure(b)
    b_expected = {"b": {"a": 0}}
    print(b_unstructured)
    print(b_expected)

    # Test it with an annotation
    c = C(c=a)
    c_unstructured = converter.unstructure(c)
    c_expected = {"c": {"a": 0}}
    print(c_unstructured)  # {'c': A(a=0)}
    print(c_expected)  # 'c': {'a': 0}}

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