Skip to content

Structure doesn't work with sub-classes #360

@ares-b

Description

@ares-b
  • cattrs version:
  • Python version:
  • Operating System:

Description

Cattrs cannot structure My Class if one of my attributes type is a Parent Type of other Child types.

What I Did

import attrs
import cattrs

@attrs.define
class A:
    pass

@attrs.define
class B(A):
    z: float = attrs.field(validator=attrs.validators.instance_of(float))

@attrs.define
class MyClass:
    test: A = attrs.field(validator=attrs.validators.instance_of(A))

mbis = cattrs.structure({"test":  {"z": 3.0}}, MyClass)
# mbis = MyClass(test=A())
# should be MyClass(test=B(z=3.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