Skip to content

Specific container type isn't respected, e.g. list can populate set #602

@vreuter

Description

@vreuter
  • cattrs version: 24.1.2
  • Python version:
>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=11, micro=9, releaselevel='final', serial=0)
  • Operating System: macOS Ventura

Description

Structure raw data into an instance of a class, in such a way that container type is respected.

What I Did

>>> @define
... class C:
...     a: int
...     b: set[str]
...
>>> instance = structure({'a': 1, 'b': ['x', 'y']}, C)
>>> instance
C(a=1, b={'x', 'y'})

What I Expect

I expect a failure, since the list[str] value for the b key doesn't conform to the set[str] type of the corresponding member of the class. This violates separation of concerns: the structure call is also deduplicating elements of the list, then, for the conversion to a set. As such, a reasonable invariant like "each collections in the raw data should have the same size as the corresponding collection once structured into the class" can be violated.

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