Remove InvalidArgument exception in favor of built-in TypeError and ValueError
InvalidArgument duplicated behavior already provided by Python's TypeError and ValueError, adding unnecessary API surface and diverging from Python conventions.
Changes
-
Removed
InvalidArgumentclass fromerrors.pyand__all__exports -
Replaced 107
raise InvalidArgumentstatements with:-
TypeError(34 cases) - for type mismatches (e.g.,isinstancechecks) -
ValueError(73 cases) - for invalid values with correct types (e.g., range checks, conflicting parameters)
-
-
Updated imports across 18 files to remove
InvalidArgumentreferences -
Updated docstrings to specify
TypeErrororValueErrorinstead of genericInvalidArgument
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>
InvalidArgumentcurrently duplicates behavior that is already covered by the built-in exceptionsTypeErrorandValueError. In most cases across the codebase, the situations whereInvalidArgumentis raised are either:
- Incorrect types → should be
TypeError- Incorrect values (but correct type) → should be
ValueErrorBecause Python already provides a clear semantic split here,
InvalidArgumentadds 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 (
TypeErrororValueErrordepending 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.
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
Holy I didn't expect it to do this
Holy I didn't expect it to do this
Lok
Lok
Documentation build overview
📚 pycord-next | 🛠️ Build #30242042 | 📁 Comparing d47bfbcdbbd284073dcdd13b31e3b6acf6b68e94 against latest (cd7ab4193d83947746b71ea909e7fd73e43e6065)
Show files changed (32 files in total): 📝 32 modified | ➕ 0 added | ➖ 0 deleted
Documentation build overview
📚 pycord-next | 🛠️ Build #30242042 | 📁 Comparing d47bfbcdbbd284073dcdd13b31e3b6acf6b68e94 against latest (cd7ab4193d83947746b71ea909e7fd73e43e6065)
Show files changed (32 files in total): 📝 32 modified | ➕ 0 added | ➖ 0 deleted
@Soheab Can I close this ?
@Soheab Can I close this ?
@Soheab Can I close this ?
Yes
@Soheab Can I close this ?
Yes