Skip to content

Commit

Permalink
Make set_documentation_group a no-op (gradio-app#7377)
Browse files Browse the repository at this point in the history
* push

* Use (

* add changeset

* add changeset

* fix

---------

Co-authored-by: gradio-pr-bot <[email protected]>
Co-authored-by: Abubakar Abid <[email protected]>
  • Loading branch information
3 people authored Feb 10, 2024
1 parent 7f19ba2 commit 6dfd40f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/rare-months-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"gradio": patch
"gradio_client": patch
---

feat:Make set_documentation_group a no-op
15 changes: 13 additions & 2 deletions client/python/gradio_client/documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
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")]
Expand Down Expand Up @@ -51,7 +56,7 @@ def extract_instance_attr_doc(cls, attr):
("gradio.layout", "layout"),
("gradio.route", "routes"),
("gradio.theme", "themes"),
("gradio_client", "py-client"),
("gradio_client.", "py-client"),
]


Expand Down Expand Up @@ -86,7 +91,13 @@ def inner_doc(cls):
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))
Expand Down
18 changes: 12 additions & 6 deletions gradio/cli/commands/components/_create_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down Expand Up @@ -388,11 +394,11 @@ def find_template_in_list(template, list_to_search):
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)
4 changes: 4 additions & 0 deletions test/test_gradio_component_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def test_template_override_component(template, tmp_path):
)
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):
Expand Down

0 comments on commit 6dfd40f

Please sign in to comment.