Skip to content

Commit e81f8ab

Browse files
committed
Add workaround for typing.get_type_hints()
1 parent eea7873 commit e81f8ab

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ requires-python = ">=3.8"
2424
dependencies = [
2525
"aiofiles >= 22.1.0",
2626
"aioshutil >= 1.0",
27-
"apify-client == 1.6.1a2",
27+
"apify-client ~= 1.6.0",
2828
"apify-shared ~= 1.1.0",
2929
"colorama >= 0.4.6",
3030
"cryptography >= 39.0.0",

tests/unit/conftest.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import asyncio
44
import inspect
55
from collections import defaultdict
6+
from copy import deepcopy
67
from typing import TYPE_CHECKING, Any, Callable, get_type_hints
78

89
import pytest
@@ -82,9 +83,25 @@ def patch(
8283
if not client_method:
8384
raise ValueError(f'ApifyClientAsync does not contain method "{method}"!')
8485

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
88105

89106
original_submethod = getattr(client_method_return_type, submethod, None)
90107

0 commit comments

Comments
 (0)