mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-18 23:15:48 +00:00
[commands] Fix certain annotations being allowed in hybrid commands
Union types were not properly constrained and callable types were too eagerly being converted
This commit is contained in:
parent
405c445093
commit
832d2c0542
@ -109,6 +109,11 @@ def is_transformer(converter: Any) -> bool:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def required_pos_arguments(func: Callable[..., Any]) -> int:
|
||||||
|
sig = inspect.signature(func)
|
||||||
|
return sum(p.default is p.empty for p in sig.parameters.values())
|
||||||
|
|
||||||
|
|
||||||
def make_converter_transformer(converter: Any) -> Type[app_commands.Transformer]:
|
def make_converter_transformer(converter: Any) -> Type[app_commands.Transformer]:
|
||||||
async def transform(cls, interaction: discord.Interaction, value: str) -> Any:
|
async def transform(cls, interaction: discord.Interaction, value: str) -> Any:
|
||||||
try:
|
try:
|
||||||
@ -184,13 +189,19 @@ def replace_parameters(parameters: Dict[str, Parameter], signature: inspect.Sign
|
|||||||
param = param.replace(annotation=make_greedy_transformer(inner, parameter))
|
param = param.replace(annotation=make_greedy_transformer(inner, parameter))
|
||||||
elif is_converter(converter):
|
elif is_converter(converter):
|
||||||
param = param.replace(annotation=make_converter_transformer(converter))
|
param = param.replace(annotation=make_converter_transformer(converter))
|
||||||
elif origin is Union and len(args) == 2 and args[-1] is _NoneType:
|
elif origin is Union:
|
||||||
# Special case Optional[X] where X is a single type that can optionally be a converter
|
if len(args) == 2 and args[-1] is _NoneType:
|
||||||
inner = args[0]
|
# Special case Optional[X] where X is a single type that can optionally be a converter
|
||||||
is_inner_tranformer = is_transformer(inner)
|
inner = args[0]
|
||||||
if is_converter(inner) and not is_inner_tranformer:
|
is_inner_tranformer = is_transformer(inner)
|
||||||
param = param.replace(annotation=Optional[make_converter_transformer(inner)]) # type: ignore
|
if is_converter(inner) and not is_inner_tranformer:
|
||||||
|
param = param.replace(annotation=Optional[make_converter_transformer(inner)]) # type: ignore
|
||||||
|
else:
|
||||||
|
raise
|
||||||
elif callable(converter) and not inspect.isclass(converter):
|
elif callable(converter) and not inspect.isclass(converter):
|
||||||
|
param_count = required_pos_arguments(converter)
|
||||||
|
if param_count != 1:
|
||||||
|
raise
|
||||||
param = param.replace(annotation=make_callable_transformer(converter))
|
param = param.replace(annotation=make_callable_transformer(converter))
|
||||||
|
|
||||||
if parameter.default is not parameter.empty:
|
if parameter.default is not parameter.empty:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user