Skip to content
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

fix: library stub not found #8473

Merged
merged 1 commit into from
Sep 17, 2022
Merged

fix: library stub not found #8473

merged 1 commit into from
Sep 17, 2022

Conversation

SaidBySolo
Copy link
Contributor

@SaidBySolo SaidBySolo commented Sep 17, 2022

Summary

# sample.py
from discord import app_commands

app_commands.command()

When I check the file with mypy, it says that the stub cannot be found.

discord\app_commands\commands.py:89: error: Cannot find implementation or library stub for module named "discord.ext.commands"

#7743 (comment)
Perhaps it has something to do with the issue comment.

Checklist

  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • 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, ...)

@mikeshardmind
Copy link
Contributor

mikeshardmind commented Sep 17, 2022

I can't reproduce the issue this intends to fix with pyright, with mypy...

(.venv) PS > Get-Content .\scratch.py

import discord
from discord import app_commands


@app_commands.command()
async def test(interaction: discord.Interaction):
    ...
(.venv) PS > mypy --namespace-packages .\scratch.py 
Success: no issues found in 1 source file

You might need to add namespace-packages to your mypy config

(.venv) PS > mypy --no-namespace-packages .\scratch.py    
.venv\lib\site-packages\discord\app_commands\commands.py:72: error: Cannot find implementation or library stub for module named "discord.ext.commands"
.venv\lib\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)

which is something the error actually points you to

@SaidBySolo
Copy link
Contributor Author

SaidBySolo commented Sep 17, 2022

sample/
├─ __init__.py
├─ main.py
# main.py
import discord
from discord import app_commands

@app_commands.command()
async def main(interaction: discord.Interaction) -> None:
    ...

mypy ./sample --strict

This is a reproducible example.

Looking at #8327, I personally think this is not an ideal import.

@mikeshardmind
Copy link
Contributor

As pointed out previously, you can resolve this as a configuration change. mypy supports changing the behavior of import discovery. Trimming what I had above to only what's needed to show that, and including the error from mypy which also informed you of this:

(.venv) PS > mypy --strict --namespace-packages .\scratch.py 
Success: no issues found in 1 source file

vs

(.venv) PS > mypy --strict .\scratch.py
.venv\lib\site-packages\discord\app_commands\commands.py:72: error: Cannot find implementation or library stub for module named "discord.ext.commands"
.venv\lib\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)

@SaidBySolo
Copy link
Contributor Author

SaidBySolo commented Sep 17, 2022

@mikeshardmind
I don't like changing the configuration of my mypy because of one discord.py.

Are there any problems with these code changes?

@mikeshardmind
Copy link
Contributor

mikeshardmind commented Sep 17, 2022

I don't like changing the configuration of my mypy because of one discord.py.

mypy's error there literally tells you to do this if this is the reason for it not detecting the import, see the link in the error message. Beyond this, you can change it on a per-package basis. you don't need to change the behavior globally.

Are there any problems with these code changes?

Possibly (such as inconsistencies in import, churn for no tangible gain, etc) but I wasn't exactly reviewing the code, just pointing out that this shouldn't be an issue.

@SaidBySolo
Copy link
Contributor Author

SaidBySolo commented Sep 17, 2022

In my opinion, considering the issues mentioned above, I think these changes are meaningful changes to maintain consistency.

Copy link
Owner

@Rapptz Rapptz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Though I can't make any promises that I'll care about this for internal code. My comment pertained to external code. What @mikeshardmind said is correct, considering discord.py makes extensive use of namespace packages you need to enable the feature for it to work as expected on mypy.

@Rapptz Rapptz merged commit e196167 into Rapptz:master Sep 17, 2022
@SaidBySolo SaidBySolo deleted the fix/cant-found-library-stub branch September 17, 2022 19:03
@richfromm
Copy link

richfromm commented Oct 7, 2022

Fwiw, I am able to reproduce this problem with discord.py 2.0.1 and mypy 0.982 and python 3.10.7, but in a bizarre way, it alternates between failing and passing:
bryanforbes/discord.py-stubs#171 (comment)
bryanforbes/discord.py-stubs#171 (comment)

But from inspection (only) I'm wondering if the fix is correct or not:
e196167#r86069248
e196167#r86069343

@Rapptz
Copy link
Owner

Rapptz commented Oct 7, 2022

Are you running mypy with --namespace-packages?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants