-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Snowflake Protocol class Invalid Under mypy #8354
Comments
Snowflake is used as a base class to concrete classes and as you said discord.py doesn't support mypy and it's recommended that you use pyright. I think if you want this fixed you should ask the mypy team. |
According to this REPL run I just did the concrete classes do not actually subclass from Snowflake (at least User/Guild) >>> import discord
>>> discord.User.mro()
[<class 'discord.user.User'>, <class 'discord.user.BaseUser'>, <class 'discord.user._UserTag'>, <class 'discord.abc.Messageable'>, <class 'object'>]
>>> discord.User.mro()[1].mro()
[<class 'discord.user.BaseUser'>, <class 'discord.user._UserTag'>, <class 'object'>]
>>> discord.Guild.mro()
[<class 'discord.guild.Guild'>, <class 'discord.mixins.Hashable'>, <class 'discord.mixins.EqualityComparable'>, <class 'object'>]
>>> Additionally in a patched version that comments out the >>> print(inspect.getsource(discord.abc.Snowflake))
@runtime_checkable
class Snowflake(Protocol):
"""An ABC that details the common operations on a Discord model.
Almost all :ref:`Discord models <discord_api_models>` meet this
abstract base class.
If you want to create a snowflake on your own, consider using
:class:`.Object`.
Attributes
-----------
id: :class:`int`
The model's unique ID.
"""
#__slots__ = ()
id: int
>>> a = discord.User(data={'username': "foo", "id": "95", "discriminator": "4543", "avatar": "None"}, state={}).__dict__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'User' object has no attribute '__dict__'. Did you mean: '__dir__'? |
An additional check using githubs class references feature on Snowflake shows that no classes outside of abc.py subclass Snowflake, and the only class that does subclass Snowflake (abc.User) also has no subclasses |
|
This is definitely a bug in mypy, but since the friction in fixing it is low enough I'll fix it. |
Summary
The Snowflake class raises errors in mypy due to having a defined empty slots, when mypy reads the interface it expects types passed to it to also have empty slots, but as most dont mypy raises errors on them
Reproduction Steps
run the below code through the
mypy
typechecker (using discord.py==2.0)Minimal Reproducible Code
Expected Results
mypy should raise no error here, as does pyright
Actual Results
mypy raises the following error due to expecting the defined slots on User to be
tuple[]
Intents
discord.Intents.all()
System Information
Checklist
Additional Context
While I understand mypy is not supported by discord.py, the fix is as simple as removing
__slots__ = ()
from the definition of Snowflake, and allows mypy to be used with discord.py again. Class init performance should not be a problem as Protocol subclasses may not be constructed at runtime.The text was updated successfully, but these errors were encountered: