Skip to content

refactor: type(x).__name__ => x.__class__.__name__#1466

Merged
shiftinv merged 3 commits into
DisnakeDev:masterfrom
Enegg:Eneg/unify-type-name
Jul 1, 2026
Merged

refactor: type(x).__name__ => x.__class__.__name__#1466
shiftinv merged 3 commits into
DisnakeDev:masterfrom
Enegg:Eneg/unify-type-name

Conversation

@Enegg

@Enegg Enegg commented Oct 24, 2025

Copy link
Copy Markdown
Contributor

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

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

@read-the-docs-community

read-the-docs-community Bot commented Oct 24, 2025

Copy link
Copy Markdown

Documentation build overview

📚 disnake | 🛠️ Build #33399248 | 📁 Comparing 6206047 against latest (7ee96b8)

  🔍 Preview build  

47 files changed · ± 47 modified

± Modified

@Victorsitou Victorsitou added s: needs review Issue/PR is awaiting reviews t: refactor/typing/lint Refactors, typing changes and/or linting changes skip news labels Oct 24, 2025
@onerandomusername

Copy link
Copy Markdown
Member

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.

@Enegg

Enegg commented Nov 1, 2025

Copy link
Copy Markdown
Contributor Author

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

@shiftinv

Copy link
Copy Markdown
Member

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.

Comment thread disnake/ui/container.py Outdated
Comment thread disnake/embeds.py Outdated
Comment thread disnake/embeds.py Outdated
@shiftinv

Copy link
Copy Markdown
Member

Would you mind updating this PR and resolving merge conflicts? ty c:

@Enegg Enegg force-pushed the Eneg/unify-type-name branch from 30d1a0c to 13da86d Compare July 1, 2026 19:17
Co-authored-by: vi <8530778+shiftinv@users.noreply.github.com>
Signed-off-by: Eneg <42005170+Enegg@users.noreply.github.com>
@shiftinv shiftinv merged commit 2bfeac1 into DisnakeDev:master Jul 1, 2026
34 checks passed
@Enegg Enegg deleted the Eneg/unify-type-name branch July 1, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

s: needs review Issue/PR is awaiting reviews skip news t: refactor/typing/lint Refactors, typing changes and/or linting changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants