Skip to content

Commit 952bf1a

Browse files
committed
type fixes
1 parent c7e0dc5 commit 952bf1a

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

discord/embeds.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,16 @@ def __init__(
7979
@classmethod
8080
def from_dict(cls, data: dict[str, str | None]) -> EmbedAuthor:
8181
self = cls.__new__(cls)
82-
self.name = data.get("name")
82+
name = data.get("name")
83+
if not name:
84+
raise ValueError("name field is required")
85+
self.name = name
8386
self.url = data.get("url")
8487
self.icon_url = data.get("icon_url")
8588
self.proxy_icon_url = data.get("proxy_icon_url")
8689
return self
8790

88-
def to_dict(self) -> dict[str, str | None]:
91+
def to_dict(self) -> dict[str, str]:
8992
d = {"name": str(self.name)}
9093
if self.url:
9194
d["url"] = str(self.url)
@@ -128,7 +131,10 @@ def __init__(
128131
@classmethod
129132
def from_dict(cls, data: dict[str, str | None]) -> EmbedFooter:
130133
self = cls.__new__(cls)
131-
self.text = data.get("text")
134+
text = data.get("text")
135+
if not text:
136+
raise ValueError("text field is required")
137+
self.text = text
132138
self.icon_url = data.get("icon_url")
133139
self.proxy_icon_url = data.get("proxy_icon_url")
134140
return self
@@ -171,12 +177,12 @@ class EmbedMedia: # Thumbnail, Image, Video
171177
width: int
172178

173179
@classmethod
174-
def from_dict(cls, data: dict[str, str | int | None]) -> EmbedMedia:
180+
def from_dict(cls, data: dict[str, str | int]) -> EmbedMedia:
175181
self = cls.__new__(cls)
176-
self.url = data.get("url")
177-
self.proxy_url = data.get("proxy_url")
178-
self.height = data.get("height")
179-
self.width = data.get("width")
182+
self.url = str(data.get("url"))
183+
self.proxy_url = str(data.get("proxy_url"))
184+
self.height = int(data["height"])
185+
self.width = int(data["width"])
180186
return self
181187

182188
def __repr__(self) -> str:
@@ -231,7 +237,7 @@ def __init__(self, name: str, value: str, inline: bool | None = False):
231237
self.inline = inline
232238

233239
@classmethod
234-
def from_dict(cls, data: dict[str,]) -> EmbedField:
240+
def from_dict(cls, data: dict[str, str | bool]) -> EmbedField:
235241
"""Converts a :class:`dict` to a :class:`EmbedField` provided it is in the
236242
format that Discord expects it to be in.
237243
@@ -246,15 +252,15 @@ def from_dict(cls, data: dict[str,]) -> EmbedField:
246252
data: :class:`dict`
247253
The dictionary to convert into an EmbedField object.
248254
"""
249-
self: E = cls.__new__(cls)
255+
self = cls.__new__(cls)
250256

251257
self.name = data["name"]
252258
self.value = data["value"]
253259
self.inline = data.get("inline", False)
254260

255261
return self
256262

257-
def to_dict(self) -> dict[str, str | bool]:
263+
def to_dict(self) -> dict[str, str | bool | None]:
258264
"""Converts this EmbedField object into a dict.
259265
260266
Returns
@@ -343,7 +349,7 @@ def __init__(
343349
type: EmbedType = "rich",
344350
url: Any | None = None,
345351
description: Any | None = None,
346-
timestamp: datetime.datetime = None,
352+
timestamp: datetime.datetime | None = None,
347353
fields: list[EmbedField] = [],
348354
author: EmbedAuthor | None = None,
349355
footer: EmbedFooter | None = None,
@@ -467,7 +473,11 @@ def copy(self: E) -> E:
467473
return self.__class__.from_dict(self.to_dict())
468474

469475
def __len__(self) -> int:
470-
total = len(self.title) + len(self.description)
476+
total = 0
477+
if self.title:
478+
total += len(self.title)
479+
if self.description:
480+
total += len(self.description)
471481
for field in getattr(self, "_fields", []):
472482
total += len(field.name) + len(field.value)
473483

@@ -614,7 +624,7 @@ def image(self) -> EmbedMedia | None:
614624
img = getattr(self, "_image", None)
615625
if not img:
616626
return None
617-
return EmbedMedia.from_dict(img) # type: ignore
627+
return EmbedMedia.from_dict(img)
618628

619629
def set_image(self: E, *, url: Any | None) -> E:
620630
"""Sets the image for the embed content.
@@ -674,7 +684,7 @@ def thumbnail(self) -> EmbedMedia | None:
674684
thumb = getattr(self, "_thumbnail", None)
675685
if not thumb:
676686
return None
677-
return EmbedMedia.from_dict(thumb) # type: ignore
687+
return EmbedMedia.from_dict(thumb)
678688

679689
def set_thumbnail(self: E, *, url: Any | None) -> E:
680690
"""Sets the thumbnail for the embed content.
@@ -733,7 +743,7 @@ def video(self) -> EmbedMedia | None:
733743
vid = getattr(self, "_video", None)
734744
if not vid:
735745
return None
736-
return EmbedMedia.from_dict(vid) # type: ignore
746+
return EmbedMedia.from_dict(vid)
737747

738748
@property
739749
def provider(self) -> EmbedProvider | None:
@@ -746,7 +756,7 @@ def provider(self) -> EmbedProvider | None:
746756
prov = getattr(self, "_provider", None)
747757
if not prov:
748758
return None
749-
return EmbedProvider.from_dict(prov) # type: ignore
759+
return EmbedProvider.from_dict(prov)
750760

751761
@property
752762
def author(self) -> EmbedAuthor | None:
@@ -759,7 +769,7 @@ def author(self) -> EmbedAuthor | None:
759769
auth = getattr(self, "_author", None)
760770
if not auth:
761771
return None
762-
return EmbedAuthor.from_dict(auth) # type: ignore
772+
return EmbedAuthor.from_dict(auth)
763773

764774
def set_author(
765775
self: E,

0 commit comments

Comments
 (0)