Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for more relaxed arg type fallback in some overloaded signatures #876

Merged
merged 3 commits into from
Jan 3, 2022
Merged
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 changes/876.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Relaxed typing of methods with union entry specific specialisations through overloads.
19 changes: 19 additions & 0 deletions hikari/api/interaction_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ def get_listener(
) -> typing.Optional[ListenerT[component_interactions.ComponentInteraction, _MessageResponseBuilderT]]:
...

@typing.overload
@abc.abstractmethod
def get_listener(
self, interaction_type: typing.Type[_InteractionT_co], /
) -> typing.Optional[ListenerT[_InteractionT_co, special_endpoints.InteractionResponseBuilder]]:
...

@abc.abstractmethod
def get_listener(
self, interaction_type: typing.Type[_InteractionT_co], /
Expand Down Expand Up @@ -188,6 +195,18 @@ def set_listener(
) -> None:
...

@typing.overload
@abc.abstractmethod
def set_listener(
self,
interaction_type: typing.Type[_InteractionT_co],
listener: typing.Optional[ListenerT[_InteractionT_co, special_endpoints.InteractionResponseBuilder]],
/,
*,
replace: bool = False,
) -> None:
...

@abc.abstractmethod
def set_listener(
self,
Expand Down
13 changes: 9 additions & 4 deletions hikari/api/special_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1542,17 +1542,22 @@ def add_button(
) -> InteractiveButtonBuilder[_T]:
...

@typing.overload
@abc.abstractmethod
def add_button(self: _T, style: typing.Literal[messages.ButtonStyle.LINK, 5], url: str, /) -> LinkButtonBuilder[_T]:
...

@typing.overload
@abc.abstractmethod
def add_button(
self: _T, style: typing.Union[typing.Literal[messages.ButtonStyle.LINK], typing.Literal[5]], url: str, /
) -> LinkButtonBuilder[_T]:
self: _T, style: typing.Union[int, messages.ButtonStyle], url_or_custom_id: str, /
) -> typing.Union[LinkButtonBuilder[_T], InteractiveButtonBuilder[_T]]:
...

@abc.abstractmethod
def add_button(
self: _T, style: typing.Union[int, messages.ButtonStyle], url_or_custom_id: str, /
) -> ButtonBuilder[_T]:
) -> typing.Union[LinkButtonBuilder[_T], InteractiveButtonBuilder[_T]]:
"""Add a button component to this action row builder.

Parameters
Expand All @@ -1568,7 +1573,7 @@ def add_button(

Returns
-------
ButtonBuilder[Self]
typing.Union[LinkButtonBuilder[Self], InteractiveButtonBuilder[Self]]
Button builder object.
`ButtonBuilder.add_to_container` should be called to finalise the
button.
Expand Down
19 changes: 19 additions & 0 deletions hikari/impl/interaction_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,12 @@ def get_listener(
]:
...

@typing.overload
def get_listener(
self, interaction_type: typing.Type[_InteractionT_co], /
) -> typing.Optional[interaction_server.ListenerT[_InteractionT_co, special_endpoints.InteractionResponseBuilder]]:
...

def get_listener(
self, interaction_type: typing.Type[_InteractionT_co], /
) -> typing.Optional[interaction_server.ListenerT[_InteractionT_co, special_endpoints.InteractionResponseBuilder]]:
Expand Down Expand Up @@ -532,6 +538,19 @@ def set_listener(
) -> None:
...

@typing.overload
def set_listener(
self,
interaction_type: typing.Type[_InteractionT_co],
listener: typing.Optional[
interaction_server.ListenerT[_InteractionT_co, special_endpoints.InteractionResponseBuilder]
],
/,
*,
replace: bool = False,
) -> None:
...

def set_listener(
self,
interaction_type: typing.Type[_InteractionT_co],
Expand Down
23 changes: 21 additions & 2 deletions hikari/impl/rest_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,16 @@ def get_listener(
]:
...

@typing.overload
def get_listener(
self, interaction_type: typing.Type[_InteractionT_co], /
) -> typing.Optional[interaction_server_.ListenerT[_InteractionT_co, special_endpoints.InteractionResponseBuilder]]:
...

def get_listener(
self, interaction_type: typing.Type[_InteractionT_co], /
) -> typing.Optional[interaction_server_.ListenerT[_InteractionT_co, special_endpoints.InteractionResponseBuilder]]:
return self._server.get_listener(interaction_type) # type: ignore[return-value, arg-type]
return self._server.get_listener(interaction_type)

@typing.overload
def set_listener(
Expand Down Expand Up @@ -618,6 +624,19 @@ def set_listener(
) -> None:
...

@typing.overload
def set_listener(
self,
interaction_type: typing.Type[_InteractionT_co],
listener: typing.Optional[
interaction_server_.ListenerT[_InteractionT_co, special_endpoints.InteractionResponseBuilder]
],
/,
*,
replace: bool = False,
) -> None:
...

def set_listener(
self,
interaction_type: typing.Type[_InteractionT_co],
Expand All @@ -628,4 +647,4 @@ def set_listener(
*,
replace: bool = False,
) -> None:
self._server.set_listener(interaction_type, listener, replace=replace) # type: ignore[arg-type]
self._server.set_listener(interaction_type, listener, replace=replace)
16 changes: 14 additions & 2 deletions hikari/impl/special_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,15 +1261,27 @@ def add_button(
@typing.overload
def add_button(
self: _ActionRowBuilderT,
style: typing.Union[typing.Literal[messages.ButtonStyle.LINK], typing.Literal[5]],
style: typing.Literal[messages.ButtonStyle.LINK, 5],
FasterSpeeding marked this conversation as resolved.
Show resolved Hide resolved
url: str,
/,
) -> special_endpoints.LinkButtonBuilder[_ActionRowBuilderT]:
...

@typing.overload
def add_button(
self: _ActionRowBuilderT, style: typing.Union[int, messages.ButtonStyle], url_or_custom_id: str, /
) -> typing.Union[
special_endpoints.LinkButtonBuilder[_ActionRowBuilderT],
special_endpoints.InteractiveButtonBuilder[_ActionRowBuilderT],
]:
...

def add_button(
self: _ActionRowBuilderT, style: typing.Union[int, messages.ButtonStyle], url_or_custom_id: str, /
) -> special_endpoints.ButtonBuilder[_ActionRowBuilderT]:
) -> typing.Union[
special_endpoints.LinkButtonBuilder[_ActionRowBuilderT],
special_endpoints.InteractiveButtonBuilder[_ActionRowBuilderT],
]:
self._assert_can_add_type(messages.ComponentType.BUTTON)
if style in messages.InteractiveButtonTypes:
return InteractiveButtonBuilder(container=self, style=style, custom_id=url_or_custom_id)
Expand Down