Skip to content

Unresolved generic types #661

Closed
Closed

Description

@AdrianSosic

Hi @Tinche, could it be that there is a line for type resolution missing somewhere in cattrs? I just noticed the situation shown below, which doesn't occur if I add an explicit resolve_types call (see commented code lines). If this is expected behavior, I'd appreciate if you could briefly comment on what's the issue. Thanks 🙃

from __future__ import annotations

from typing import Any, Generic, Protocol, TypeVar

import cattrs
from attrs import define, field

T = TypeVar("T")


class SomeProtocol(Protocol):
    pass


@define
class ImplementsProtocol(SomeProtocol, Generic[T]):
    template: T = field()


# --> Uncomment this and the examples works
# from attrs import resolve_types
# resolve_types(ImplementsProtocol)


@define
class ContainsProtocol:
    container: SomeProtocol = field()


converter = cattrs.Converter()


@converter.register_unstructure_hook
def _unstructure_container(obj: SomeProtocol):
    return converter.unstructure(obj, unstructure_as=ImplementsProtocol[int])


c = ContainsProtocol(ImplementsProtocol(42))
print(converter.unstructure(c))
TypeError: __main__.ImplementsProtocol[int] is not a module, class, method, or function.

Activity

  1. Tinche commented on Jun 26, 2025

    @Tinche
    Member

    Here's a minimized example:

    from __future__ import annotations
    
    from typing import Generic, TypeVar
    
    import cattrs
    from attrs import define
    
    T = TypeVar("T")
    
    
    @define
    class ImplementsProtocol(Generic[T]):
        template: T
    
    
    converter = cattrs.Converter()
    
    
    print(
        converter.unstructure(
            ImplementsProtocol(42), unstructure_as=ImplementsProtocol[int]
        )
    )
    

    Looks like a bug, most likely here: https://github.com/python-attrs/cattrs/blob/main/src/cattrs/converters.py#L1249 (line should be: resolve_types(origin or cl). I can get it in a day or two, or if you want to (the trickiest part will be adding a test :p)

  2. added this to the 25.2 milestone on Jun 26, 2025
  3. AdrianSosic commented on Jun 26, 2025

    @AdrianSosic
    ContributorAuthor

    Glad to hear it's a bug and not my wrong understanding 🐈 Re test: what do you have in mind? Why not just use your minimized example as test?

  4. Tinche commented on Jun 26, 2025

    @Tinche
    Member

    Ah actually you're right. The class should just be defined in a file that has from __future__ import annotations. This used to be more annoying when we still supported Python versions without this feature :p

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions