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

Fix type issues in options.py #1473

Merged
merged 16 commits into from
Jul 13, 2022
Merged
42 changes: 36 additions & 6 deletions discord/commands/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,40 @@
"""

import inspect
from typing import Any, Dict, List, Literal, Optional, Union
from typing import TYPE_CHECKING, Dict, List, Literal, Optional, Type, Union
from enum import Enum

from ..abc import GuildChannel
from ..abc import GuildChannel, Mentionable
from ..channel import TextChannel, VoiceChannel, StageChannel, CategoryChannel, Thread
from ..enums import ChannelType, SlashCommandOptionType, Enum as DiscordEnum

if TYPE_CHECKING:
Dorukyum marked this conversation as resolved.
Show resolved Hide resolved
from ..ext.commands import Converter
from ..user import User
from ..member import Member
from ..message import Attachment
from ..role import Role

InputType = Union[
Type[str],
Dorukyum marked this conversation as resolved.
Show resolved Hide resolved
Type[bool],
Type[int],
Type[float],
Type[GuildChannel],
Type[Thread],
Type["ThreadOption"],
Dorukyum marked this conversation as resolved.
Show resolved Hide resolved
Type[Member],
Type[User],
Type[Attachment],
Type[Role],
Type[Mentionable],
SlashCommandOptionType,
Converter,
Type[Converter],
Type[Enum],
Type[DiscordEnum],
]

__all__ = (
"ThreadOption",
"Option",
Expand Down Expand Up @@ -129,7 +156,7 @@ async def hello(
See `here <https://discord.com/developers/docs/reference#locales>`_ for a list of valid locales.
"""

def __init__(self, input_type: Any = str, /, description: Optional[str] = None, **kwargs) -> None:
def __init__(self, input_type: InputType = str, /, description: Optional[str] = None, **kwargs) -> None:
self.name: Optional[str] = kwargs.pop("name", None)
if self.name is not None:
self.name = str(self.name)
Expand Down Expand Up @@ -187,24 +214,27 @@ def __init__(self, input_type: Any = str, /, description: Optional[str] = None,

if description is not None:
self.description = description
elif issubclass(self._raw_type, Enum) and (doc := inspect.getdoc(self._raw_type)) is not None:
elif isinstance(self._raw_type, type) and issubclass(self._raw_type, Enum) and (doc := inspect.getdoc(self._raw_type)) is not None:
self.description = doc
else:
self.description = "No description provided"

if self.input_type == SlashCommandOptionType.integer:
minmax_types = (int, type(None))
minmax_typehint = Optional[int]
elif self.input_type == SlashCommandOptionType.number:
minmax_types = (int, float, type(None))
minmax_typehint = Optional[Union[int, float]]
else:
minmax_types = (type(None),)
minmax_typehint = Optional[Union[minmax_types]] # type: ignore
minmax_typehint = type(None)

if self.input_type == SlashCommandOptionType.string:
minmax_length_types = (int, type(None))
minmax_length_typehint = Optional[int]
else:
minmax_length_types = (type(None),)
minmax_length_typehint = Optional[Union[minmax_length_types]] # type: ignore
minmax_length_typehint = type(None)

self.min_value: minmax_typehint = kwargs.pop("min_value", None)
self.max_value: minmax_typehint = kwargs.pop("max_value", None)
Expand Down