|
3 | 3 | import asyncio
|
4 | 4 | import inspect
|
5 | 5 | from collections import defaultdict
|
| 6 | +from copy import deepcopy |
6 | 7 | from typing import TYPE_CHECKING, Any, Callable, get_type_hints
|
7 | 8 |
|
8 | 9 | import pytest
|
@@ -82,9 +83,25 @@ def patch(
|
82 | 83 | if not client_method:
|
83 | 84 | raise ValueError(f'ApifyClientAsync does not contain method "{method}"!')
|
84 | 85 |
|
85 |
| - # TODO: This is the problematic line |
86 |
| - # https://github.com/apify/apify-sdk-python/issues/151 |
87 |
| - client_method_return_type = get_type_hints(client_method)['return'] |
| 86 | + try: |
| 87 | + # Try to get the return type of the client method using `typing.get_type_hints()` |
| 88 | + client_method_return_type = get_type_hints(client_method)['return'] |
| 89 | + except TypeError: |
| 90 | + # There is a known issue with `typing.get_type_hints()` on Python 3.8 and 3.9. It raises a `TypeError` |
| 91 | + # when `|` (Union) is used in the type hint, even with `from __future__ import annotations`. Since we |
| 92 | + # only need the return type, we attempt the following workaround. |
| 93 | + |
| 94 | + # 1. Create a deep copy of the client method object |
| 95 | + client_method_copied = deepcopy(client_method) |
| 96 | + |
| 97 | + # 2. Restrict the annotations to only include the return type |
| 98 | + client_method_copied.__annotations__ = {'return': client_method.__annotations__['return']} |
| 99 | + |
| 100 | + # 3. Try to get the return type again using `typing.get_type_hints()` |
| 101 | + client_method_return_type = get_type_hints(client_method_copied)['return'] |
| 102 | + |
| 103 | + # TODO: Remove this fallback once we drop support for Python 3.8 and 3.9 |
| 104 | + # https://github.com/apify/apify-sdk-python/issues/151 |
88 | 105 |
|
89 | 106 | original_submethod = getattr(client_method_return_type, submethod, None)
|
90 | 107 |
|
|
0 commit comments