-
-
Notifications
You must be signed in to change notification settings - Fork 482
fix(commands): Option not working with str / ForwardRef annotations #2919
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
base: master
Are you sure you want to change the base?
Changes from all commits
9fc0dbf
694de67
77ec0cf
47e31dd
0a07638
e5c4cbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
import sys | ||
import types | ||
from collections import OrderedDict | ||
from collections.abc import Iterator | ||
from enum import Enum | ||
from typing import ( | ||
TYPE_CHECKING, | ||
|
@@ -50,9 +51,7 @@ | |
from ..enums import ( | ||
IntegrationType, | ||
InteractionContextType, | ||
MessageType, | ||
SlashCommandOptionType, | ||
try_enum, | ||
) | ||
from ..errors import ( | ||
ApplicationCommandError, | ||
|
@@ -62,13 +61,20 @@ | |
InvalidArgument, | ||
ValidationError, | ||
) | ||
from ..member import Member | ||
from ..message import Attachment, Message | ||
from ..object import Object | ||
from ..role import Role | ||
from ..threads import Thread | ||
from ..user import User | ||
from ..utils import MISSING, async_all, find, maybe_coroutine, utcnow, warn_deprecated | ||
from ..utils import ( | ||
MISSING, | ||
async_all, | ||
find, | ||
maybe_coroutine, | ||
resolve_annotation, | ||
utcnow, | ||
warn_deprecated, | ||
) | ||
from .context import ApplicationContext, AutocompleteContext | ||
from .options import Option, OptionChoice | ||
|
||
|
@@ -314,7 +320,11 @@ def guild_only(self) -> bool: | |
"2.6", | ||
reference="https://discord.com/developers/docs/change-log#userinstallable-apps-preview", | ||
) | ||
return InteractionContextType.guild in self.contexts and len(self.contexts) == 1 | ||
return ( | ||
self.contexts is not None | ||
and InteractionContextType.guild in self.contexts | ||
and len(self.contexts) == 1 | ||
) | ||
|
||
@guild_only.setter | ||
def guild_only(self, value: bool) -> None: | ||
|
@@ -779,33 +789,41 @@ def _validate_parameters(self): | |
else: | ||
self.options = self._parse_options(params) | ||
|
||
def _check_required_params(self, params): | ||
params = iter(params.items()) | ||
def _check_required_params( | ||
self, params: OrderedDict[str, inspect.Parameter] | ||
) -> Iterator[tuple[str, inspect.Parameter]]: | ||
params_iter = iter(params.items()) | ||
required_params = ( | ||
["self", "context"] if self.attached_to_group or self.cog else ["context"] | ||
) | ||
for p in required_params: | ||
try: | ||
next(params) | ||
next(params_iter) | ||
except StopIteration: | ||
raise ClientException( | ||
f'Callback for {self.name} command is missing "{p}" parameter.' | ||
) | ||
|
||
return params | ||
return params_iter | ||
|
||
def _parse_options(self, params, *, check_params: bool = True) -> list[Option]: | ||
def _parse_options( | ||
self, params: OrderedDict[str, inspect.Parameter], *, check_params: bool = True | ||
) -> list[Option]: | ||
if check_params: | ||
params = self._check_required_params(params) | ||
params_iter = self._check_required_params(params) | ||
else: | ||
params = iter(params.items()) | ||
params_iter = iter(params.items()) | ||
|
||
final_options = [] | ||
for p_name, p_obj in params: | ||
cache = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The cache is created fresh for each call to Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
for p_name, p_obj in params_iter: | ||
option = p_obj.annotation | ||
if option == inspect.Parameter.empty: | ||
option = str | ||
|
||
if isinstance(option, str): | ||
option = resolve_annotation(option, globals(), locals(), cache) | ||
Soheab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
option = Option._strip_none_type(option) | ||
if self._is_typing_literal(option): | ||
literal_values = get_args(option) | ||
|
Uh oh!
There was an error while loading. Please reload this page.