-
-
Notifications
You must be signed in to change notification settings - Fork 133
Closed
Description
- cattrs version: 1.3.0
- Python version: 3.9.1
- Operating System: macOS
Description
PEP 593 introduced Annotated in typing.
According to this PEP
If a library (or tool) encounters a typehint Annotated[T, x] and has no special logic for metadata x, it should ignore it and simply treat the type as T.
Thus, I expect cattrs to work without any issues when I useAnnotated. However, that is not the case. In the following, I provide a minimal working example outlining what should happen and what actually happens.
What I Did
from typing import Annotated
from attr import define
from cattr import GenConverter
converter = GenConverter()
@define
class A:
a: int
@define
class B:
b: A
@define
class C:
c: Annotated[A, "test"]
if __name__ == "__main__":
a = A(a=0)
# Test it without an annotation
b = B(b=a)
b_unstructured = converter.unstructure(b)
b_expected = {"b": {"a": 0}}
print(b_unstructured)
print(b_expected)
# Test it with an annotation
c = C(c=a)
c_unstructured = converter.unstructure(c)
c_expected = {"c": {"a": 0}}
print(c_unstructured) # {'c': A(a=0)}
print(c_expected) # 'c': {'a': 0}}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels