You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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__ importannotationsfromtypingimportAny, Generic, Protocol, TypeVarimportcattrsfromattrsimportdefine, fieldT=TypeVar("T")
classSomeProtocol(Protocol):
pass@defineclassImplementsProtocol(SomeProtocol, Generic[T]):
template: T=field()
# --> Uncomment this and the examples works# from attrs import resolve_types# resolve_types(ImplementsProtocol)@defineclassContainsProtocol:
container: SomeProtocol=field()
converter=cattrs.Converter()
@converter.register_unstructure_hookdef_unstructure_container(obj: SomeProtocol):
returnconverter.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.
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
Activity
Tinche commentedon Jun 26, 2025
Here's a minimized example:
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)AdrianSosic commentedon Jun 26, 2025
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?
Tinche commentedon Jun 26, 2025
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