Skip to content

Commit 3dfc17e

Browse files
authored
Merge pull request #17 from comfuture/runtime-typecheck
2 parents 3502f3d + abbbda3 commit 3dfc17e

File tree

2 files changed

+7
-19
lines changed

2 files changed

+7
-19
lines changed

function_schema/types.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TypedDict, Literal
1+
from typing import TypedDict, Literal, Protocol, runtime_checkable
22

33
try:
44
from typing import NotRequired
@@ -15,7 +15,10 @@
1515
from typing_extensions import Doc
1616
except ImportError:
1717

18-
class Doc:
18+
@runtime_checkable
19+
class Doc(Protocol):
20+
documentation: str
21+
1922
def __init__(self, documentation: str, /):
2023
self.documentation = documentation
2124

function_schema/utils.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Annotated, Any, Union
1+
from typing import Annotated, Union
22
from .types import Doc
33

44

@@ -11,21 +11,6 @@ def is_support_uniontype():
1111
return True
1212

1313

14-
def is_doc_meta(
15-
obj: Annotated[Any, Doc("The object to be checked.")],
16-
) -> Annotated[
17-
bool, Doc("True if the object is a documentation object, False otherwise.")
18-
]:
19-
"""
20-
Check if the given object is a documentation object.
21-
22-
Example:
23-
>>> is_doc_meta(Doc("This is a documentation object"))
24-
True
25-
"""
26-
return getattr(obj, "__class__") == Doc and hasattr(obj, "documentation")
27-
28-
2914
def unwrap_doc(
3015
obj: Annotated[
3116
Union[Doc, str], Doc(
@@ -41,6 +26,6 @@ def unwrap_doc(
4126
>>> unwrap_doc("This is a documentation string")
4227
'This is a documentation string'
4328
"""
44-
if is_doc_meta(obj):
29+
if isinstance(obj, Doc):
4530
return obj.documentation
4631
return str(obj)

0 commit comments

Comments
 (0)