Skip to content

Commit da1782c

Browse files
Copilotcomfuture
andcommitted
Fix Python 3.9 compatibility issue with isinstance and subscripted generics
Co-authored-by: comfuture <[email protected]>
1 parent d345923 commit da1782c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

function_schema/core.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
UnionType = Union # type: ignore
2323

2424

25+
def is_optional_type(T: Any) -> bool:
26+
"""Check if a type annotation represents an optional/nullable type."""
27+
origin = get_origin(T)
28+
if origin is Union or origin is UnionType:
29+
args = get_args(T)
30+
return type(None) in args
31+
return False
32+
33+
2534
__all__ = ("get_function_schema", "guess_type", "Doc", "Annotated")
2635

2736

@@ -146,7 +155,7 @@ def get_function_schema(
146155

147156
if (
148157
get_origin(T) is not Literal
149-
and not isinstance(None, T)
158+
and not is_optional_type(T)
150159
and default_value is inspect._empty
151160
):
152161
schema["required"].append(name)

0 commit comments

Comments
 (0)