Skip to content

Commit 5a8378c

Browse files
authored
fix: use input for timestamp if datetime (#845)
1 parent 4e92256 commit 5a8378c

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

interactions/api/models/attrs_utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from functools import wraps
2-
from typing import Dict, Mapping, Tuple
2+
from typing import Dict, Mapping, Optional, Tuple
33

44
import attrs
55

@@ -157,11 +157,14 @@ def inner_convert_dict(dict):
157157
return inner_convert_dict
158158

159159

160-
def convert_type(type_: type):
160+
def convert_type(type_: type, *, classmethod: Optional[str] = None):
161161
"""A helper function to convert an input to a specified type."""
162162

163163
def inner_convert_object(value):
164-
return value if isinstance(value, type_) else type_(value)
164+
if not classmethod:
165+
return value if isinstance(value, type_) else type_(value)
166+
else:
167+
return value if isinstance(value, type_) else getattr(type_, classmethod)(value)
165168

166169
return inner_convert_object
167170

interactions/api/models/attrs_utils.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def convert_list(converter: Callable[[_T], _P]) -> Callable[[List[_T]], List[_P]
5555
def convert_int(converter: Callable[[int], _T]) -> Callable[[Any], _T]:
5656
"""A helper function to pass an int to the converter, e.x. for Enums"""
5757

58-
def convert_type(object: Type[_T]) -> Callable[[Any], _T]:
58+
def convert_type(type_: Type[_T], *, classmethod: Optional[str] = None) -> Callable[[Any], _T]:
5959
"""A helper function to convert an input to a specified type."""
6060

6161
def convert_dict(

interactions/api/models/message.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
ClientSerializerMixin,
1111
DictSerializerMixin,
1212
convert_list,
13+
convert_type,
1314
define,
1415
field,
1516
)
@@ -454,7 +455,9 @@ class Embed(DictSerializerMixin):
454455
type: Optional[str] = field(default=None)
455456
description: Optional[str] = field(default=None)
456457
url: Optional[str] = field(default=None)
457-
timestamp: Optional[datetime] = field(converter=datetime.fromisoformat, default=None)
458+
timestamp: Optional[datetime] = field(
459+
converter=convert_type(datetime, classmethod="fromisoformat"), default=None
460+
)
458461
color: Optional[int] = field(default=None)
459462
footer: Optional[EmbedFooter] = field(converter=EmbedFooter, default=None)
460463
image: Optional[EmbedImageStruct] = field(converter=EmbedImageStruct, default=None)

0 commit comments

Comments
 (0)