Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vreuter opened this issue Nov 21, 2024 · 1 comment
Open

Comments

@vreuter
Copy link

vreuter commented Nov 21, 2024

  • 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.

@vreuter
Copy link
Author

vreuter commented Nov 21, 2024

Possibly related to #44 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant