aboutsummaryrefslogtreecommitdiff
path: root/discord/ext
diff options
context:
space:
mode:
authorRapptz <[email protected]>2021-04-30 01:23:56 -0400
committerRapptz <[email protected]>2021-04-30 01:23:56 -0400
commit58274eafbcf251705612ae7b9c2fed48c5553ea2 (patch)
tree8cd4451e1337bfc5d5dfc0da2d9e05234422563d /discord/ext
parentFix utils.MISSING not evaluating to True in implicit bool contexts (diff)
downloaddiscord.py-58274eafbcf251705612ae7b9c2fed48c5553ea2.tar.xz
discord.py-58274eafbcf251705612ae7b9c2fed48c5553ea2.zip
[commands] Fix Generics causing other typing converters to fail
Diffstat (limited to 'discord/ext')
-rw-r--r--discord/ext/commands/converter.py17
1 files changed, 14 insertions, 3 deletions
diff --git a/discord/ext/commands/converter.py b/discord/ext/commands/converter.py
index fab826db..1f1828b1 100644
--- a/discord/ext/commands/converter.py
+++ b/discord/ext/commands/converter.py
@@ -905,6 +905,13 @@ def get_converter(param: inspect.Parameter) -> Any:
return converter
+_GenericAlias = type(List[T])
+
+
+def is_generic_type(tp: Any, *, _GenericAlias: Type = _GenericAlias) -> bool:
+ return isinstance(tp, type) and issubclass(tp, Generic) or isinstance(tp, _GenericAlias) # type: ignore
+
+
CONVERTER_MAPPING: Dict[Type[Any], Any] = {
discord.Object: ObjectConverter,
discord.Member: MemberConverter,
@@ -996,9 +1003,6 @@ async def run_converters(ctx: Context, converter, argument: str, param: inspect.
"""
origin = getattr(converter, '__origin__', None)
- if origin is not None and issubclass(converter, Generic): # type: ignore
- converter = origin
-
if origin is Union:
errors = []
_NoneType = type(None)
@@ -1045,4 +1049,11 @@ async def run_converters(ctx: Context, converter, argument: str, param: inspect.
# if we're here, then we failed to match all the literals
raise BadLiteralArgument(param, literal_args, errors)
+ # This must be the last if-clause in the chain of origin checking
+ # Nearly every type is a generic type within the typing library
+ # So care must be taken to make sure a more specialised origin handle
+ # isn't overwritten by the widest if clause
+ if origin is not None and is_generic_type(converter):
+ converter = origin
+
return await _actual_conversion(ctx, converter, argument, param)