cattrs
cattrs copied to clipboard
Unstructuring subclassed built-ins
- cattrs version: 22.1.0
- Python version: 3.10.5
- Operating System: Linux
Description
Hi, I wanted to have fancy code and decided to subclass built-in list. I run into problems when I tried to unstructure it using cattrs. It behaves differently. I am not sure if this is on purpose or a bug. Can you please help me, should I register some hooks for all such classes? Thank you.
What I Did
from attr import define
from cattrs import GenConverter
from typing import TypeVar
T = TypeVar("T")
@define
class Foo:
val: int
class mylist(list[T]):
pass
def main() -> None:
a = list[Foo]([Foo(val=1)])
b = mylist[Foo]([Foo(val=1)])
converter = GenConverter()
print(converter.unstructure(a)) # [{'val': 1}]
print(converter.unstructure(b)) # [Foo(val=1)]