Skip to content

Commit

Permalink
type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
OmLanke committed May 10, 2023
1 parent c7e0dc5 commit 952bf1a
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions discord/embeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,16 @@ def __init__(
@classmethod
def from_dict(cls, data: dict[str, str | None]) -> EmbedAuthor:
self = cls.__new__(cls)
self.name = data.get("name")
name = data.get("name")
if not name:
raise ValueError("name field is required")
self.name = name
self.url = data.get("url")
self.icon_url = data.get("icon_url")
self.proxy_icon_url = data.get("proxy_icon_url")
return self

Check warning on line 89 in discord/embeds.py

View check run for this annotation

Codecov / codecov/patch

discord/embeds.py#L81-L89

Added lines #L81 - L89 were not covered by tests

def to_dict(self) -> dict[str, str | None]:
def to_dict(self) -> dict[str, str]:
d = {"name": str(self.name)}
if self.url:
d["url"] = str(self.url)
Expand Down Expand Up @@ -128,7 +131,10 @@ def __init__(
@classmethod
def from_dict(cls, data: dict[str, str | None]) -> EmbedFooter:
self = cls.__new__(cls)
self.text = data.get("text")
text = data.get("text")
if not text:
raise ValueError("text field is required")
self.text = text
self.icon_url = data.get("icon_url")
self.proxy_icon_url = data.get("proxy_icon_url")
return self

Check warning on line 140 in discord/embeds.py

View check run for this annotation

Codecov / codecov/patch

discord/embeds.py#L133-L140

Added lines #L133 - L140 were not covered by tests
Expand Down Expand Up @@ -171,12 +177,12 @@ class EmbedMedia: # Thumbnail, Image, Video
width: int

@classmethod
def from_dict(cls, data: dict[str, str | int | None]) -> EmbedMedia:
def from_dict(cls, data: dict[str, str | int]) -> EmbedMedia:
self = cls.__new__(cls)
self.url = data.get("url")
self.proxy_url = data.get("proxy_url")
self.height = data.get("height")
self.width = data.get("width")
self.url = str(data.get("url"))
self.proxy_url = str(data.get("proxy_url"))
self.height = int(data["height"])
self.width = int(data["width"])
return self

Check warning on line 186 in discord/embeds.py

View check run for this annotation

Codecov / codecov/patch

discord/embeds.py#L181-L186

Added lines #L181 - L186 were not covered by tests

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

@classmethod
def from_dict(cls, data: dict[str,]) -> EmbedField:
def from_dict(cls, data: dict[str, str | bool]) -> EmbedField:
"""Converts a :class:`dict` to a :class:`EmbedField` provided it is in the
format that Discord expects it to be in.
Expand All @@ -246,15 +252,15 @@ def from_dict(cls, data: dict[str,]) -> EmbedField:
data: :class:`dict`
The dictionary to convert into an EmbedField object.
"""
self: E = cls.__new__(cls)
self = cls.__new__(cls)

Check warning on line 255 in discord/embeds.py

View check run for this annotation

Codecov / codecov/patch

discord/embeds.py#L255

Added line #L255 was not covered by tests

self.name = data["name"]
self.value = data["value"]
self.inline = data.get("inline", False)

return self

def to_dict(self) -> dict[str, str | bool]:
def to_dict(self) -> dict[str, str | bool | None]:
"""Converts this EmbedField object into a dict.
Returns
Expand Down Expand Up @@ -343,7 +349,7 @@ def __init__(
type: EmbedType = "rich",
url: Any | None = None,
description: Any | None = None,
timestamp: datetime.datetime = None,
timestamp: datetime.datetime | None = None,
fields: list[EmbedField] = [],
author: EmbedAuthor | None = None,
footer: EmbedFooter | None = None,
Expand Down Expand Up @@ -467,7 +473,11 @@ def copy(self: E) -> E:
return self.__class__.from_dict(self.to_dict())

def __len__(self) -> int:
total = len(self.title) + len(self.description)
total = 0
if self.title:
total += len(self.title)
if self.description:
total += len(self.description)

Check warning on line 480 in discord/embeds.py

View check run for this annotation

Codecov / codecov/patch

discord/embeds.py#L476-L480

Added lines #L476 - L480 were not covered by tests
for field in getattr(self, "_fields", []):
total += len(field.name) + len(field.value)

Expand Down Expand Up @@ -614,7 +624,7 @@ def image(self) -> EmbedMedia | None:
img = getattr(self, "_image", None)
if not img:
return None
return EmbedMedia.from_dict(img) # type: ignore
return EmbedMedia.from_dict(img)

Check warning on line 627 in discord/embeds.py

View check run for this annotation

Codecov / codecov/patch

discord/embeds.py#L624-L627

Added lines #L624 - L627 were not covered by tests

def set_image(self: E, *, url: Any | None) -> E:
"""Sets the image for the embed content.
Expand Down Expand Up @@ -674,7 +684,7 @@ def thumbnail(self) -> EmbedMedia | None:
thumb = getattr(self, "_thumbnail", None)
if not thumb:
return None
return EmbedMedia.from_dict(thumb) # type: ignore
return EmbedMedia.from_dict(thumb)

Check warning on line 687 in discord/embeds.py

View check run for this annotation

Codecov / codecov/patch

discord/embeds.py#L684-L687

Added lines #L684 - L687 were not covered by tests

def set_thumbnail(self: E, *, url: Any | None) -> E:
"""Sets the thumbnail for the embed content.
Expand Down Expand Up @@ -733,7 +743,7 @@ def video(self) -> EmbedMedia | None:
vid = getattr(self, "_video", None)
if not vid:
return None
return EmbedMedia.from_dict(vid) # type: ignore
return EmbedMedia.from_dict(vid)

Check warning on line 746 in discord/embeds.py

View check run for this annotation

Codecov / codecov/patch

discord/embeds.py#L743-L746

Added lines #L743 - L746 were not covered by tests

@property
def provider(self) -> EmbedProvider | None:
Expand All @@ -746,7 +756,7 @@ def provider(self) -> EmbedProvider | None:
prov = getattr(self, "_provider", None)
if not prov:
return None
return EmbedProvider.from_dict(prov) # type: ignore
return EmbedProvider.from_dict(prov)

Check warning on line 759 in discord/embeds.py

View check run for this annotation

Codecov / codecov/patch

discord/embeds.py#L756-L759

Added lines #L756 - L759 were not covered by tests

@property
def author(self) -> EmbedAuthor | None:
Expand All @@ -759,7 +769,7 @@ def author(self) -> EmbedAuthor | None:
auth = getattr(self, "_author", None)
if not auth:
return None
return EmbedAuthor.from_dict(auth) # type: ignore
return EmbedAuthor.from_dict(auth)

Check warning on line 772 in discord/embeds.py

View check run for this annotation

Codecov / codecov/patch

discord/embeds.py#L769-L772

Added lines #L769 - L772 were not covered by tests

def set_author(
self: E,
Expand Down

0 comments on commit 952bf1a

Please sign in to comment.