Skip to content
This repository was archived by the owner on Aug 28, 2019. It is now read-only.

Commit d1039e2

Browse files
committed
Skip default parameter validation when using a transformer
Fixes Rapptz#8110
1 parent 87edabc commit d1039e2

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

discord/app_commands/transformers.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -613,44 +613,46 @@ def get_supported_annotation(
613613
*,
614614
_none: type = NoneType,
615615
_mapping: Dict[Any, Type[Transformer]] = BUILT_IN_TRANSFORMERS,
616-
) -> Tuple[Any, Any]:
616+
) -> Tuple[Any, Any, bool]:
617617
"""Returns an appropriate, yet supported, annotation along with an optional default value.
618618
619+
The third boolean element of the tuple indicates if default values should be validated.
620+
619621
This differs from the built in mapping by supporting a few more things.
620622
Likewise, this returns a "transformed" annotation that is ready to use with CommandParameter.transform.
621623
"""
622624

623625
try:
624-
return (_mapping[annotation], MISSING)
626+
return (_mapping[annotation], MISSING, True)
625627
except KeyError:
626628
pass
627629

628630
if hasattr(annotation, '__discord_app_commands_transform__'):
629-
return (annotation.metadata, MISSING)
631+
return (annotation.metadata, MISSING, False)
630632

631633
if hasattr(annotation, '__metadata__'):
632634
return get_supported_annotation(annotation.__metadata__[0])
633635

634636
if inspect.isclass(annotation):
635637
if issubclass(annotation, Transformer):
636-
return (annotation, MISSING)
638+
return (annotation, MISSING, False)
637639
if issubclass(annotation, (Enum, InternalEnum)):
638640
if all(isinstance(v.value, (str, int, float)) for v in annotation):
639-
return (_make_enum_transformer(annotation), MISSING)
641+
return (_make_enum_transformer(annotation), MISSING, False)
640642
else:
641-
return (_make_complex_enum_transformer(annotation), MISSING)
643+
return (_make_complex_enum_transformer(annotation), MISSING, False)
642644
if annotation is Choice:
643645
raise TypeError(f'Choice requires a type argument of int, str, or float')
644646

645647
# Check if there's an origin
646648
origin = getattr(annotation, '__origin__', None)
647649
if origin is Literal:
648650
args = annotation.__args__ # type: ignore
649-
return (_make_literal_transformer(args), MISSING)
651+
return (_make_literal_transformer(args), MISSING, True)
650652

651653
if origin is Choice:
652654
arg = annotation.__args__[0] # type: ignore
653-
return (_make_choice_transformer(arg), MISSING)
655+
return (_make_choice_transformer(arg), MISSING, True)
654656

655657
if origin is not Union:
656658
# Only Union/Optional is supported right now so bail early
@@ -661,18 +663,18 @@ def get_supported_annotation(
661663
if args[-1] is _none:
662664
if len(args) == 2:
663665
underlying = args[0]
664-
inner, _ = get_supported_annotation(underlying)
666+
inner, _, validate_default = get_supported_annotation(underlying)
665667
if inner is None:
666668
raise TypeError(f'unsupported inner optional type {underlying!r}')
667-
return (inner, None)
669+
return (inner, None, validate_default)
668670
else:
669671
args = args[:-1]
670672
default = None
671673

672674
# Check for channel union types
673675
if any(arg in CHANNEL_TO_TYPES for arg in args):
674676
# If any channel type is given, then *all* must be channel types
675-
return (channel_transformer(*args, raw=None), default)
677+
return (channel_transformer(*args, raw=None), default, True)
676678

677679
# The only valid transformations here are:
678680
# [Member, User] => user
@@ -682,9 +684,9 @@ def get_supported_annotation(
682684
if not all(arg in supported_types for arg in args):
683685
raise TypeError(f'unsupported types given inside {annotation!r}')
684686
if args == (User, Member) or args == (Member, User):
685-
return (passthrough_transformer(AppCommandOptionType.user), default)
687+
return (passthrough_transformer(AppCommandOptionType.user), default, True)
686688

687-
return (passthrough_transformer(AppCommandOptionType.mentionable), default)
689+
return (passthrough_transformer(AppCommandOptionType.mentionable), default, True)
688690

689691

690692
def annotation_to_parameter(annotation: Any, parameter: inspect.Parameter) -> CommandParameter:
@@ -695,7 +697,7 @@ def annotation_to_parameter(annotation: Any, parameter: inspect.Parameter) -> Co
695697
of a command parameter.
696698
"""
697699

698-
(inner, default) = get_supported_annotation(annotation)
700+
(inner, default, validate_default) = get_supported_annotation(annotation)
699701
type = inner.type()
700702

701703
if default is MISSING or default is None:
@@ -704,12 +706,10 @@ def annotation_to_parameter(annotation: Any, parameter: inspect.Parameter) -> Co
704706
default = param_default
705707

706708
# Verify validity of the default parameter
707-
if default is not MISSING:
708-
enum_type = getattr(inner, '__discord_app_commands_transformer_enum__', None)
709-
if default.__class__ is not enum_type:
710-
valid_types: Tuple[Any, ...] = ALLOWED_DEFAULTS.get(type, (NoneType,))
711-
if not isinstance(default, valid_types):
712-
raise TypeError(f'invalid default parameter type given ({default.__class__}), expected {valid_types}')
709+
if default is not MISSING and validate_default:
710+
valid_types: Tuple[Any, ...] = ALLOWED_DEFAULTS.get(type, (NoneType,))
711+
if not isinstance(default, valid_types):
712+
raise TypeError(f'invalid default parameter type given ({default.__class__}), expected {valid_types}')
713713

714714
result = CommandParameter(
715715
type=type,

0 commit comments

Comments
 (0)