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

Nested functions with generic type annotations using python 3.12 syntax is not supported #10736

Open
1 task done
JackismyShephard opened this issue Mar 5, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@JackismyShephard
Copy link
Contributor

Describe the bug

The easiest way to explain this issue is with an example of a concrete failing function:

def confirmation_harness[T](x: T) -> Callable[..., T]:

    def _wrapped_fn() -> T:
        return x

    return _wrapped_fn

In this case gradio errors out with NameError: name 'T' is not defined because the occurrence of T in the inner function is not recognized.

A workaround is to use the old pre-3.12 way where T is defined as a stand alone variable like this:

....
T = TypeVar('T')
def confirmation_harness(x: T) -> Callable[..., T]:

    def _wrapped_fn() -> T:
        return x

    return _wrapped_fn

while this works it is not the recommended way of defining generics anymore, so I think the new way should be supported

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

import gradio as gr

Screenshot

No response

Logs

Traceback (most recent call last):
  File "C:\Users\Jacki\repositories\ultimate-rvc\src\ultimate_rvc\web\main.py", line 452, in <module>
    app = render_app()
          ^^^^^^^^^^^^
  File "C:\Users\Jacki\repositories\ultimate-rvc\src\ultimate_rvc\web\main.py", line 403, in render_app
    render_manage_audio_tab(
  File "C:\Users\Jacki\repositories\ultimate-rvc\src\ultimate_rvc\web\tabs\manage\audio.py", line 294, in render
    all_audio_click = all_audio_btn.click(
                      ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Jacki\repositories\ultimate-rvc\uv\.venv\Lib\site-packages\gradio\events.py", line 670, in event_trigger
    dep, dep_index = root_block.set_event_trigger(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Jacki\repositories\ultimate-rvc\uv\.venv\Lib\site-packages\gradio\blocks.py", line 803, in set_event_trigger
    check_function_inputs_match(fn, inputs, inputs_as_dict)
  File "C:\Users\Jacki\repositories\ultimate-rvc\uv\.venv\Lib\site-packages\gradio\utils.py", line 1007, in check_function_inputs_match
    parameter_types = get_type_hints(fn)
                      ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Jacki\repositories\ultimate-rvc\uv\.venv\Lib\site-packages\gradio\utils.py", line 974, in get_type_hints
    return typing.get_type_hints(fn)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Jacki\AppData\Local\Programs\Python\Python312\Lib\typing.py", line 2310, in get_type_hints
    hints[name] = _eval_type(value, globalns, localns, type_params)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Jacki\AppData\Local\Programs\Python\Python312\Lib\typing.py", line 415, in _eval_type
    return t._evaluate(globalns, localns, type_params, recursive_guard=recursive_guard)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Jacki\AppData\Local\Programs\Python\Python312\Lib\typing.py", line 947, in _evaluate
    eval(self.__forward_code__, globalns, localns),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
NameError: name 'T' is not defined

System Info

windows 11
python 3.12
gradio 5.20.0

Severity

I can work around it

@JackismyShephard JackismyShephard added the bug Something isn't working label Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant