pycord-v3 icon indicating copy to clipboard operation
pycord-v3 copied to clipboard

Remove InvalidArgument exception in favor of built-in TypeError and ValueError

Open Copilot opened this issue 4 months ago • 4 comments

InvalidArgument duplicated behavior already provided by Python's TypeError and ValueError, adding unnecessary API surface and diverging from Python conventions.

Changes

  • Removed InvalidArgument class from errors.py and __all__ exports
  • Replaced 107 raise InvalidArgument statements with:
    • TypeError (34 cases) - for type mismatches (e.g., isinstance checks)
    • ValueError (73 cases) - for invalid values with correct types (e.g., range checks, conflicting parameters)
  • Updated imports across 18 files to remove InvalidArgument references
  • Updated docstrings to specify TypeError or ValueError instead of generic InvalidArgument

Migration

Before:

if not isinstance(perm, PermissionOverwrite):
    raise InvalidArgument(f"Expected PermissionOverwrite received {perm.__class__.__name__}")

if position <= 0:
    raise InvalidArgument("Cannot move role to position 0 or below")

After:

if not isinstance(perm, PermissionOverwrite):
    raise TypeError(f"Expected PermissionOverwrite received {perm.__class__.__name__}")

if position <= 0:
    raise ValueError("Cannot move role to position 0 or below")

Net reduction of 22 lines while improving semantic clarity and Python standards compliance.

Original prompt

This section details on the original issue you should resolve

<issue_title>Remove InvalidArgument infavour of builtin TypeError & ValueError</issue_title> <issue_description>InvalidArgument currently duplicates behavior that is already covered by the built-in exceptions TypeError and ValueError. In most cases across the codebase, the situations where InvalidArgument is raised are either:

  • Incorrect types → should be TypeError
  • Incorrect values (but correct type) → should be ValueError

Because Python already provides a clear semantic split here, InvalidArgument adds no meaningful benefit and instead introduces inconsistency in error patterns across the library.

Proposal

  • Remove InvalidArgument
  • Replace existing uses with the appropriate built-in exception (TypeError or ValueError depending on context)
  • Update documentation and type hints accordingly

Benefits

  • Aligns with standard Python exception semantics
  • Improves clarity for users handling exceptions
  • Reduces surface area of the public API </issue_description>

Comments on the Issue (you are @copilot in this section)

  • Fixes Pycord-Development/pycord-next#186

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot avatar Nov 07 '25 18:11 Copilot

Thanks for opening this pull request! Please make sure you have read the Contributing Guidelines and Code of Conduct.

This pull request can be checked-out with:

git fetch origin pull/187/head:pr-187
git checkout pr-187

This pull request can be installed with:

pip install git+https://github.com/Pycord-Development/pycord-next@refs/pull/187/head

pycord-app[bot] avatar Nov 07 '25 18:11 pycord-app[bot]

Holy I didn't expect it to do this

Soheab avatar Nov 07 '25 18:11 Soheab

Holy I didn't expect it to do this

Soheab avatar Nov 07 '25 18:11 Soheab

Lok

Paillat-dev avatar Nov 07 '25 18:11 Paillat-dev

Lok

Paillat-dev avatar Nov 07 '25 18:11 Paillat-dev

Documentation build overview

📚 pycord-next | 🛠️ Build #30242042 | 📁 Comparing d47bfbcdbbd284073dcdd13b31e3b6acf6b68e94 against latest (cd7ab4193d83947746b71ea909e7fd73e43e6065)


🔍 Preview build

Show files changed (32 files in total): 📝 32 modified | ➕ 0 added | ➖ 0 deleted
File Status
genindex.html 📝 modified
api/abcs.html 📝 modified
api/application_commands.html 📝 modified
api/clients.html 📝 modified
api/data_classes.html 📝 modified
api/exceptions.html 📝 modified
api/models.html 📝 modified
api/webhooks.html 📝 modified
_modules/discord/abc.html 📝 modified
_modules/discord/asset.html 📝 modified
_modules/discord/channel.html 📝 modified
_modules/discord/client.html 📝 modified
_modules/discord/errors.html 📝 modified
_modules/discord/guild.html 📝 modified
_modules/discord/integrations.html 📝 modified
_modules/discord/interactions.html 📝 modified
_modules/discord/member.html 📝 modified
_modules/discord/message.html 📝 modified
_modules/discord/partial_emoji.html 📝 modified
_modules/discord/reaction.html 📝 modified
_modules/discord/role.html 📝 modified
_modules/discord/scheduled_events.html 📝 modified
_modules/discord/shard.html 📝 modified
_modules/discord/stage_instance.html 📝 modified
_modules/discord/template.html 📝 modified
_modules/discord/user.html 📝 modified
ext/commands/api.html 📝 modified
_modules/discord/commands/core.html 📝 modified
_modules/discord/ui/select.html 📝 modified
modules/discord/webhook/async.html 📝 modified
_modules/discord/webhook/sync.html 📝 modified
_modules/discord/ext/pages/pagination.html 📝 modified

Documentation build overview

📚 pycord-next | 🛠️ Build #30242042 | 📁 Comparing d47bfbcdbbd284073dcdd13b31e3b6acf6b68e94 against latest (cd7ab4193d83947746b71ea909e7fd73e43e6065)


🔍 Preview build

Show files changed (32 files in total): 📝 32 modified | ➕ 0 added | ➖ 0 deleted
File Status
genindex.html 📝 modified
api/abcs.html 📝 modified
api/application_commands.html 📝 modified
api/clients.html 📝 modified
api/data_classes.html 📝 modified
api/exceptions.html 📝 modified
api/models.html 📝 modified
api/webhooks.html 📝 modified
_modules/discord/abc.html 📝 modified
_modules/discord/asset.html 📝 modified
_modules/discord/channel.html 📝 modified
_modules/discord/client.html 📝 modified
_modules/discord/errors.html 📝 modified
_modules/discord/guild.html 📝 modified
_modules/discord/integrations.html 📝 modified
_modules/discord/interactions.html 📝 modified
_modules/discord/member.html 📝 modified
_modules/discord/message.html 📝 modified
_modules/discord/partial_emoji.html 📝 modified
_modules/discord/reaction.html 📝 modified
_modules/discord/role.html 📝 modified
_modules/discord/scheduled_events.html 📝 modified
_modules/discord/shard.html 📝 modified
_modules/discord/stage_instance.html 📝 modified
_modules/discord/template.html 📝 modified
_modules/discord/user.html 📝 modified
ext/commands/api.html 📝 modified
_modules/discord/commands/core.html 📝 modified
_modules/discord/ui/select.html 📝 modified
modules/discord/webhook/async.html 📝 modified
_modules/discord/webhook/sync.html 📝 modified
_modules/discord/ext/pages/pagination.html 📝 modified

@Soheab Can I close this ?

Paillat-dev avatar Dec 28 '25 12:12 Paillat-dev

@Soheab Can I close this ?

Paillat-dev avatar Dec 28 '25 12:12 Paillat-dev

@Soheab Can I close this ?

Yes

Soheab avatar Dec 28 '25 12:12 Soheab

@Soheab Can I close this ?

Yes

Soheab avatar Dec 28 '25 12:12 Soheab