From 6dfd40fc6b2fa461490d2370ab91fcda7e07c0da Mon Sep 17 00:00:00 2001 From: Freddy Boulton Date: Fri, 9 Feb 2024 16:53:21 -0800 Subject: [PATCH] Make set_documentation_group a no-op (#7377) * push * Use ( * add changeset * add changeset * fix --------- Co-authored-by: gradio-pr-bot Co-authored-by: Abubakar Abid --- .changeset/rare-months-guess.md | 6 ++++++ client/python/gradio_client/documentation.py | 15 +++++++++++++-- .../cli/commands/components/_create_utils.py | 18 ++++++++++++------ test/test_gradio_component_cli.py | 4 ++++ 4 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 .changeset/rare-months-guess.md diff --git a/.changeset/rare-months-guess.md b/.changeset/rare-months-guess.md new file mode 100644 index 0000000000..1f25ad1385 --- /dev/null +++ b/.changeset/rare-months-guess.md @@ -0,0 +1,6 @@ +--- +"gradio": patch +"gradio_client": patch +--- + +feat:Make set_documentation_group a no-op diff --git a/client/python/gradio_client/documentation.py b/client/python/gradio_client/documentation.py index 16dec9be7c..6530bd8d96 100644 --- a/client/python/gradio_client/documentation.py +++ b/client/python/gradio_client/documentation.py @@ -12,6 +12,11 @@ classes_to_document = defaultdict(list) classes_inherit_documentation = {} +def set_documentation_group(m): # noqa: ARG001 + """A no-op for backwards compatibility of custom components published prior to 4.16.0""" + pass + + def extract_instance_attr_doc(cls, attr): code = inspect.getsource(cls.__init__) lines = [line.strip() for line in code.split("\n")] @@ -51,7 +56,7 @@ _module_prefixes = [ ("gradio.layout", "layout"), ("gradio.route", "routes"), ("gradio.theme", "themes"), - ("gradio_client", "py-client"), + ("gradio_client.", "py-client"), ] @@ -86,7 +91,13 @@ def document(*fns, inherit=False, documentation_group=None): if _documentation_group is None: try: modname = inspect.getmodule(cls).__name__ # type: ignore - documentation_group = _get_module_documentation_group(modname) + if modname.startswith("gradio.") or modname.startswith( + "gradio_client." + ): + documentation_group = _get_module_documentation_group(modname) + else: + # Then this is likely a custom Gradio component that we do not include in the documentation + pass except Exception as exc: warnings.warn(f"Could not get documentation group for {cls}: {exc}") classes_to_document[documentation_group].append((cls, functions)) diff --git a/gradio/cli/commands/components/_create_utils.py b/gradio/cli/commands/components/_create_utils.py index d5b58dab8c..c23747fdea 100644 --- a/gradio/cli/commands/components/_create_utils.py +++ b/gradio/cli/commands/components/_create_utils.py @@ -287,6 +287,12 @@ def _replace_old_class_name(old_class_name: str, new_class_name: str, content: s return re.sub(pattern, new_class_name, content) +def _strip_document_lines(content: str): + return "\n".join( + [line for line in content.split("\n") if not line.startswith("@document(")] + ) + + def _create_backend( name: str, component: ComponentFiles, directory: Path, package_name: str ): @@ -388,11 +394,11 @@ __all__ = ['{name}'] shutil.copy(str(source_pyi_file), str(pyi_file)) content = python_file.read_text() - python_file.write_text( - _replace_old_class_name(correct_cased_template, name, content) - ) + content = _replace_old_class_name(correct_cased_template, name, content) + content = _strip_document_lines(content) + python_file.write_text(content) if pyi_file.exists(): pyi_content = pyi_file.read_text() - pyi_file.write_text( - _replace_old_class_name(correct_cased_template, name, pyi_content) - ) + pyi_content = _replace_old_class_name(correct_cased_template, name, content) + pyi_content = _strip_document_lines(pyi_content) + pyi_file.write_text(pyi_content) diff --git a/test/test_gradio_component_cli.py b/test/test_gradio_component_cli.py index 7cd732b70e..bdc53a1c96 100644 --- a/test/test_gradio_component_cli.py +++ b/test/test_gradio_component_cli.py @@ -58,6 +58,10 @@ if __name__ == "__main__": ) assert app.strip() == answer.strip() assert (tmp_path / "backend" / "gradio_mycomponent" / "mycomponent.py").exists() + source_code = ( + tmp_path / "backend" / "gradio_mycomponent" / "mycomponent.py" + ).read_text() + assert "@document()" not in source_code def test_raise_error_component_template_does_not_exist(tmp_path):