pycord icon indicating copy to clipboard operation
pycord copied to clipboard

refactor: :recycle: Refactor weird checks in `CogMeta` and fix some typing and other qol things

Open Paillat-dev opened this issue 9 months ago • 0 comments

Summary

Changes made:

  • Added typing for SlashCommand.parent
  • Added BridgeCommand.__bridge__ which is True
  • Replaced weird checks to assess something is a BridgeCommand by checking it has an add_to attribute (BridgeCommand is the only class in py-cord that has such method) with a TypeGuard that checks it has a __bridge__ attribute (which also makes this typed and that's cool)
  • Fixed typing of CogMeta.__cog_commands__
  • Rename name_filter to _name_filter and define it outside of __new__ to avoid redefining it every time and also avoid assigning a lambda to a variable.
  • Extract name validation logic (disallow the bot_ or cog_ prefixes) to a helper function _validate_name_prefix.
  • Extract class attributes iteration and processing to helper function _process_attributes. Simplify it by changing overengineered checks and simplifying dead code. Rename elem, value to attr_name, attr_value
  • Extract command update logic to _update_command, idem as above.
  • Import Generator & Mapping from collections.abc instead of typing (deprecated since python 3.9).

Tested and fixed all the issues I found, but it would be nice if someone else could also test this to make sure I didn't miss anything.

Information

  • [ ] This PR fixes an issue.
  • [ ] This PR adds something new (e.g. new method or parameters).
  • [ ] This PR is a breaking change (e.g. methods or parameters removed/renamed).
  • [ ] This PR is not a code change (e.g. documentation, README, typehinting, examples, ...).

Checklist

  • [ ] I have searched the open pull requests for duplicates.
  • [x] If code changes were made then they have been tested.
    • [ ] I have updated the documentation to reflect the changes.
  • [ ] If type: ignore comments were used, a comment is also left explaining why.
  • [ ] I have updated the changelog to include these changes.

Paillat-dev avatar Mar 04 '25 12:03 Paillat-dev