diff --git a/function_schema/types.py b/function_schema/types.py index a67fe85..ddab827 100644 --- a/function_schema/types.py +++ b/function_schema/types.py @@ -1,4 +1,4 @@ -from typing import TypedDict, Literal +from typing import TypedDict, Literal, Protocol, runtime_checkable try: from typing import NotRequired @@ -15,7 +15,10 @@ from typing_extensions import Doc except ImportError: - class Doc: + @runtime_checkable + class Doc(Protocol): + documentation: str + def __init__(self, documentation: str, /): self.documentation = documentation diff --git a/function_schema/utils.py b/function_schema/utils.py index 4ef1c4c..1243d99 100644 --- a/function_schema/utils.py +++ b/function_schema/utils.py @@ -1,4 +1,4 @@ -from typing import Annotated, Any, Union +from typing import Annotated, Union from .types import Doc @@ -11,21 +11,6 @@ def is_support_uniontype(): return True -def is_doc_meta( - obj: Annotated[Any, Doc("The object to be checked.")], -) -> Annotated[ - bool, Doc("True if the object is a documentation object, False otherwise.") -]: - """ - Check if the given object is a documentation object. - - Example: - >>> is_doc_meta(Doc("This is a documentation object")) - True - """ - return getattr(obj, "__class__") == Doc and hasattr(obj, "documentation") - - def unwrap_doc( obj: Annotated[ Union[Doc, str], Doc( @@ -41,6 +26,6 @@ def unwrap_doc( >>> unwrap_doc("This is a documentation string") 'This is a documentation string' """ - if is_doc_meta(obj): + if isinstance(obj, Doc): return obj.documentation return str(obj)