Skip to content

Commit 09fdb9c

Browse files
committed
better requests support
1 parent 9c853a9 commit 09fdb9c

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

automated_api.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@
2020
import typing
2121

2222
# Fake modules to avoid import errors
23-
for module_name in ("requests", "unidecode"):
24-
sys.modules[module_name] = object()
23+
24+
requests = type(sys)("requests")
25+
requests.__dict__["Response"] = type(
26+
"Response", (), {"__module__": "requests"}
27+
)
28+
29+
sys.modules["requests"] = requests
30+
sys.modules["unidecode"] = type(sys)("unidecode")
2531

2632
import ayon_api # noqa: E402
2733
from ayon_api.server_api import ServerAPI, _PLACEHOLDER # noqa: E402
@@ -116,7 +122,32 @@ def prepare_docstring(func):
116122

117123
def _get_typehint(annotation, api_globals):
118124
if inspect.isclass(annotation):
119-
return annotation.__name__
125+
module_name_parts = list(str(annotation.__module__).split("."))
126+
module_name_parts.append(annotation.__name__)
127+
module_name_parts.reverse()
128+
options = []
129+
_name = None
130+
for name in module_name_parts:
131+
if _name is None:
132+
_name = name
133+
options.append(name)
134+
else:
135+
_name = f"{name}.{_name}"
136+
options.append(_name)
137+
138+
options.reverse()
139+
for option in options:
140+
try:
141+
# Test if typehint is valid for known '_api' content
142+
exec(f"_: {option} = None", api_globals)
143+
return option
144+
except NameError:
145+
pass
146+
147+
typehint = options[0]
148+
print("Unknown typehint:", typehint)
149+
typehint = f'"{typehint}"'
150+
return typehint
120151

121152
typehint = (
122153
str(annotation)
@@ -127,8 +158,9 @@ def _get_typehint(annotation, api_globals):
127158
)
128159
for item in full_path_regex.finditer(str(typehint)):
129160
groups = item.groupdict()
130-
name = groups["name"].split(".")[-1]
131-
typehint = typehint.replace(groups["full"], name)
161+
if groups["full"] not in {"io.BytesIO"}:
162+
name = groups["name"].split(".")[-1]
163+
typehint = typehint.replace(groups["full"], name)
132164

133165
forwardref_regex = re.compile(
134166
r"(?P<full>ForwardRef\('(?P<name>[a-zA-Z0-9]+)'\))"

0 commit comments

Comments
 (0)