|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | from collections.abc import Callable |
| 4 | +from typing import Any, TypeVar, overload |
2 | 5 |
|
3 | | -from aiopynamodb.attributes import Attribute, UnicodeAttribute |
| 6 | +from aiopynamodb.attributes import _T, Attribute, UnicodeAttribute |
4 | 7 | from aiopynamodb.constants import STRING |
5 | 8 |
|
6 | 9 | from ._generics import UUID_ID |
7 | 10 |
|
| 11 | +_A = TypeVar("_A", bound="TransformingUnicodeAttribute") |
| 12 | + |
8 | 13 |
|
9 | 14 | class GUID(Attribute[UUID_ID]): |
10 | 15 | """ |
@@ -63,6 +68,22 @@ def __init__(self, transform: Callable[[str], str] | None = None, **kwargs): |
63 | 68 | super().__init__(**kwargs) |
64 | 69 | self.transform = transform |
65 | 70 |
|
| 71 | + @overload |
| 72 | + def __get__(self: _A, instance: None, owner: Any) -> _A: ... |
| 73 | + @overload |
| 74 | + def __get__(self: _A, instance: Any, owner: Any) -> _T: ... # type: ignore |
| 75 | + def __get__(self: _A, instance: Any, owner: Any) -> _A | _T: # type: ignore |
| 76 | + if instance: |
| 77 | + attr_name = instance._dynamo_to_python_attrs.get( |
| 78 | + self.attr_name, self.attr_name |
| 79 | + ) |
| 80 | + value = instance.attribute_values.get(attr_name, None) |
| 81 | + if getattr(self, "transform", None) and value is not None: |
| 82 | + return self.transform(value) # type: ignore |
| 83 | + return value |
| 84 | + else: |
| 85 | + return self |
| 86 | + |
66 | 87 | def serialize(self, value): |
67 | 88 | """Serialize a value for later storage in DynamoDB. |
68 | 89 |
|
|
0 commit comments