From 4fbc78ba8120d719be14d5fa34e7997671a0cb5e Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 25 Apr 2021 18:35:19 +1000 Subject: [PATCH] [commands] Add support for typing.Union to Flags --- discord/ext/commands/flags.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/discord/ext/commands/flags.py b/discord/ext/commands/flags.py index 6bbfb00ba..e58c9ce5b 100644 --- a/discord/ext/commands/flags.py +++ b/discord/ext/commands/flags.py @@ -197,6 +197,7 @@ def get_flags(namespace: Dict[str, Any], globals: Dict[str, Any], locals: Dict[s # List[str] -> (max_args=-1) # Tuple[int, ...] -> (max_args=1) # Dict[K, V] -> (max_args=-1, override=True) + # Union[str, int] -> (max_args=1) # Optional[str] -> (default=None, max_args=1) try: @@ -206,11 +207,12 @@ def get_flags(namespace: Dict[str, Any], globals: Dict[str, Any], locals: Dict[s if flag.max_args is MISSING: flag.max_args = 1 else: - if origin is Union and annotation.__args__[-1] is type(None): - # typing.Optional + if origin is Union: + # typing.Union if flag.max_args is MISSING: flag.max_args = 1 - if flag.default is MISSING: + if annotation.__args__[-1] is type(None) and flag.default is MISSING: + # typing.Optional flag.default = None elif origin is tuple: # typing.Tuple @@ -421,10 +423,14 @@ async def convert_flag(ctx, argument: str, flag: Flag, annotation: Any = None) - return await tuple_convert_all(ctx, argument, flag, annotation.__args__[0]) else: return await tuple_convert_flag(ctx, argument, flag, annotation.__args__) - elif origin is list or origin is Union and annotation.__args__[-1] is type(None): - # typing.List[x] or typing.Optional[x] + elif origin is list: + # typing.List[x] annotation = annotation.__args__[0] return await convert_flag(ctx, argument, flag, annotation) + elif origin is Union and annotation.__args__[-1] is type(None): + # typing.Optional[x] + annotation = Union[annotation.__args__[:-1]] + return await run_converters(ctx, annotation, argument, param) elif origin is dict: # typing.Dict[K, V] -> typing.Tuple[K, V] return await tuple_convert_flag(ctx, argument, flag, annotation.__args__)