Skip to content

Commit 093ec6e

Browse files
authored
Merge pull request #224 from ynput/enhancement/175-type-hints-in-serverapi-and-global-api
Chore: Type hints in ServerAPI and global api functions
2 parents f339c6f + 06f1374 commit 093ec6e

File tree

7 files changed

+2709
-1956
lines changed

7 files changed

+2709
-1956
lines changed

automated_api.py

+34-3
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)

ayon_api/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
get_project_anatomy_presets,
123123
get_default_anatomy_preset_name,
124124
get_project_anatomy_preset,
125+
get_built_in_anatomy_preset,
125126
get_build_in_anatomy_preset,
126127
get_project_root_overrides,
127128
get_project_roots_by_site,
@@ -366,6 +367,7 @@
366367
"get_project_anatomy_presets",
367368
"get_default_anatomy_preset_name",
368369
"get_project_anatomy_preset",
370+
"get_built_in_anatomy_preset",
369371
"get_build_in_anatomy_preset",
370372
"get_project_root_overrides",
371373
"get_project_roots_by_site",

0 commit comments

Comments
 (0)