gradio icon indicating copy to clipboard operation
gradio copied to clipboard

pyright error: Stub file not found for "gradio"

Open JackismyShephard opened this issue 1 year ago • 5 comments

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? 🔎

  • [X] I have searched and found no existing issues

Reproduction

  1. Install gradio using pip (or some other package manager)
  2. Create an app.py file
import gradio as gr
  1. 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

JackismyShephard avatar Jul 28 '24 14:07 JackismyShephard

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

abidlabs avatar Jul 29 '24 18:07 abidlabs

@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 avatar Jul 30 '24 15:07 JackismyShephard

@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.

JackismyShephard avatar Aug 25 '24 17:08 JackismyShephard

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?)

abidlabs avatar Aug 27 '24 23:08 abidlabs

@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.

JackismyShephard avatar Aug 28 '24 20:08 JackismyShephard

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):

  • (improper?) quickfix is to let VSCode generate locally the stubs
  • configure pyright at project-level: "reportMissingTypeStubs":false

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.

0gust1 avatar Oct 21 '24 10:10 0gust1

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

programmer-ke avatar Nov 13 '24 14:11 programmer-ke

@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.

JackismyShephard avatar Nov 22 '24 00:11 JackismyShephard

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

abidlabs avatar Nov 23 '24 09:11 abidlabs

@abidlabs Here they are: gradio.zip

These should be up to date with gradio 5.6.0

JackismyShephard avatar Nov 23 '24 15:11 JackismyShephard

Thanks, will work on this as soon as I can!

abidlabs avatar Nov 27 '24 19:11 abidlabs

Since gradio already includes types it should simply need a py.typed marker in the package that's available on PyPI in order to resolve this issue. See the streamlit package for example.

As far as I know, py.typed is just an empty file that marks the module as "available" for type checkers like mypy and pyright.

The gradio source mentions py.typed: https://github.com/search?q=repo%3Agradio-app%2Fgradio%20py.typed&type=code -- but it seems to be limited to running the type checker, the file doesn't end up in the PyPI artifact.

For example:

❯ curl -sSLO https://files.pythonhosted.org/packages/c1/99/d01b99288c58855c86a0f65a1590b5e9cca7f30005fd8b2c39a55d08867a/gradio-5.8.0-py3-none-any.whl
❯ unzip -l gradio-5.8.0-py3-none-any.whl|grep py.typed; echo $status
1

Doing the same thing for streamlit:

❯ curl -sSLO https://files.pythonhosted.org/packages/ae/53/418536f5d0b87bfbe7bbd8c001983c27e9474f82723bd2e529660fd9a534/streamlit-1.40.2-py2.py3-none-any.whl
❯ unzip -l streamlit-1.40.2-py2.py3-none-any.whl | grep py.typed; echo $status
        0  2024-11-25 21:25   streamlit/py.typed
0

(I mention streamlit because it does not have this issue, both gradio and mesop do, even though they also include types in their source files).

silverjam avatar Dec 07 '24 07:12 silverjam

@silverjam we had the same problem (missing stubs) with a private multi-packages repository of us. Your comment gave us the solution (no more Pyright "Stub file not found"). Thanks!

=> I can confirm it was as simple as described: create an empty py.typed and make sure it's included in the package files (in [tool.setuptools.package-data] for us).

ref: https://typing.readthedocs.io/en/latest/spec/distributing.html#packaging-type-information & https://peps.python.org/pep-0561/#packaging-type-information

0gust1 avatar Dec 09 '24 14:12 0gust1

Thanks @silverjam and all, just opened a PR to this effect: https://github.com/gradio-app/gradio/pull/10167. Will test to see if this fixes the issue.

abidlabs avatar Dec 10 '24 17:12 abidlabs