Skip to content

Collections.abc types do not work like typing types #128

@Dr-ZeeD

Description

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

Description

Python 3.9 deprecated typing.<containers> in favor of using collections.abc.<containers>. Using collections.abc. containers does not work as expected, though.

What I Did

from collections import abc
import typing

from attr import define

from cattr import GenConverter


@define
class A:
    a: int


@define
class B:
    b: typing.MutableSequence[A]


@define
class C:
    c: abc.MutableSequence[A]


converter = GenConverter()


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

    b = B([a])
    print(converter.unstructure(b))

    c = C([a])
    print(converter.unstructure(c))

The actual output is:

{'b': [{'a': 1}]}
{'c': [A(a=1)]}

What I expected:

{'b': [{'a': 1}]}
{'c': [{'a': 1}]}

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