adf0d17497
publish / version_or_publish (push) Has been cancelled
storybook-build / changes (push) Has been cancelled
storybook-build / :storybook-build (push) Has been cancelled
Sync Gradio Skills to Hugging Face / sync-skills (push) Has been cancelled
functional / changes (push) Has been cancelled
functional / build-frontend (push) Has been cancelled
functional / functional-test-SSR=false (push) Has been cancelled
functional / functional-reload (push) Has been cancelled
js / changes (push) Has been cancelled
js / js-test (push) Has been cancelled
docs-build / changes (push) Has been cancelled
docs-build / docs-build (push) Has been cancelled
docs-build / website-build (push) Has been cancelled
functional / functional-test-SSR=true (push) Has been cancelled
hygiene / hygiene-test (push) Has been cancelled
python / changes (push) Has been cancelled
python / build (push) Has been cancelled
python / test-ubuntu-latest-flaky (push) Has been cancelled
python / test-ubuntu-latest-not-flaky (push) Has been cancelled
python / test-windows-latest-flaky (push) Has been cancelled
python / test-windows-latest-not-flaky (push) Has been cancelled
78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
import dataclasses
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
import gradio as gr
|
|
from gradio.cli.commands.reload import _setup_config
|
|
from gradio.http_server import Server
|
|
|
|
|
|
def build_demo():
|
|
with gr.Blocks() as demo:
|
|
gr.Textbox("")
|
|
|
|
return demo
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class Config:
|
|
module_name: str
|
|
path: Path
|
|
watch_dirs: list[str]
|
|
demo_name: str
|
|
|
|
|
|
class TestReload:
|
|
@pytest.fixture(autouse=True)
|
|
def argv(self):
|
|
return ["demo/calculator/run.py"]
|
|
|
|
@pytest.fixture
|
|
def config(self, monkeypatch, argv) -> Config:
|
|
monkeypatch.setattr("sys.argv", ["gradio"] + argv)
|
|
name = argv[1].replace("--demo-name", "").strip() if len(argv) > 1 else "demo"
|
|
return Config(*_setup_config(argv[0]), demo_name=name) # ty: ignore error[parameter-already-assigned]
|
|
|
|
@pytest.fixture(params=[{}])
|
|
def reloader(self, config):
|
|
reloader = Server(config)
|
|
reloader.should_exit = True
|
|
yield reloader
|
|
reloader.close()
|
|
|
|
def test_config_default_app(self, config):
|
|
assert config.module_name == "demo.calculator.run"
|
|
|
|
@pytest.mark.parametrize("argv", [["demo/calculator/run.py", "--demo-name test"]])
|
|
def test_config_custom_app(self, config):
|
|
assert config.module_name == "demo.calculator.run"
|
|
assert config.demo_name == "test"
|
|
|
|
def test_config_watch_app(self, config):
|
|
demo_dir = str(Path("demo/calculator/run.py").resolve().parent)
|
|
assert demo_dir in config.watch_dirs
|
|
|
|
|
|
def test_watchfn_does_not_inherit_future_annotations():
|
|
"""The watchfn function must not carry CO_FUTURE_ANNOTATIONS.
|
|
|
|
When a module uses `from __future__ import annotations`, every
|
|
exec(source_string, ...) called from that module inherits the flag and
|
|
stringifies all annotations in the exec'd code. This breaks libraries
|
|
(e.g. langgraph) that call get_type_hints() on user-defined classes with
|
|
Annotated types during hot reload.
|
|
|
|
Regression test for https://github.com/gradio-app/gradio/issues/12090
|
|
"""
|
|
import __future__
|
|
|
|
from gradio.utils import watchfn
|
|
|
|
flag = __future__.annotations.compiler_flag
|
|
assert not (watchfn.__code__.co_flags & flag), (
|
|
"watchfn has CO_FUTURE_ANNOTATIONS set. "
|
|
"Remove `from __future__ import annotations` from gradio/utils.py "
|
|
"to prevent exec() from stringifying user annotations during reload."
|
|
)
|