Description
I realized I forgot to file an issue before submitting a PR, but this is to capture #51 .
The problem I have experienced is complex object with generic can be tedious to make work. Take the below code as an example, to use cattrs I need to register a hook for type ~T
which in this case would be assign it to int
, or I could register a hook on M
.
T = TypeVar("T")
@attrs(auto_attrib=True)
class M(Generic[T]):
prop: List[T]
data = {
"prop": [1,2,3]
}
result = structure(data, M)
If a TypeVar with the name T
is used anywhere else in my code it must be of type int or structuring is going to have issues.; This gets even worse when you have nested generics that share the same type var name. I typically just use T more my generic variable name, so I run into this problem often.
To address this, what I would like to see cattrs support is passing a _GenericAlias
into the structure function and automatically handle the structuring if possible. The code above would stay the same except the last line which would become
result = structure(data, M[str])
Given this support, I no longer need to manually register ~T
or M and the existing structuring mechanisms could be utilized to handle the structuring. The mentioned PR is my first pass at implementing this feature, looking to see if this i something the library is willing support before I continue the work in polishing the code, documentation and tests.