Skip to content

refactor: Make Object generic - #3450

Open
Paillat-dev wants to merge 2 commits into
masterfrom
refactor/object
Open

Paillat-dev wants to merge 2 commits into
masterfrom
refactor/object

Conversation

@Paillat-dev

@Paillat-dev Paillat-dev commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Preparation work to allow more places to accept Object without destroying typing enforcement.

example:

image

It allows us to type later on other methods as requiring Something | Object[Something] and force users to explicitly pass an object like Object(my_id, type=SomethingCompatible) where SomethignCompatible is compatible with Something. Before, Object.type was only used for runtime checks but was not checked at type checking time.

(this PR does not modify runtime behavior)

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.
  • 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.
  • I have read the Contributing Guidelines.
  • AI Usage has been disclosed.
    • If AI has been used, I understand fully what the code does

@pycord-app

pycord-app Bot commented Nov 17, 2026

Copy link
Copy Markdown

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/3450/head:pr-3450
git checkout pr-3450

This pull request can be installed with:

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

@Paillat-dev Paillat-dev added the typing Is related to / changes python typing label Nov 17, 2026
Comment thread discord/object.py Resolved
@Dorukyum

Copy link
Copy Markdown
Member

Can you tell why this helps?

@Paillat-dev

Paillat-dev commented Nov 17, 2026 •

Copy link
Copy Markdown
Member Author

It allows us to type later on other methods as requiring Something | Object[Something] and force users to explicitly pass an object like Object(my_id, type=SomethingCompatible) where SomethignCompatible is compatible with Something. Before, Object.type was only used for runtime checks but was not checked at type checking time.

(this PR does not modify runtime behavior)

@Dorukyum

Copy link
Copy Markdown
Member

In your example, the type Object[VocalGuildChannel] invalidates .change_voice_state(channel=Object(id)) in type checking and makes it a requirement to pass a matching type. I think this adds an unnecessary step. We could still merge this and only add Object[type] type hints where the .type attribute is actually used (my text editor only found three such instances).

@Paillat-dev

Paillat-dev commented Nov 17, 2026 •

Copy link
Copy Markdown
Member Author

My example was not an actual implementation proposal, just a showcase of how it worked & the method I used for testing on my end.

That said, I actually believe that it would be beneficial to implement this more than exclusively where .type is actually used, given that many places that currently could but don't accept a discord.Object don't do so because that would broaden it to accepting any discord.Object, loosing the information about what kinds of Snowflakes are actually accepted in a given context, even though it would be beneficial for more advanced uses (it has already happened to me multiple times to missinterpret what an Object was, passing it with forced type ignores because pycord wouldn't accept it and then having to fix a runtime issue).

In general (at least from my experience type checking all my python strictly), it is common to add verbosity if it means being clearer with what types something actually represents. And this would not impact "standard" users that pass normal "full" objects from the cache for e.g. (which is the case broadly right now, as it is the only supported system [the example method above, change_voice_state currently only accepts a VocalGuildChannel]), nor users who don't type check their code strictly.

@vmphase maybe you have a better idea on how to do this ?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PA: All Contributors pending typing Is related to / changes python typing

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants