Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1441.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enable `reportInconsistentOverload` and `reportInvalidTypeVarUse` pyright rules.
4 changes: 2 additions & 2 deletions disnake/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ def __new__(
value_cls._actual_enum_cls_ = actual_cls = super().__new__(cls, name, bases, attrs)
return actual_cls

def __iter__(cls) -> Iterator[EnumMetaT]:
def __iter__(cls) -> Iterator[EnumMeta]:
return (cls._enum_member_map_[name] for name in cls._enum_member_names_)

def __reversed__(cls) -> Iterator[EnumMetaT]:
def __reversed__(cls) -> Iterator[EnumMeta]:
Comment on lines +170 to +173
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be EnumMetaT, before you rebased the TypeVar was incorrectly bound to type[EnumMeta]

return (cls._enum_member_map_[name] for name in reversed(cls._enum_member_names_))

def __len__(cls) -> int:
Expand Down
7 changes: 5 additions & 2 deletions disnake/ext/commands/bot_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import traceback
import warnings
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any, Callable, Optional, TypeVar, Union
from typing import TYPE_CHECKING, Any, Callable, Optional, TypeVar, Union, cast

import disnake
from disnake.utils import iscoroutinefunction
Expand All @@ -26,6 +26,7 @@
if TYPE_CHECKING:
from typing_extensions import Self

from disnake.ext.commands.bot import AutoShardedBot, Bot
from disnake.message import Message

from ._types import Check, CoroFunc, MaybeCoro
Expand Down Expand Up @@ -507,7 +508,9 @@ class be provided, it must be similar enough to :class:`.Context`\'s
``cls`` parameter.
"""
view = StringView(message.content)
ctx = cls(prefix=None, view=view, bot=self, message=message)
ctx = cls(
prefix=None, view=view, bot=cast("Union[Bot, AutoShardedBot]", self), message=message
)

if message.author.id == self.user.id: # pyright: ignore[reportAttributeAccessIssue]
return ctx
Expand Down
10 changes: 9 additions & 1 deletion disnake/ext/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@


T = TypeVar("T")
BotT = TypeVar("BotT", bound="Union[Bot, AutoShardedBot]")
CogT = TypeVar("CogT", bound="Cog")

if TYPE_CHECKING:
from typing_extensions import TypeVar # noqa: TC004

P = ParamSpec("P")
BotT = TypeVar(
"BotT",
bound="Union[Bot, AutoShardedBot]",
covariant=True,
default=Union[Bot, AutoShardedBot],
)
else:
P = TypeVar("P")
BotT = TypeVar("BotT", bound="Union[Bot, AutoShardedBot]")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no real need to specify anything but the TypeVar's name in the else branch of TYPE_CHECKING

Suggested change
BotT = TypeVar("BotT", bound="Union[Bot, AutoShardedBot]")
BotT = TypeVar("BotT")



class Context(disnake.abc.Messageable, Generic[BotT]):
Expand Down
15 changes: 10 additions & 5 deletions disnake/ext/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,30 @@

MISSING: Any = disnake.utils.MISSING

T = TypeVar("T")
VT = TypeVar("VT")
CogT = TypeVar("CogT", bound="Optional[Cog]")
CommandT = TypeVar("CommandT", bound="Command")
ContextT = TypeVar("ContextT", bound="Context")
GroupT = TypeVar("GroupT", bound="Group")
HookT = TypeVar("HookT", bound="Hook")
ErrorT = TypeVar("ErrorT", bound="Error")


if TYPE_CHECKING:
P = ParamSpec("P")
from typing_extensions import TypeVar # noqa: TC004

P = ParamSpec("P", default=...)
T = TypeVar("T", default=Any)

CogT = TypeVar("CogT", bound="Optional[Cog]", default="Optional[Cog]")
ContextT = TypeVar("ContextT", bound="Context", default="Context")
CommandCallback = Union[
Callable[Concatenate[CogT, ContextT, P], Coro[T]],
Callable[Concatenate[ContextT, P], Coro[T]],
]
else:
T = TypeVar("T")
P = TypeVar("P")
CogT = TypeVar("CogT", bound="Optional[Cog]")
ContextT = TypeVar("ContextT", bound="Context")
Comment on lines +122 to +123
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/DisnakeDev/disnake/pull/1441/files#r2443495659

Suggested change
CogT = TypeVar("CogT", bound="Optional[Cog]")
ContextT = TypeVar("ContextT", bound="Context")
CogT = TypeVar("CogT")
ContextT = TypeVar("ContextT")



def wrap_callback(coro: Callable[..., Coro[T]]) -> Callable[..., Coro[Optional[T]]]:
Expand Down Expand Up @@ -1418,7 +1423,7 @@ def copy(self: GroupT) -> GroupT:
"""
ret = super().copy()
for cmd in self.commands:
ret.add_command(cmd.copy())
ret.add_command(cast("Command[CogT, Any, Any]", cmd.copy()))
return ret

async def invoke(self, ctx: Context) -> None:
Expand Down
3 changes: 2 additions & 1 deletion disnake/ext/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,8 @@ def loop(

def loop(
cls: type[Object[L_co, Concatenate[LF, P]]] = Loop[Any],
**kwargs: Any,
*_: P.args,
**kwargs: P.kwargs,
) -> Callable[[LF], L_co]:
"""A decorator that schedules a task in the background for you with
optional reconnect logic. The decorator returns a :class:`Loop`.
Expand Down
12 changes: 6 additions & 6 deletions disnake/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,22 @@ class Localized(Generic[StringT]):
__slots__ = ("string", "localizations")

@overload
def __init__(self: Localized[StringT], string: StringT, *, key: str) -> None: ...
def __init__(self, string: StringT, *, key: str) -> None: ...

@overload
def __init__(self: Localized[Optional[str]], *, key: str) -> None: ...
def __init__(self, *, key: str) -> None: ...

@overload
def __init__(
self: Localized[StringT],
self,
string: StringT,
*,
data: Union[Optional[LocalizationsDict], LocalizationValue],
) -> None: ...

@overload
def __init__(
self: Localized[Optional[str]],
self,
*,
data: Union[Optional[LocalizationsDict], LocalizationValue],
) -> None: ...
Expand All @@ -108,12 +108,12 @@ def __init__(
# as it's only meant to be used internally
def __init__(
self,
string: StringT = None,
string: Optional[StringT] = None,
*,
key: str = MISSING,
data: Union[Optional[LocalizationsDict], LocalizationValue] = MISSING,
) -> None:
self.string: StringT = string
self.string: StringT = string # pyright: ignore[reportAttributeAccessIssue]

if not (key is MISSING) ^ (data is MISSING):
msg = "Exactly one of `key` or `data` must be provided"
Expand Down
9 changes: 7 additions & 2 deletions disnake/ui/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
from typing import TYPE_CHECKING, Any, Optional, TypeVar, Union

if TYPE_CHECKING:
from typing_extensions import TypeAlias
from typing_extensions import (
TypeAlias,
TypeVar, # noqa: TC004
)

from . import (
ActionRow,
Expand All @@ -24,7 +27,9 @@
from .select import ChannelSelect, MentionableSelect, RoleSelect, StringSelect, UserSelect
from .view import View

V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=Optional[View])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/DisnakeDev/disnake/pull/1441/files#r2443498907

Suggested change
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=Optional[View])
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=None)

else:
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/DisnakeDev/disnake/pull/1441/files#r2443495659

Suggested change
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
V_co = TypeVar("V_co")


AnySelect = Union[
"ChannelSelect[V_co]",
Expand Down
39 changes: 7 additions & 32 deletions disnake/ui/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@
)

if TYPE_CHECKING:
from typing_extensions import ParamSpec, Self
from typing_extensions import (
ParamSpec,
Self,
TypeVar, # noqa: TC004
)

from ..emoji import Emoji
from .item import ItemCallbackType
from .view import View

V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=Optional[View])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/DisnakeDev/disnake/pull/1441/files#r2443498907

Suggested change
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=Optional[View])
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=None)

else:
ParamSpec = TypeVar
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
V_co = TypeVar("V_co")


B = TypeVar("B", bound="Button")
B_co = TypeVar("B_co", bound="Button", covariant=True)
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
P = ParamSpec("P")


Expand Down Expand Up @@ -83,36 +88,6 @@ class Button(Item[V_co]):
# We have to set this to MISSING in order to overwrite the abstract property from UIComponent
_underlying: ButtonComponent = MISSING

@overload
def __init__(
self: Button[None],
*,
style: ButtonStyle = ButtonStyle.secondary,
label: Optional[str] = None,
disabled: bool = False,
custom_id: Optional[str] = None,
url: Optional[str] = None,
emoji: Optional[Union[str, Emoji, PartialEmoji]] = None,
sku_id: Optional[int] = None,
id: int = 0,
row: Optional[int] = None,
) -> None: ...

@overload
def __init__(
self: Button[V_co],
*,
style: ButtonStyle = ButtonStyle.secondary,
label: Optional[str] = None,
disabled: bool = False,
custom_id: Optional[str] = None,
url: Optional[str] = None,
emoji: Optional[Union[str, Emoji, PartialEmoji]] = None,
sku_id: Optional[int] = None,
id: int = 0,
row: Optional[int] = None,
) -> None: ...

def __init__(
self,
*,
Expand Down
26 changes: 14 additions & 12 deletions disnake/ui/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
"Item",
)

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

if TYPE_CHECKING:
from typing_extensions import Self
from typing_extensions import (
Self,
TypeVar, # noqa: TC004
)

from ..client import Client
from ..components import ActionRowChildComponent, Component
Expand All @@ -35,8 +36,18 @@
from ..types.components import ActionRowChildComponent as ActionRowChildComponentPayload
from .view import View

V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=Optional[View])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=Optional[View])
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=None)

I = TypeVar("I", bound="Item[Any]", default="Item[Any]") # noqa: E741
ItemCallbackType = Callable[[V_co, I, MessageInteraction], Coroutine[Any, Any, Any]]

SelfViewT = TypeVar("SelfViewT", bound="Optional[View]", default=Optional[View])
else:
I = TypeVar("I", bound="Item[Any]") # noqa: E741
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)

SelfViewT = TypeVar("SelfViewT", bound="Optional[View]")
Comment on lines +45 to +48
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
I = TypeVar("I", bound="Item[Any]") # noqa: E741
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
SelfViewT = TypeVar("SelfViewT", bound="Optional[View]")
I = TypeVar("I") # noqa: E741
V_co = TypeVar("V_co")
SelfViewT = TypeVar("SelfViewT")



ClientT = TypeVar("ClientT", bound="Client")
UIComponentT = TypeVar("UIComponentT", bound="UIComponent")

Expand Down Expand Up @@ -159,12 +170,6 @@ class Item(WrappedComponent, Generic[V_co]):

__repr_attributes__: ClassVar[tuple[str, ...]] = ("row",)

@overload
def __init__(self: Item[None]) -> None: ...

@overload
def __init__(self: Item[V_co]) -> None: ...

def __init__(self) -> None:
self._view: V_co = None # pyright: ignore[reportAttributeAccessIssue]
self._row: Optional[int] = None
Expand Down Expand Up @@ -223,9 +228,6 @@ async def callback(self, interaction: MessageInteraction[ClientT], /) -> None:
pass


SelfViewT = TypeVar("SelfViewT", bound="Optional[View]")


# While the decorators don't actually return a descriptor that matches this protocol,
# this protocol ensures that type checkers don't complain about statements like `self.button.disabled = True`,
# which work as `View.__init__` replaces the handler with the item.
Expand Down
9 changes: 7 additions & 2 deletions disnake/ui/select/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,24 @@
__all__ = ("BaseSelect",)

if TYPE_CHECKING:
from typing_extensions import ParamSpec, Self
from typing_extensions import (
ParamSpec,
Self,
TypeVar, # noqa: TC004
)

from ...abc import Snowflake
from ...interactions import MessageInteraction
from ..item import ItemCallbackType
from ..view import View

V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=Optional[View])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current behavior is to default to None

Suggested change
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=Optional[View])
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True, default=None)

else:
ParamSpec = TypeVar
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/DisnakeDev/disnake/pull/1441/files#r2443495659

Suggested change
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
V_co = TypeVar("V_co")



S_co = TypeVar("S_co", bound="BaseSelect", covariant=True)
V_co = TypeVar("V_co", bound="Optional[View]", covariant=True)
SelectMenuT = TypeVar("SelectMenuT", bound=AnySelectMenu)
SelectValueT = TypeVar("SelectValueT")
P = ParamSpec("P")
Expand Down
32 changes: 0 additions & 32 deletions disnake/ui/select/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,38 +109,6 @@ class ChannelSelect(BaseSelect[ChannelSelectMenu, "AnyChannel", V_co]):
),
}

@overload
def __init__(
self: ChannelSelect[None],
*,
custom_id: str = ...,
placeholder: Optional[str] = None,
min_values: int = 1,
max_values: int = 1,
disabled: bool = False,
channel_types: Optional[list[ChannelType]] = None,
default_values: Optional[Sequence[SelectDefaultValueInputType[AnyChannel]]] = None,
required: bool = True,
id: int = 0,
row: Optional[int] = None,
) -> None: ...

@overload
def __init__(
self: ChannelSelect[V_co],
*,
custom_id: str = ...,
placeholder: Optional[str] = None,
min_values: int = 1,
max_values: int = 1,
disabled: bool = False,
channel_types: Optional[list[ChannelType]] = None,
default_values: Optional[Sequence[SelectDefaultValueInputType[AnyChannel]]] = None,
required: bool = True,
id: int = 0,
row: Optional[int] = None,
) -> None: ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing those should only be done once V_co TypeVar is changed to have a default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to try to convert as many of these as I can to have a default. Moving this to draft for now. Will return to this PR later.

def __init__(
self,
*,
Expand Down
Loading
Loading