Skip to content

Unresolved generic types #661

Closed
Closed
@AdrianSosic

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions