Skip to content

Commit e070fca

Browse files
committed
fix non existing module types in python<3.10
1 parent 107290a commit e070fca

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

function_schema/core.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import enum
22
import typing
3-
import types
43
import inspect
4+
import platform
5+
import packaging.version
6+
7+
current_version = packaging.version.parse(platform.python_version())
8+
py_310 = packaging.version.parse("3.10")
9+
10+
if current_version >= py_310:
11+
import types
12+
from types import UnionType
13+
else:
14+
UnionType = typing.Union # type: ignore
515

616

717
def get_function_schema(
@@ -139,7 +149,7 @@ def guess_type(
139149
origin = typing.get_origin(T)
140150

141151
# hacking around typing modules, `typing.Union` and `types.UnitonType`
142-
if origin is typing.Union or origin is types.UnionType:
152+
if origin is typing.Union or origin is UnionType:
143153
union_types = [t for t in typing.get_args(T) if t is not type(None)]
144154
_types = []
145155
for union_type in union_types:

0 commit comments

Comments
 (0)