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

Add py.typed file to gradio package #6396

Closed
1 task done
aleneum opened this issue Nov 13, 2023 · 3 comments · Fixed by #6455
Closed
1 task done

Add py.typed file to gradio package #6396

aleneum opened this issue Nov 13, 2023 · 3 comments · Fixed by #6455
Labels
enhancement New feature or request

Comments

@aleneum
Copy link
Contributor

aleneum commented Nov 13, 2023

  • I have searched to see if a similar issue already exists.

Is your feature request related to a problem? Please describe.
Whenever I use mypy I will get the following warning:

Skipping analyzing "gradio": module is installed, but missing library stubs or py.typed marker

As far as I know, there are no Gradio stubs and Gradio does not contain a py.typed file as well.
A common workaround for untyped packages is to tell mypy to not follow such imports:

[mypy-gradio.*]
ignore_missing_imports = True

However, when disallow_any_unimported is enabled, mypy cannot type check functions with Gradio parameters:

Argument 2 to "upload" becomes "Any | None" due to an unfollowed import  [no-any-unimported]

My current solution is to allow any imports whenever Gradio is used. However, as most gradio imports seem to be typed, why not just add a py.typed file to the package to be PEP 561 compliant? This would make it easier to spot typing issues when working with Gradio.

Describe the solution you'd like
Add an empty file py.typed to the package (<project>/gradio) and include it via:

[tool.setuptools.package-data]
gradio = ["py.typed"]

I am happy to create a PR for this (hopefully trivial) change when you consider this useful.

@abidlabs
Copy link
Member

Thanks @aleneum this seems like a helpful addition -- please go ahead and create the PR!

@abidlabs abidlabs added the enhancement New feature or request label Nov 13, 2023
@aleneum
Copy link
Contributor Author

aleneum commented Nov 14, 2023

Hello @abidlabs,

it is (of course) not as straight forward as I have hoped for.

Problem 1: Adding a py.typed file to gradio will break pip install . (from source) since necessary pyi-stubs are generated only after gradio has been imported in format_backend.sh.

Solution: I could create/touch 'py.typed' the moment a pyi file is generated or (probably easier to maintain) inside format_backend.sh.

Problem 2: In templates.py, there are multiple classes commented out. The problem is that some/all of them contain an attribute named 'type'. mypy (and maybe other type checkers) use the annotation '# type:' for inline configuration. Consequently, a commented out type annotation such as # type: int will cause syntax issues ref.

Solution: I could comment out the relevant classes in templates.py TWICE and add a remark concerning this issue. The mentioned commits above did that.

A generated gradio package including pyi files, py.typed and changes to templates.py works though. Let me know what you think and what would be in the best interest of the project.

@abidlabs
Copy link
Member

Hi @aleneum thanks for investigating this! I think both of those solutions are reasonable. Could you add the py.typed files in the scripts/type_check_backend.sh which is called from within format_backend.sh? We might also need to ensure that gradio is imported in our CI when we install Gradio from source (i.e. in .github/workflows/)

And also could you also add a py.typed file for the gradio_client package which is located at: client/python/?

aleneum added a commit to aleneum/gradio that referenced this issue Nov 16, 2023
abidlabs added a commit that referenced this issue Nov 16, 2023
* comment out classes in templates TWICE
see #6396 for details

* add py.typed to hatch artifacts

* add py.typed to gitignore

* create py.typed in type_check_backend

* add changeset

* add changeset

---------

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants