Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions function_schema/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TypedDict, Literal
from typing import TypedDict, Literal, Protocol, runtime_checkable

try:
from typing import NotRequired
Expand All @@ -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

Expand Down
19 changes: 2 additions & 17 deletions function_schema/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Annotated, Any, Union
from typing import Annotated, Union
from .types import Doc


Expand All @@ -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(
Expand All @@ -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)