Skip to content

cattr.structure doesn't support generic parameters. #149

Closed

Description

@asford

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

  1. Tinche commented on May 27, 2021

    @Tinche
    Member

    Yeah I'd definitely be open to merging in something that fixes this.

  2. petergaultney commented on Sep 1, 2021

    @petergaultney
    Contributor

    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.

  3. Tinche commented on Sep 1, 2021

    @Tinche
    Member

    I can also take a stab at this if I get some spare cycles

  4. wakemaster39 commented on Sep 27, 2021

    @wakemaster39

    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.

  5. asford commented on Jan 6, 2022

    @asford
    ContributorAuthor

    Is this fixed by #177 in the 1.8 release?

  6. Tinche commented on Jan 6, 2022

    @Tinche
    Member

    @asford Yes, it should be, could you give it a try?

  7. asford commented on Jan 6, 2022

    @asford
    ContributorAuthor

    I've verified this via the test inlined below. This failed in 1.7 but now succeeds in 1.10. Thanks!

    from typing import Generic, Dict, TypeVar
    
    import attr
    import cattr
    import pytest
    
    T = TypeVar("T")
    V = TypeVar("V")
    
    
    @attr.frozen
    class Col(Generic[T]):
        vals: Dict[str, T]
    
    
    @attr.frozen
    class NestCol(Generic[V]):
        v: Col[V]
    
    
    @attr.frozen
    class IntV:
        v: Col[int]
    
    
    def test_run():
        sv = {str(i): str(i) for i in range(1, 5)}
        iv = {str(i): i for i in range(1, 5)}
    
        tv = dict(vals={i: i for i in [1, "2", "3", 4]})
        tn = dict(v=tv)
    
        assert cattr.structure(tv, Col[str]).vals == sv
        assert cattr.structure(tv, Col[int]).vals == iv
    
        assert cattr.structure(tn, NestCol[str]).v.vals == sv
        assert cattr.structure(tn, NestCol[int]).v.vals == iv
    
        assert cattr.structure(tn, IntV).v.vals == iv
    
        # Missing TypeVar
        with pytest.raises(KeyError):
            cattr.structure(tn, NestCol).v.vals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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