diff options
| author | Josh <[email protected]> | 2021-04-25 18:35:19 +1000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-04-25 04:35:19 -0400 |
| commit | 4fbc78ba8120d719be14d5fa34e7997671a0cb5e (patch) | |
| tree | 2ba8a49c99d1f062a133e4329d79e4f3832d382d /discord/ext/commands/flags.py | |
| parent | [commands] Fix regression with Union converters not working (diff) | |
| download | discord.py-4fbc78ba8120d719be14d5fa34e7997671a0cb5e.tar.xz discord.py-4fbc78ba8120d719be14d5fa34e7997671a0cb5e.zip | |
[commands] Add support for typing.Union to Flags
Diffstat (limited to 'discord/ext/commands/flags.py')
| -rw-r--r-- | discord/ext/commands/flags.py | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/discord/ext/commands/flags.py b/discord/ext/commands/flags.py index 6bbfb00b..e58c9ce5 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__) |