Skip to content

Commit 6cc9318

Browse files
fix: fix incorrect types on MessagePinIterator & document MessagePin (#3178)
* fix: Fix incorrect types on MessagePinIterator * docs: Document MessagePin * fix: Auto-import issues * docs: CHANGELOG * docs: Fix deprecation entry and delay removal to 2.9 --------- Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>
1 parent 3269f81 commit 6cc9318

File tree

5 files changed

+55
-23
lines changed

5 files changed

+55
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ These changes are available on the `master` branch, but have not yet been releas
2727
([#3181](https://github.com/Pycord-Development/pycord/pull/3181))
2828
- Fixed internal use of deprecated \_PayloadLike dict operations.
2929
([#3189](https://github.com/Pycord-Development/pycord/pull/3189))
30+
- Fixed incorrect type hints for `MessagePinIterator`.
31+
([#3178](https://github.com/Pycord-Development/pycord/pull/3178))
3032

3133
### Deprecated
3234

discord/abc.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
Iterable,
3636
Protocol,
3737
Sequence,
38+
TypeAlias,
3839
TypeVar,
3940
Union,
4041
overload,
@@ -97,10 +98,15 @@
9798
from .ui.view import BaseView
9899
from .user import ClientUser
99100

100-
PartialMessageableChannel = Union[
101-
TextChannel, VoiceChannel, StageChannel, Thread, DMChannel, PartialMessageable
102-
]
103-
MessageableChannel = Union[PartialMessageableChannel, GroupChannel]
101+
PartialMessageableChannel: TypeAlias = (
102+
TextChannel
103+
| VoiceChannel
104+
| StageChannel
105+
| Thread
106+
| DMChannel
107+
| PartialMessageable
108+
)
109+
MessageableChannel: TypeAlias = PartialMessageableChannel | GroupChannel
104110
SnowflakeTime = Union["Snowflake", datetime]
105111

106112
from .voice import VoiceClient, VoiceProtocol

discord/iterators.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@
3939
Union,
4040
)
4141

42+
from typing_extensions import deprecated, override
43+
4244
from .audit_logs import AuditLogEntry
4345
from .errors import NoMoreItems
4446
from .object import Object
45-
from .utils import maybe_coroutine, snowflake_time, time_snowflake, warn_deprecated
47+
from .utils import maybe_coroutine, snowflake_time, time_snowflake
4648

4749
__all__ = (
4850
"ReactionIterator",
@@ -53,12 +55,13 @@
5355
"ScheduledEventSubscribersIterator",
5456
"EntitlementIterator",
5557
"SubscriptionIterator",
58+
"MessagePinIterator",
5659
)
5760

5861
if TYPE_CHECKING:
59-
from .abc import Snowflake
60-
from .channel import MessageableChannel
62+
from .abc import MessageableChannel, Snowflake
6163
from .guild import BanEntry, Guild
64+
from .http import HTTPClient
6265
from .member import Member
6366
from .message import Message, MessagePin
6467
from .monetization import Entitlement, Subscription
@@ -1210,9 +1213,13 @@ def __init__(
12101213
limit: int | None,
12111214
before: Snowflake | datetime.datetime | None = None,
12121215
):
1213-
self._channel = channel
1214-
self.limit = limit
1215-
self.http = channel._state.http
1216+
self._channel: MessageableChannel = channel
1217+
self.channel: MessageableChannel | Object | None = None
1218+
1219+
self.limit: int | None = limit
1220+
self.http: HTTPClient = (
1221+
channel._state.http # pyright: ignore[reportPrivateUsage]
1222+
)
12161223

12171224
self.before: str | None
12181225
if before is None:
@@ -1224,11 +1231,10 @@ def __init__(
12241231

12251232
self.update_before: Callable[[MessagePinPayload], str] = self.get_last_pinned
12261233

1227-
self.endpoint = self.http.pins_from
1228-
12291234
self.queue: asyncio.Queue[MessagePin] = asyncio.Queue()
12301235
self.has_more: bool = True
12311236

1237+
@override
12321238
async def next(self) -> MessagePin:
12331239
if self.queue.empty():
12341240
await self.fill_queue()
@@ -1246,12 +1252,16 @@ async def fill_queue(self) -> None:
12461252
if not self.has_more:
12471253
raise NoMoreItems()
12481254

1249-
if not hasattr(self, "channel"):
1250-
channel = await self._channel._get_channel()
1255+
if self.channel is None:
1256+
channel = (
1257+
await self._channel._get_channel() # pyright: ignore[reportPrivateUsage]
1258+
)
12511259
self.channel = channel
12521260

12531261
limit = 50 if self.limit is None else min(self.limit, 50)
1254-
data = await self.endpoint(self.channel.id, before=self.before, limit=limit)
1262+
data = await self.http.pins_from(
1263+
self.channel.id, before=self.before, limit=limit
1264+
)
12551265

12561266
pins: list[MessagePinPayload] = data.get("items", [])
12571267
for d in pins:
@@ -1269,17 +1279,25 @@ async def fill_queue(self) -> None:
12691279
def create_pin(self, data: MessagePinPayload) -> MessagePin:
12701280
from .message import MessagePin
12711281

1272-
return MessagePin(state=self.channel._state, channel=self.channel, data=data)
1282+
if self.channel is None:
1283+
raise RuntimeError("Channel is None, cannot create pin")
1284+
1285+
if isinstance(self.channel, Object):
1286+
raise RuntimeError("Cannot create pin for Object channel")
1287+
1288+
return MessagePin(
1289+
state=self.channel._state, # pyright: ignore[reportPrivateUsage]
1290+
channel=self.channel,
1291+
data=data,
1292+
)
12731293

12741294
async def retrieve_inner(self) -> list[Message]:
12751295
pins = await self.flatten()
12761296
return [p.message for p in pins]
12771297

1278-
def __await__(self) -> Generator[Any, Any, MessagePin]:
1279-
warn_deprecated(
1280-
f"Messageable.pins() returning a list of Message",
1281-
since="2.7",
1282-
removed="3.0",
1283-
reference="The documentation of pins()",
1284-
)
1298+
@deprecated(
1299+
"Messageable.pins() returning a list of Message is deprecated since version 2.7 and will be removed in 2.9."
1300+
+ " See the documentation of Messageable.pins() for more information."
1301+
)
1302+
def __await__(self) -> Generator[Any, Any, list[Message]]:
12851303
return self.retrieve_inner().__await__()

discord/message.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
__all__ = (
103103
"Attachment",
104104
"Message",
105+
"MessagePin",
105106
"PartialMessage",
106107
"MessageReference",
107108
"MessageCall",

docs/api/models.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ Messages
8989
.. autoclass:: Message()
9090
:members:
9191

92+
.. attributetable:: MessagePin
93+
94+
.. autoclass:: MessagePin()
95+
:members:
96+
9297
.. attributetable:: MessageSnapshot
9398

9499
.. autoclass:: MessageSnapshot()

0 commit comments

Comments
 (0)