bryanforbes / discord.py-stubs

discord.py stubs
BSD 3-Clause "New" or "Revised" License
46 stars 12 forks source link

Is discord.py-stubs no longer needed as of discord.py 2.0.0 ? #171

Open richfromm opened 2 years ago

richfromm commented 2 years ago

Apologies if opening an issue is not the best way to ask this question, I wasn't sure how else to.

I have some code using discord.py (https://github.com/richfromm/slack2discord) to which I'm adding mypy support. I had earlier seen the warning:

/Users/rich/.virtualenvs/slack2discord/lib/python3.10/site-packages/discord/app_commands/commands.py:72: error: Cannot find implementation or library stub for module named "discord.ext.commands"
/Users/rich/.virtualenvs/slack2discord/lib/python3.10/site-packages/discord/app_commands/commands.py:72: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

Which caused me to find and try installing this library.

But I see that if I first install the latest discord.py (currently 2.0.1), when I install discord.py-stubs, it downgrades discord.py to 1.7.3.

Looking further, I see that 1.7.3 is the latest release of discord.py-stubs (see https://github.com/bryanforbes/discord.py-stubs/tags), and that 1.7.3 is the end of the 1.x line for discord.py (see https://github.com/Rapptz/discord.py/tags), and that the versions need to match (https://github.com/bryanforbes/discord.py-stubs#version-numbering-scheme).

So I was about to open an issue asking when this stubs library would be updated for 2.0.0. and 2.0.1, but taking a look at the diffs in discord.py 2.0.0, I see that it now already includes mypy typing hints: https://github.com/Rapptz/discord.py/compare/v1.7.3...v2.0.0

And indeed, if I start over with a fresh virtualenv, I am no longer seeing the above error when running mypy with discord.py 2.0.1 installed and discord.py-stubs not installed at all.

So is this library no longer needed moving forward, and if so, should that be noted in the README ? (I can add some text and open a PR if you agree and would like me to take care of it.)

bryanforbes commented 2 years ago

discord.py is fully typed starting in 2.0, so there's no need to use stubs anymore. If you'd like to open a PR to add that to the README, I'd be very grateful.

richfromm commented 2 years ago

Sure, I'll add something to the README

But while I've got your attention, I think I know why I was a bit confused initially, and I'm wondering if you have a clue what's going on. I'm in the process of adding mypy typing hints to my project, and it's currently passing. However, when I run mypy, every other time (precisely), it gives me the warning I included above, about not being able to find an implementation or library stub for discord.ext.commands. As in, I can put it in a tight loop, not touching anything at all, and it alternates fail, pass, fail, pass, etc. See below. Any clue what might be going on? Could this be an issue with discord.py? A bug in mypy? I know mypy does some local caching (there's a .mypy_cache dir), but I don't know the details of how that works. This is with discord.py 2.0.1, mypy 0.982, and python 3.10.7

(slack2discord) rich@richmbp21 slack2discord % while [ true ]; do date; echo "BEGIN mypy"; mypy slack2discord.py; echo $?; echo "END mypy"; echo; sleep 1; done
while [ true ]; do date; echo "BEGIN mypy"; mypy slack2discord.py; echo $?; echo "END mypy"; echo; sleep 1; done
Thu Oct  6 17:23:04 PDT 2022
BEGIN mypy
/Users/rich/.virtualenvs/slack2discord/lib/python3.10/site-packages/discord/app_commands/commands.py:72: error: Cannot find implementation or library stub for module named "discord.ext.commands"
/Users/rich/.virtualenvs/slack2discord/lib/python3.10/site-packages/discord/app_commands/commands.py:72: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)
1
END mypy

Thu Oct  6 17:23:09 PDT 2022
BEGIN mypy
Success: no issues found in 1 source file
0
END mypy

Thu Oct  6 17:23:14 PDT 2022
BEGIN mypy
/Users/rich/.virtualenvs/slack2discord/lib/python3.10/site-packages/discord/app_commands/commands.py:72: error: Cannot find implementation or library stub for module named "discord.ext.commands"
/Users/rich/.virtualenvs/slack2discord/lib/python3.10/site-packages/discord/app_commands/commands.py:72: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)
1
END mypy

Thu Oct  6 17:23:18 PDT 2022
BEGIN mypy
Success: no issues found in 1 source file
0
END mypy

Thu Oct  6 17:23:23 PDT 2022
BEGIN mypy
/Users/rich/.virtualenvs/slack2discord/lib/python3.10/site-packages/discord/app_commands/commands.py:72: error: Cannot find implementation or library stub for module named "discord.ext.commands"
/Users/rich/.virtualenvs/slack2discord/lib/python3.10/site-packages/discord/app_commands/commands.py:72: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)
1
END mypy

Thu Oct  6 17:23:27 PDT 2022
BEGIN mypy
Success: no issues found in 1 source file
0
END mypy
richfromm commented 2 years ago

The offending line is at https://github.com/Rapptz/discord.py/blob/8f19cfe72925a1dd493c8e92525186d34915d0a9/discord/app_commands/commands.py#L72:

    # Generally, these two libraries are supposed to be separate from each other.
    # However, for type hinting purposes it's unfortunately necessary for one to
    # reference the other to prevent type checking errors in callbacks
    from discord.ext.commands import Cog

UPDATE: I think it should be:

    from discord.ext.commands.cog import Cog

Making that change locally in my virtualenv fixes the issue. No clue why the behavior would fail in the way it did without that change, though.

Anyway, it's outside the scope of this repo, I'll file an issue in discord.py

UPDATE2: There's already an issue: https://github.com/Rapptz/discord.py/pull/8473 And it's already been fixed, but isn't in the latest release as of this writing, but I'm not sure if I agree with the fix: https://github.com/Rapptz/discord.py/commit/e1961676a979e0684833bd90f1b7f74fe766677c#diff-edc6201f259e50254d3bc72339d8935f50a795fee7f4f0056c31e0641635c493

Regardless, that is indeed outside of the scope of this stub, although if I was going to put a disclaimer about it not being needed, but mypy doesn't quite work yet (which I think is the case, as the fix hasn't been released), that might influence my text. So for the immediate time I'm going to hold off a day or so and see if I get a response to any of my comments.