[commands] Handle BadUnionArgument #3067

Manually merged
laggron42 merged 0 commits from laggron42/BadUnionArgument-handler into V3/develop 2026-11-21 16:09:15 +01:00
laggron42 commented 2019-10-17 09:43:26 +02:00 (Migrated from github.com)

Type

  • Bugfix
  • Enhancement
  • New feature

Description of the changes

When using typing.Union for a command argument, multiple conversion errors can happen and discord.py manages this by raising commands.BadUnionArgument. However, Red only handles commands.BadArgument. A BadUnionArgument will currently display help.

I added the support for this by creating a new class MultipleConversionFailures that inherits from BadUnionArgument, following ConversionFailure's logic. Message formatting is half-handled by discord.py (see here) and look like this:

Could not convert "argument name" into {first converter}, {second converter} or {third converter}

A bit of code was added on our side to also display each converter error. If only one message is available, then it will be displayed alone.

Example with two displayed messages:

from typing import Union

class A(commands.Converter):
    async def convert(self, ctx, arg):
        raise commands.BadArgument("argument A failed")

class B(commands.Converter):
    async def convert(self, ctx, arg):
        raise commands.BadArgument("argument B failed")

@commands.command()
async def test(ctx, arg: Union[A, B]):
    await ctx.send("we'll never reach this point anyway")

Could not convert "arg" into A or B.
argument A failed
argument B failed

Example with a single message:

from typing import Union

class A(commands.Converter):
    async def convert(self, ctx, arg):
        raise commands.BadArgument

class B(commands.Converter):
    async def convert(self, ctx, arg):
        raise commands.BadArgument("argument B failed")

@commands.command()
async def test(ctx, arg: Union[A, B]):
    await ctx.send("we'll never reach this point anyway")

argument B failed

### Type - [x] Bugfix - [x] Enhancement - [ ] New feature ### Description of the changes When using `typing.Union` for a command argument, multiple conversion errors can happen and discord.py manages this by [raising `commands.BadUnionArgument`](https://github.com/Rapptz/discord.py/blob/master/discord/ext/commands/core.py#L406). However, Red only handles `commands.BadArgument`. A `BadUnionArgument` will currently display help. I added the support for this by creating a new class `MultipleConversionFailures` that inherits from `BadUnionArgument`, following `ConversionFailure`'s logic. Message formatting is half-handled by discord.py ([see here](https://github.com/Rapptz/discord.py/blob/c6539bbc615e22a59455179b8526dc6ae264c2a4/discord/ext/commands/errors.py#L403)) and look like this: > Could not convert "argument name" into {first converter}, {second converter} or {third converter} A bit of code was added on our side to also display each converter error. If only one message is available, then it will be displayed alone. Example with two displayed messages: ```py from typing import Union class A(commands.Converter): async def convert(self, ctx, arg): raise commands.BadArgument("argument A failed") class B(commands.Converter): async def convert(self, ctx, arg): raise commands.BadArgument("argument B failed") @commands.command() async def test(ctx, arg: Union[A, B]): await ctx.send("we'll never reach this point anyway") ``` > Could not convert "arg" into A or B. > argument A failed > argument B failed Example with a single message: ```py from typing import Union class A(commands.Converter): async def convert(self, ctx, arg): raise commands.BadArgument class B(commands.Converter): async def convert(self, ctx, arg): raise commands.BadArgument("argument B failed") @commands.command() async def test(ctx, arg: Union[A, B]): await ctx.send("we'll never reach this point anyway") ``` > argument B failed
tekulvw (Migrated from github.com) reviewed 2019-10-17 09:43:26 +02:00
Twentysix26 (Migrated from github.com) reviewed 2019-10-17 09:43:26 +02:00
mikeshardmind (Migrated from github.com) requested changes 2019-10-17 13:49:37 +02:00
mikeshardmind (Migrated from github.com) left a comment

I'm not sure I'm following the rationale behind adding another exception type instead of just handling the existing one we don't handle here.

Should just handle the bad union argument.

Additionally, we really shouldn't be sending exceptions to the user without confirming the exceptions are designed as user feedback here, this makes no attempt to see what the failures are, and just joins them to send.

I'm not sure I'm following the rationale behind adding another exception type instead of just handling the existing one we don't handle here. Should just handle the bad union argument. Additionally, we really shouldn't be sending exceptions to the user without confirming the exceptions are designed as user feedback here, this makes no attempt to see what the failures are, and just joins them to send.
laggron42 commented 2019-10-18 17:15:18 +02:00 (Migrated from github.com)

Fixed. I didn't notice BadUnionArgument was including all command errors instead of only catching BadArguments. And I also removed MultipleConversionError, didn't realize the base class already included all needed arguments compared to ConversionError.

Fixed. I didn't notice `BadUnionArgument` was including all command errors instead of only catching `BadArgument`s. And I also removed `MultipleConversionError`, didn't realize the base class already included all needed arguments compared to `ConversionError`.
mikeshardmind (Migrated from github.com) requested changes 2019-10-20 01:27:58 +02:00
mikeshardmind (Migrated from github.com) left a comment

This is still presenting errors to users without any meangingful context.

Let's say for example the following converter

Union[discord.TextChannel, int]

The user will then get two messages about the same arg telling them two converters failed.

This wont be user friendly, as the number of errors they are shown won't match the actual issue.

While it's true that each of the converters displaying an error did fail, it's not intuitive to an end user to see seperate failures to one argument without an explanation.

This may require a much more in depth handling of converters if we want to be able to retain this clarity while also supporting i18n

Currently, at least when users are sent help, the help text can be tailored to explain the usage of the command and have that be translated.

This is still presenting errors to users without any meangingful context. Let's say for example the following converter ```py Union[discord.TextChannel, int] ``` The user will then get two messages about the same arg telling them two converters failed. This wont be user friendly, as the number of errors they are shown won't match the actual issue. While it's true that each of the converters displaying an error did fail, it's not intuitive to an end user to see seperate failures to one argument without an explanation. This may require a much more in depth handling of converters if we want to be able to retain this clarity while also supporting i18n Currently, at least when users are sent help, the help text can be tailored to explain the usage of the command and have that be translated.
Jackenmen commented 2022-12-03 14:34:40 +01:00 (Migrated from github.com)

I haven't tested it but based on the previous review comments and my own expectations, I imagine that the general clarity of error messages will somewhat decrease if this were applied to Red on its own.

I think this may be an acceptable catch-all scenario but it would be great if we encouraged quality error messages by the usage of custom converters wherever possible.
I think we could provide a bunch of equivalent custom converters to commonly used Union converters. It doesn't ensure that anyone uses them but it would make for a better experience.

In addition to this, we could special-case on the Union's arguments (i.e. sth like `if converter == Union[discord.TextChannel, int]') to make sure that we don't lower the error messages in common scenarios. It shouldn't be hard to achieve and if we can convert a significant portion of error messages this way, I think it would be worth it.

Maybe we could even be a bit smarter about it than doing exact equality comparisons for the special cases but I think that this would be going into 'too much effort for not much gain' territory.

I'm not suggesting any concrete changes yet, just trying to revive discussion on this as, in general, the current UX of Union converter errors in Red leaves a lot to desire.

So, what do you think?

I haven't tested it but based on the previous review comments and my own expectations, I imagine that the general clarity of error messages will somewhat decrease if this were applied to Red on its own. I think this may be an acceptable catch-all scenario but it would be great if we encouraged quality error messages by the usage of custom converters wherever possible. I think we could provide a bunch of equivalent custom converters to commonly used `Union` converters. It doesn't ensure that anyone uses them but it would make for a better experience. In addition to this, we *could* special-case on the Union's arguments (i.e. sth like `if converter == Union[discord.TextChannel, int]') to make sure that we don't lower the error messages in common scenarios. It shouldn't be hard to achieve and if we can convert a significant portion of error messages this way, I think it would be worth it. Maybe we could even be a bit smarter about it than doing exact equality comparisons for the special cases but I think that this would be going into 'too much effort for not much gain' territory. I'm not suggesting any concrete changes yet, just trying to revive discussion on this as, in general, the current UX of Union converter errors in Red leaves a lot to desire. So, what do you think?
Mrcoolgamer11 manually merged commit 98fcb78d70 into V3/develop 2026-11-21 16:09:15 +01:00
Owner

This PR was ported to the current codebase in e75740cbb instead of being merged as-is:

Show the errors of all converters when a Union argument fails to convert,
instead of only showing the command's help.

This PR was ported to the current codebase in e75740cbb instead of being merged as-is: Show the errors of all converters when a Union argument fails to convert, instead of only showing the command's help.
Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!3067
No description provided.