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 type hints for render and on #8429

Merged
merged 6 commits into from
Jun 3, 2024
Merged

Conversation

freddyaboulton
Copy link
Collaborator

@freddyaboulton freddyaboulton commented May 31, 2024

Description

I noticed two things that were off about the type hints for on and render

  • We were using Any for the triggers of gr.on. Which was too broad.
  • In render, the triggers were not typed correctly so passing a valid trigger, e.g. slider.change would be flagged in vs code.
  • In render, the type hint did not allow for a single component to be passed as input, but it is allowed.

Main
Screenshot 2024-05-31 at 2 09 04 PM
Screenshot 2024-05-31 at 2 03 46 PM

PR
Invalid value for triggers of gr.on is flagged
image

Passing an event listener method for render is not flagged
image

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented May 31, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
🦄 Changes detecting...

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/1c535500afa038b9170e18d9e806883323b7b2ba/gradio-4.32.2-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@1c535500afa038b9170e18d9e806883323b7b2ba#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-builds.s3.amazonaws.com/1c535500afa038b9170e18d9e806883323b7b2ba/gradio-client-0.20.1.tgz

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented May 31, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Fix type hints for render and on

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@freddyaboulton freddyaboulton marked this pull request as ready for review May 31, 2024 21:35
@abidlabs abidlabs added the v: patch A change that requires a patch release label Jun 3, 2024
inputs: list[Component] | None = None,
triggers: list[EventListener] | EventListener | None = None,
inputs: list[Component] | Component | None = None,
triggers: Sequence[EventListenerCallable] | EventListenerCallable | None = None,
Copy link
Member

Choose a reason for hiding this comment

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

The only issue with putting Sequence here is that it'll affect the docs for gr.render(): https://www.gradio.app/docs/gradio/render

Copy link
Member

Choose a reason for hiding this comment

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

And I think many Python users don't know what Sequence is

Copy link
Member

Choose a reason for hiding this comment

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

Can we add a step in the docs generation to convert Sequence container types to list @aliabd?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I can edit 01_render.svx to do this but maybe we do this once users complain? I would prefer to not introduce a difference between docs and code unless it's absolutely necessary imo.

Copy link
Member

@abidlabs abidlabs Jun 3, 2024

Choose a reason for hiding this comment

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

Ok I think this would be applicable in many other parts of the codebase as well (e.g. anywhere we currently type hint list[Component] should really be Sequence[Component]) so I don't think we should manually edit but doing it later is fine

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok I think we can edit the ParamTable svelte component in that case but will hold off for now.

Copy link
Collaborator

@aliabid94 aliabid94 left a comment

Choose a reason for hiding this comment

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

LGTM! I was trying to see if we could actually use EventListenerCallable to typehint EventListener.listener, so that if EventListenerCallable ever diverges from the actual signature of a listener, our typecheker catches it. Cou;dn't figure it out though

Copy link
Member

@abidlabs abidlabs left a comment

Choose a reason for hiding this comment

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

LGTM

@freddyaboulton
Copy link
Collaborator Author

Thanks for the reviews everyone!

@freddyaboulton freddyaboulton merged commit d393a4a into main Jun 3, 2024
7 checks passed
@freddyaboulton freddyaboulton deleted the fix-dependency-typehint branch June 3, 2024 23:33
@pngwn pngwn mentioned this pull request Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v: patch A change that requires a patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants