Repository navigation
Description
Partially a note to myself, if you're open to a PR fixing this.
cattr has solid support for generics in unstructure/structure operations, however it doesn't fully support nested generic types.
Consider instances where a generic attrs class contains a generic collection as a member...
import attr
from typing import Generic, TypeVar
T = TypeVar("T")
@attr.define
class Foo(Generic[T]):
bar: Dict[str, T]
cattr.structure({"bar" : {"bat": 1}}, Foo[int])...this fails, though one might expect cattr to propagate the T typevar into the Dict[str, T] generic member for an inferred Dict[str, int] field type.
This can probably be fixed by mildly extending the existing gen converter logic.
All the TypeVars in the current context are already resolved into mapping at:
https://github.com/Tinche/cattrs/blob/71a7ed2b4c72558af77487ae991f19cc5401c378/src/cattr/gen.py#L117-L126
which is then used to map TypeVars to their concrete type in the generated converter at:
https://github.com/Tinche/cattrs/blob/71a7ed2b4c72558af77487ae991f19cc5401c378/src/cattr/gen.py#L155-L161
Here, generic types could be detected via typing.get_args or the equivalent _compat function.
If the attribute type contains any TypeVar args, these could be replaced via mapping lookups and an updated type created with type.copy_with:
for a in attrs:
an = a.name
override = kwargs.pop(an, _neutral)
type = a.type
type_args = typing.get_args(type)
if isinstance(type, TypeVar):
type = getattr(mapping, type.__name__, type)
elif type_args is not None:
type_args = [
getattr(mapping, arg.__name__, arg)
for ta in type_args
]
type = type.copy_with(
tuple(
getattr(mapping, arg.__name__, arg)
for ta in type_args
)
)This will probably be fragile to updates in the typing module, so should probably be moved into _compat.
Activity
Tinche commentedon May 27, 2021
Yeah I'd definitely be open to merging in something that fixes this.
petergaultney commentedon Sep 1, 2021
this may be our last remaining blocker to using the GenConverter - for some reason it seems this was not an issue with the old converter? At least, using the old converter our code doesn't blow up and structures properly, but switching over, things now seem to be broken.
@asford it feels like you're close to a solution. Would you recommend the code above as a starting point, or do you think there's something missing there? I can probably dive in and fix this, but trying to decide how much context I really need to load into my head in order to do so.
Tinche commentedon Sep 1, 2021
I can also take a stab at this if I get some spare cycles
Tinche commentedon Sep 3, 2021
Tentative fix over at https://github.com/Tinche/cattrs/tree/tin/generic-structuring-fix. Feel free to try it out!
wakemaster39 commentedon Sep 27, 2021
I apparently should have looked for tickets before I went about fixing this.
I made a couple adjustments to your branch to solve the deeply nested problem, now it shouldn't matter how deep of a chain someone makes it should nest properly.
I also added a default NoneType handler. I can roll that out, but I found it was used in my generic code and figured it would be useful to have a default implementation.
chore(deps): update cattrs requirement from <1.10,>=1.8 to >=1.8,<1.1…
asford commentedon Jan 6, 2022
Is this fixed by #177 in the 1.8 release?
Tinche commentedon Jan 6, 2022
@asford Yes, it should be, could you give it a try?
asford commentedon Jan 6, 2022
I've verified this via the test inlined below. This failed in 1.7 but now succeeds in 1.10. Thanks!
chore(deps): Update cattrs requirement from <1.11,>=1.8 to >=1.8,<22.…