Skip to content

Commit 9ca683b

Browse files
committed
fix: refactor typing imports and event loop handling
Replaced usage of typing_extensions with standard typing imports for TypedDict, ParamSpec, Concatenate, and related types where supported. Updated asyncio event loop initialization in Client to avoid deprecation warnings on Python 3.11+. Removed unused PY_310 constant and related conditional logic in utils.py.
1 parent fc0b623 commit 9ca683b

33 files changed

+105
-42
lines changed

discord/_version.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import warnings
3131
from importlib.metadata import PackageNotFoundError, version
3232

33-
from typing_extensions import TypedDict
33+
from typing import TypedDict
3434

3535
__all__ = ("__version__", "VersionInfo", "version_info")
3636

@@ -155,3 +155,4 @@ def date(self) -> datetime.date | None:
155155
commit=raw_info["commit"],
156156
date=date_info,
157157
)
158+

discord/client.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,28 @@ def __init__(
246246
):
247247
# self.ws is set in the connect method
248248
self.ws: DiscordWebSocket = None # type: ignore
249-
self.loop: asyncio.AbstractEventLoop = (
250-
asyncio.get_event_loop() if loop is None else loop
251-
)
249+
# Prefer an explicitly provided loop. If none is provided, try to use
250+
# a running loop (when constructed inside async code) and otherwise
251+
# fall back to the event loop policy's get_event_loop implementation
252+
# to avoid the deprecation warning for asyncio.get_event_loop().
253+
if loop is not None:
254+
self.loop: asyncio.AbstractEventLoop = loop
255+
else:
256+
try:
257+
self.loop = asyncio.get_running_loop()
258+
except RuntimeError:
259+
# No running loop in this thread; explicitly create a new
260+
# event loop and set it as the current event loop for this
261+
# thread. This mirrors the previous behavior of
262+
# asyncio.get_event_loop() (which would create and set a loop)
263+
# but avoids emitting the deprecation warning on Python >=3.11.
264+
self.loop = asyncio.new_event_loop()
265+
try:
266+
asyncio.set_event_loop(self.loop)
267+
except Exception:
268+
# If for some reason setting the loop fails, continue
269+
# using the locally created loop without setting it.
270+
pass
252271
self._listeners: dict[str, list[tuple[asyncio.Future, Callable[..., bool]]]] = (
253272
{}
254273
)

discord/commands/context.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
from discord.webhook.async_ import Webhook
3333

3434
if TYPE_CHECKING:
35-
from typing import Awaitable, Callable
36-
37-
from typing_extensions import ParamSpec
35+
from typing import Awaitable, Callable, ParamSpec
3836

3937
import discord
4038

@@ -439,3 +437,4 @@ def command(self) -> ApplicationCommand | None:
439437
@command.setter
440438
def command(self, value: ApplicationCommand | None) -> None:
441439
self.interaction.command = value
440+

discord/commands/core.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@
7272
from .context import ApplicationContext, AutocompleteContext
7373
from .options import Option, OptionChoice
7474

75-
if sys.version_info >= (3, 11):
76-
from typing import Annotated, Literal, get_args, get_origin
77-
else:
78-
from typing_extensions import Annotated, Literal, get_args, get_origin
75+
from typing import Annotated, Literal, get_args, get_origin
7976

8077
__all__ = (
8178
"_BaseCommand",
@@ -93,7 +90,7 @@
9390
)
9491

9592
if TYPE_CHECKING:
96-
from typing_extensions import Concatenate, ParamSpec
93+
from typing import Concatenate, ParamSpec
9794

9895
from .. import Permissions
9996
from ..cog import Cog
@@ -2159,3 +2156,4 @@ def validate_chat_input_description(description: Any, locale: str | None = None)
21592156
if locale:
21602157
error.args = (f"{error.args[0]} in locale {locale}",)
21612158
raise error
2159+

discord/commands/options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,3 +554,4 @@ def decorator(func):
554554
return func
555555

556556
return decorator
557+

discord/ext/commands/context.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from discord.message import Message
3535

3636
if TYPE_CHECKING:
37-
from typing_extensions import ParamSpec
37+
from typing import ParamSpec
3838

3939
from discord.abc import MessageableChannel
4040
from discord.guild import Guild
@@ -409,3 +409,4 @@ async def forward_to(
409409
self, channel: discord.abc.Messageable, **kwargs: Any
410410
) -> Message:
411411
return await self.message.forward_to(channel, **kwargs)
412+

discord/ext/commands/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
from .errors import *
6868

6969
if TYPE_CHECKING:
70-
from typing_extensions import Concatenate, ParamSpec, TypeGuard
70+
from typing import Concatenate, ParamSpec, TypeGuard
7171

7272
from discord.message import Message
7373

@@ -2501,3 +2501,4 @@ def decorator(func: Command | CoroFunc) -> Command | CoroFunc:
25012501
return func
25022502

25032503
return decorator # type: ignore
2504+

discord/types/activity.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
from typing import Literal
2929

30-
from typing_extensions import NotRequired, TypedDict
30+
from typing import TypedDict
31+
from typing_extensions import NotRequired
3132

3233
from .snowflake import Snowflake
3334
from .user import PartialUser
@@ -109,3 +110,4 @@ class Activity(_BaseActivity, total=False):
109110
session_id: str | None
110111
instance: bool
111112
buttons: list[str]
113+

discord/types/appinfo.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
from __future__ import annotations
2727

28-
from typing_extensions import NotRequired, TypedDict
28+
from typing import TypedDict
29+
from typing_extensions import NotRequired
2930

3031
from .snowflake import Snowflake
3132
from .team import Team
@@ -65,3 +66,4 @@ class PartialAppInfo(BaseAppInfo):
6566
class AppInstallParams(TypedDict):
6667
scopes: list[str]
6768
permissions: str
69+

discord/types/application_role_connection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626

2727
from typing import Literal
2828

29-
from typing_extensions import NotRequired, TypedDict
29+
from typing import TypedDict
30+
from typing_extensions import NotRequired
3031

3132
ApplicationRoleConnectionMetadataType = Literal[1, 2, 3, 4, 5, 6, 7, 8]
3233

@@ -38,3 +39,4 @@ class ApplicationRoleConnectionMetadata(TypedDict):
3839
name_localizations: NotRequired[dict[str, str]]
3940
description: str
4041
description_localizations: NotRequired[dict[str, str]]
42+

0 commit comments

Comments
 (0)