Skip to content

Commit 526ef70

Browse files
committed
WIP: provide typing default for optional typevar
1 parent 2d72236 commit 526ef70

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

disnake/ui/button.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@
33
from __future__ import annotations
44

55
import os
6-
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Optional, Tuple, TypeVar, Union, overload
6+
from typing import (
7+
TYPE_CHECKING,
8+
Any,
9+
Callable,
10+
ClassVar,
11+
Optional,
12+
Tuple,
13+
TypeVar,
14+
Union,
15+
overload,
16+
)
717

818
from ..components import Button as ButtonComponent
919
from ..enums import ButtonStyle, ComponentType
@@ -17,18 +27,23 @@
1727
)
1828

1929
if TYPE_CHECKING:
20-
from typing_extensions import ParamSpec, Self
30+
from typing_extensions import (
31+
ParamSpec,
32+
Self,
33+
TypeVar, # noqa: TC004
34+
)
2135

2236
from ..emoji import Emoji
2337
from .item import ItemCallbackType
2438
from .view import View
2539

40+
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=None)
2641
else:
2742
ParamSpec = TypeVar
43+
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
2844

2945
B = TypeVar("B", bound="Button")
3046
B_co = TypeVar("B_co", bound="Button", covariant=True)
31-
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
3247
P = ParamSpec("P")
3348

3449

disnake/ui/item.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
"Item",
2626
)
2727

28-
I = TypeVar("I", bound="Item[Any]") # noqa: E741
29-
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
3028

3129
if TYPE_CHECKING:
32-
from typing_extensions import Self
30+
from typing_extensions import (
31+
Self,
32+
TypeVar, # noqa: TC004
33+
)
3334

3435
from ..client import Client
3536
from ..components import ActionRowChildComponent, Component
@@ -38,8 +39,18 @@
3839
from ..types.components import ActionRowChildComponent as ActionRowChildComponentPayload
3940
from .view import View
4041

42+
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=None)
43+
I = TypeVar("I", bound="Item[Any]", default="Item[None]") # noqa: E741
4144
ItemCallbackType = Callable[[V_co, I, MessageInteraction], Coroutine[Any, Any, Any]]
4245

46+
SelfViewT = TypeVar("SelfViewT", bound="Optional[View]", default=None)
47+
else:
48+
I = TypeVar("I", bound="Item[Any]") # noqa: E741
49+
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
50+
51+
SelfViewT = TypeVar("SelfViewT", bound="Optional[View]")
52+
53+
4354
ClientT = TypeVar("ClientT", bound="Client")
4455
UIComponentT = TypeVar("UIComponentT", bound="UIComponent")
4556

@@ -220,9 +231,6 @@ async def callback(self, interaction: MessageInteraction[ClientT], /) -> None:
220231
pass
221232

222233

223-
SelfViewT = TypeVar("SelfViewT", bound="Optional[View]")
224-
225-
226234
# While the decorators don't actually return a descriptor that matches this protocol,
227235
# this protocol ensures that type checkers don't complain about statements like `self.button.disabled = True`,
228236
# which work as `View.__init__` replaces the handler with the item.

disnake/ui/select/base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,24 @@
2828
__all__ = ("BaseSelect",)
2929

3030
if TYPE_CHECKING:
31-
from typing_extensions import ParamSpec, Self
31+
from typing_extensions import (
32+
ParamSpec,
33+
Self,
34+
TypeVar, # noqa: TC004
35+
)
3236

3337
from ...abc import Snowflake
3438
from ...interactions import MessageInteraction
3539
from ..item import ItemCallbackType
3640
from ..view import View
3741

42+
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=None)
3843
else:
3944
ParamSpec = TypeVar
45+
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
4046

4147

4248
S_co = TypeVar("S_co", bound="BaseSelect", covariant=True)
43-
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
4449
SelectMenuT = TypeVar("SelectMenuT", bound=AnySelectMenu)
4550
SelectValueT = TypeVar("SelectValueT")
4651
P = ParamSpec("P")

disnake/ui/view.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ async def __timeout_task_impl(self) -> None:
170170
await asyncio.sleep(self.__timeout_expiry - now)
171171

172172
def to_components(self) -> List[ActionRowPayload]:
173-
def key(item: Item[View]) -> int:
173+
def key(item: Item[Self]) -> int:
174174
return item._rendered_row or 0
175175

176176
children = sorted(self.children, key=key)

0 commit comments

Comments
 (0)