disnake icon indicating copy to clipboard operation
disnake copied to clipboard

refactor: `type(x).__name__` => `x.__class__.__name__`

Open Enegg opened this issue 2 months ago • 4 comments

Summary

This PR unifies the style of access to a value's type's name, replacing type(x).__name__ with x.__class__.__name__ (since the latter has been used more often) Also moves a type guard at disnake/ext/tasks/init.py#L125-L127 higher up, so it can fail quicker.

Checklist

  • [x] If code changes were made, then they have been tested
    • [ ] I have updated the documentation to reflect the changes
    • [x] I have formatted the code properly by running uv run nox -s lint
    • [x] I have type-checked the code by running uv run nox -s pyright
  • [ ] 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, ...)

Enegg avatar Oct 24 '25 02:10 Enegg

Documentation build overview

📚 disnake | 🛠️ Build #30061600 | 📁 Comparing 30d1a0ce75fd1647e6d00555fb3f8a1f98ee8290 against latest (79a8027a14e8ef99789490fd94f9b7d52b6a1422)


🔍 Preview build

Show files changed (48 files in total): 📝 48 modified | ➕ 0 added | ➖ 0 deleted
File Status
index.html 📝 modified
whats_new.html 📝 modified
api/abc.html 📝 modified
api/activities.html 📝 modified
api/app_commands.html 📝 modified
api/app_info.html 📝 modified
api/audit_logs.html 📝 modified
api/automod.html 📝 modified
api/channels.html 📝 modified
api/clients.html 📝 modified
api/components.html 📝 modified
api/emoji.html 📝 modified
api/entitlements.html 📝 modified
api/events.html 📝 modified
api/exceptions.html 📝 modified
api/guild_scheduled_events.html 📝 modified
api/guilds.html 📝 modified
api/integrations.html 📝 modified
api/interactions.html 📝 modified
api/invites.html 📝 modified
api/localization.html 📝 modified
api/members.html 📝 modified
api/messages.html 📝 modified
api/misc.html 📝 modified
api/permissions.html 📝 modified
api/roles.html 📝 modified
api/skus.html 📝 modified
api/soundboard.html 📝 modified
api/stage_instances.html 📝 modified
api/stickers.html 📝 modified
api/subscriptions.html 📝 modified
api/ui.html 📝 modified
api/users.html 📝 modified
api/utilities.html 📝 modified
api/voice.html 📝 modified
api/webhooks.html 📝 modified
api/widgets.html 📝 modified
ext/tasks/index.html 📝 modified
ext/commands/api/app_commands.html 📝 modified
ext/commands/api/bots.html 📝 modified
ext/commands/api/checks.html 📝 modified
ext/commands/api/cogs.html 📝 modified
ext/commands/api/context.html 📝 modified
ext/commands/api/converters.html 📝 modified
ext/commands/api/exceptions.html 📝 modified
ext/commands/api/help_commands.html 📝 modified
ext/commands/api/misc.html 📝 modified
ext/commands/api/prefix_commands.html 📝 modified

There's a risk to this as __class__ can be overridden by user code, and the majority of these are used in instances where the instance check failed and a type could be an arbitrary type.

I'm not sure unification of these is necessary in one big go, but we should probably standardize on a specific type for future code.

onerandomusername avatar Nov 01 '25 18:11 onerandomusername

The PR could go the other way and replace .__class__ with type(). Just wanted to standardize it as a prelude to standardizing exception messages that I have a branch for

Enegg avatar Nov 01 '25 21:11 Enegg

There's a risk to this as __class__ can be overridden by user code

type(x) appears to be completely equivalent to x.__class__ even when overridden by user code: https://github.com/python/cpython/blob/95296a9d40aa2d58502a09e86e2a93c03df23366/Objects/typeobject.c#L5070-L5080 https://github.com/python/cpython/blob/95296a9d40aa2d58502a09e86e2a93c03df23366/Objects/typeobject.c#L7308-L7312 All in all this seems fine. There isn't a way around it, but I also think we don't need one.

shiftinv avatar Nov 19 '25 12:11 shiftinv