gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
34.17k stars 2.6k forks source link

pyright error: Stub file not found for "gradio" #8923

Open JackismyShephard opened 3 months ago

JackismyShephard commented 3 months ago

Describe the bug

I am using pyright with strict mode for type checking in visual studio code. When importing gradio, pyright complains Stub file not found for "gradio". I guess the error should have been fixed after this https://github.com/gradio-app/gradio/issues/6396 was solved, but that is not the case for me.

Have you searched existing issues? 🔎

Reproduction

  1. Install gradio using pip (or some other package manager)
  2. Create an app.py file
    import gradio as gr
  3. Open it in visual studio code with pyright strict mode enabled

Screenshot

image

Logs

No response

System Info

Gradio 4.39.0

Python 3.10.6

Windows 11

Severity

I can work around it

abidlabs commented 3 months ago

Hi @JackismyShephard did you resolve this issue? (Given https://github.com/gradio-app/gradio/issues/8928)

JackismyShephard commented 3 months ago

@abidlabs I have not solved this issue yet, but I might be able to solve it alongside https://github.com/gradio-app/gradio/issues/8928.

JackismyShephard commented 3 months ago

@abidlabs I have been able to solve this issue now locally. The problem was that a type stub (.pyi file) was missing for gradio/__init__.py . Pyright also complained about gradio.events missing a stub file, so I added gradio/events.pyi as well. Both type stubs explicitly export all definitions by setting __all__ appropriately.

I would like to incorporate these changes upstream but I know you render type stub files dynamically so I am not 100% sure how to replicate my manually defined type stubs.

abidlabs commented 2 months ago

Thanks @JackismyShephard! Just to make sure I understand, why do you need a .pyi file if it just has the same definitions as the .py files? Pyright should just be able to glean that information from the .py files directly?

I think that we just need to add __all__ in the gradio/__init__.py? I notice that its missing in that file, but included in the other __init__.py files. Can you see if adding that resolves the typing issues for you? (Perhaps similarly we need to modify gradio.events.py instead of creating a new .pyi file?)

JackismyShephard commented 2 months ago

@abidlabs I have tried doing as you suggest but that does not fix the error reported by pyright. Pyright is specifically complaining that a stub file is missing so a .pyi exporting all definitions from the corresponding .py is needed. Pyright by default infers typing from .py files if no .pyi files are present. I think the problem in this case might be that some but not all gradio module files have corresponding type stubs. This might be confusing pyright.

0gust1 commented 1 month ago

Edit: comment edited (improper initial testing)

Can confirm the "error" still present. However, pyright is still working, including gradio elements.

To get rid of the error (not very satisfactory):

I think the problem in this case might be that some but not all gradio module files have corresponding type stubs. This might be confusing pyright.

After playing with pyright conf, and trying to patch my local gradio package, I have the same feeling too.

programmer-ke commented 1 week ago

I'm experiencing a similar issue but with mypy.

In app.py:

import gradio as gr

def hello_world(messages: str, history):
    return "Hello, World!"

gr.ChatInterface(hello_world).launch()

When I run $ mypy app.py, I get:

app.py:1: error: Skipping analyzing "gradio": module is installed, but missing library stubs or py.typed marker  [import-untyped]
app.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)

pip freeze | grep -E -e "gradio|mypy"

gradio==5.5.0
gradio_client==1.4.2
mypy==1.13.0
mypy-extensions==1.0.0

$ gradio enviroment

Gradio Environment Information:
------------------------------
Operating System: Linux
gradio version: 5.5.0
gradio_client version: 1.4.2
JackismyShephard commented 3 days ago

@0gust1 @programmer-ke I have manually implemented stubs that fix this problem. I guess I could share the stubs if you are interested. Just have to keep in mind that these need to (potentially) be manually updated each time there is a gradio update.

abidlabs commented 2 days ago

@JackismyShephard if you can share here, we can revisit this issue and try to include them within the gradio library

JackismyShephard commented 1 day ago

@abidlabs Here they are: gradio.zip

These should be up to date with gradio 5.6.0