|
20 | 20 | import typing
|
21 | 21 |
|
22 | 22 | # 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") |
25 | 31 |
|
26 | 32 | import ayon_api # noqa: E402
|
27 | 33 | from ayon_api.server_api import ServerAPI, _PLACEHOLDER # noqa: E402
|
@@ -116,7 +122,32 @@ def prepare_docstring(func):
|
116 | 122 |
|
117 | 123 | def _get_typehint(annotation, api_globals):
|
118 | 124 | 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 |
120 | 151 |
|
121 | 152 | typehint = (
|
122 | 153 | str(annotation)
|
|
0 commit comments