[commands] Add converter kwarg to flag

This commit is contained in:
z03h 2022-04-08 23:10:32 -07:00 committed by GitHub
parent 1e914e8945
commit aee657bca5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -103,6 +103,7 @@ def flag(
default: Any = MISSING, default: Any = MISSING,
max_args: int = MISSING, max_args: int = MISSING,
override: bool = MISSING, override: bool = MISSING,
converter: Any = MISSING,
) -> Any: ) -> Any:
"""Override default functionality and parameters of the underlying :class:`FlagConverter` """Override default functionality and parameters of the underlying :class:`FlagConverter`
class attributes. class attributes.
@ -124,8 +125,11 @@ def flag(
override: :class:`bool` override: :class:`bool`
Whether multiple given values overrides the previous value. The default Whether multiple given values overrides the previous value. The default
value depends on the annotation given. value depends on the annotation given.
converter: Any
The converter to use for this flag. This replaces the annotation at
runtime which is transparent to type checkers.
""" """
return Flag(name=name, aliases=aliases, default=default, max_args=max_args, override=override) return Flag(name=name, aliases=aliases, default=default, max_args=max_args, override=override, annotation=converter)
def validate_flag_name(name: str, forbidden: Set[str]) -> None: def validate_flag_name(name: str, forbidden: Set[str]) -> None:
@ -150,7 +154,8 @@ def get_flags(namespace: Dict[str, Any], globals: Dict[str, Any], locals: Dict[s
for name, annotation in annotations.items(): for name, annotation in annotations.items():
flag = namespace.pop(name, MISSING) flag = namespace.pop(name, MISSING)
if isinstance(flag, Flag): if isinstance(flag, Flag):
flag.annotation = annotation if flag.annotation is MISSING:
flag.annotation = annotation
else: else:
flag = Flag(name=name, annotation=annotation, default=flag) flag = Flag(name=name, annotation=annotation, default=flag)