tip + patch to solve typing

#2
by SNVBX - opened

I had recurring errors like the following:
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'

For some reason I couldn't make a PR so here's a patch for that:

commit 7525fadc4a301f4ed16ac90dba2b3a6d0a09de7b
Author: redacted
Date:   Wed Apr 24 13:15:16 2024 +0200

    fix typing

diff --git a/app.py b/app.py
index 96dffae..c28c6d2 100644
--- a/app.py
+++ b/app.py
@@ -1,5 +1,7 @@
 import os
 
+from typing import Union
+
 import gradio as gr
 
 from gradio_highlightedtextbox import HighlightedTextbox
@@ -181,20 +183,20 @@ abs_path = os.path.join(os.path.dirname(__file__), "css.css")
 
 def convert_tagged_text_to_highlighted_text(
     tagged_text: str,
-    tag_id: str | list[str],
-    tag_open: str | list[str],
-    tag_close: str | list[str],
-) -> list[tuple[str, str | None]]:
+    tag_id: Union[str, list[str]],
+    tag_open: Union[str, list[str]],
+    tag_close: Union[str, list[str]],
+) -> list[tuple[str, Union[str, None]]]:
     return HighlightedTextbox.tagged_text_to_tuples(
         tagged_text, tag_id, tag_open, tag_close
     )
 
 
 def convert_highlighted_text_to_tagged_text(
-    highlighted_text: dict[str, str | list[tuple[str, str | None]]],
-    tag_id: str | list[str],
-    tag_open: str | list[str],
-    tag_close: str | list[str],
+    highlighted_text: dict[str, Union[str, list[tuple[str, Union[str, None]]]]],
+    tag_id: Union[str, list[str]],
+    tag_open: Union[str, list[str]],
+    tag_close: Union[str, list[str]],
 ) -> str:
     return HighlightedTextbox.tuples_to_tagged_text(
         highlighted_text["data"], tag_id, tag_open, tag_close
@@ -202,10 +204,10 @@ def convert_highlighted_text_to_tagged_text(
 
 
 def show_info(
-    highlighted_text: dict[str, str | list[tuple[str, str | None]]],
-    tag_id: str | list[str],
-    tag_open: str | list[str],
-    tag_close: str | list[str],
+    highlighted_text: dict[str, Union[str, list[tuple[str, Union[str, None]]]]],
+    tag_id: Union[str, list[str]],
+    tag_open: Union[str, list[str]],
+    tag_close: Union[str, list[str]],
     msg: str,
 ) -> None:
     gr.Info(
diff --git a/src/backend/gradio_highlightedtextbox/highlightedtextbox.py b/src/backend/gradio_highlightedtextbox/highlightedtextbox.py
index 87ab6bc..800ce63 100644
--- a/src/backend/gradio_highlightedtextbox/highlightedtextbox.py
+++ b/src/backend/gradio_highlightedtextbox/highlightedtextbox.py
@@ -1,7 +1,7 @@
 from __future__ import annotations
 
 import re
-from typing import Any, Callable
+from typing import Any, Callable, Union
 
 from gradio.components.base import FormComponent
 from gradio.data_classes import GradioRootModel
@@ -9,7 +9,7 @@ from gradio.events import Events
 
 
 class HighlightedTextData(GradioRootModel):
-    root: list[tuple[str, str | None]]
+    root: list[tuple[str, Union[str, None]]]
 
 
 class HighlightedTextbox(FormComponent):
@@ -165,10 +165,10 @@ class HighlightedTextbox(FormComponent):
     def tagged_text_to_tuples(
         cls,
         text: str,
-        tag_ids: str | list[str],
-        tags_open: str | list[str] = "<h>",
-        tags_close: str | list[str] = "</h>",
-    ) -> list[tuple[str, str | None]]:
+        tag_ids: Union[str,  list[str]],
+        tags_open: Union[str,  list[str]] = "<h>",
+        tags_close: Union[str,  list[str]] = "</h>",
+    ) -> list[tuple[str, Union[str, None]]]:
         """Parse a text containing tags into a list of tuples in the format accepted by HighlightedTextbox.
 
         E.g. Hello <h>world</h>! -> [("Hello", None), ("world", <TAG_ID>), ("!", None)]
diff --git a/src/backend/gradio_highlightedtextbox/highlightedtextbox.pyi b/src/backend/gradio_highlightedtextbox/highlightedtextbox.pyi
index 207a966..8419202 100644
--- a/src/backend/gradio_highlightedtextbox/highlightedtextbox.pyi
+++ b/src/backend/gradio_highlightedtextbox/highlightedtextbox.pyi
@@ -166,10 +166,10 @@ class HighlightedTextbox(FormComponent):
     def tagged_text_to_tuples(
         cls,
         text: str,
-        tag_ids: str | list[str],
-        tags_open: str | list[str] = "<h>",
-        tags_close: str | list[str] = "</h>",
-    ) -> list[tuple[str, str | None]]:
+        tag_ids: Union[str,  list[str]],
+        tags_open: Union[str,  list[str]] = "<h>",
+        tags_close: Union[str,  list[str]] = "</h>",
+    ) -> list[tuple[str, Union[str, None]]]:
         """Parse a text containing tags into a list of tuples in the format accepted by HighlightedTextbox.
 
         E.g. Hello <h>world</h>! -> [("Hello", None), ("world", <TAG_ID>), ("!", None)]

Also, you might have issues with your path when importing the component. You might want to use a try block to try several ways to import the custom component to be sure that it works for everyone (user that cloned, the docker, etc)

Owner

Hi @SNVBX , thank you for your message. The issue you highlight is due to your Python version: if you're below 3.9, the type format specified here might cause problems. In general, you might want to update your Python version, provided most libraries have limited support for versions below 3.9 at this time.

Regarding the path issue, you should not have any such problem when pip-installing the component with pip install gradio_highlightedtextbox. Were you trying to use the local files in this demo instead?

Thanks for the quick response.

I was on python 3.9.19 when I tried earlier today so it's not that. I think it might be due to pydantic versions but given that it was not part of the requirements I wasn't sure. I have pydantic==2.5.3 and pydantic_core==2.14.6

Regardless I had forgotten that I could install custom components via pip and instead was trying to run the files I cloned from this space.

Sign up or log in to comment