Skip to content

Commit e38c0b5

Browse files
committed
fix: Do not mask unrelated import errors when guarding optional dependencies
1 parent 31f7854 commit e38c0b5

4 files changed

Lines changed: 54 additions & 8 deletions

File tree

‎src/apify_client/_utils/try_import.py‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,29 @@
1111
from typing import Any
1212

1313

14+
@dataclass
15+
class ImportState:
16+
"""Describe whether an optional import succeeded."""
17+
18+
available: bool = True
19+
20+
1421
@contextmanager
15-
def try_import(module_name: str, *symbol_names: str) -> Iterator[None]:
22+
def try_import(module_name: str, *symbol_names: str, dependency_name: str) -> Iterator[ImportState]:
1623
"""Context manager to attempt importing symbols into a module.
1724
18-
If an `ImportError` is raised during the import, the symbol will be replaced with a `FailedImport` object.
25+
If the named optional dependency is missing, the symbols are replaced with `FailedImport` objects. Import errors
26+
caused by the importing module itself or by another dependency are propagated instead of being masked.
1927
"""
28+
state = ImportState()
2029
try:
21-
yield
22-
except ImportError as e:
30+
yield state
31+
except ModuleNotFoundError as exc:
32+
if exc.name != dependency_name:
33+
raise
34+
state.available = False
2335
for symbol_name in symbol_names:
24-
setattr(sys.modules[module_name], symbol_name, FailedImport(e.args[0]))
36+
setattr(sys.modules[module_name], symbol_name, FailedImport(exc.args[0]))
2537

2638

2739
def install_import_hook(module_name: str) -> None:

‎src/apify_client/http_compressors/__init__.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
# `brotli` is an optional extra, so it's wrapped in try_import. Accessing `BrotliHttpCompressor`
1111
# without the extra installed raises a clear ImportError instead of failing at package import time.
12-
with _try_import(__name__, 'BrotliHttpCompressor'):
12+
with _try_import(__name__, 'BrotliHttpCompressor', dependency_name='brotli') as _brotli_import:
1313
from apify_client.http_compressors._brotli import BrotliHttpCompressor
1414

15-
__all__ = ['BrotliHttpCompressor', 'GzipHttpCompressor', 'HttpCompressor']
15+
if _brotli_import.available:
16+
__all__ = ['BrotliHttpCompressor', 'GzipHttpCompressor', 'HttpCompressor']
17+
else:
18+
__all__ = ['GzipHttpCompressor', 'HttpCompressor']

‎tests/unit/test_http_compressors.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def _brotli_unavailable() -> Iterator[None]:
3535
class _Blocker:
3636
def find_spec(self, name: str, *_args: object) -> None:
3737
if name == 'brotli' or name.startswith('brotli.'):
38-
raise ModuleNotFoundError(f"No module named '{name}'")
38+
raise ModuleNotFoundError(f"No module named '{name}'", name='brotli')
3939

4040
def _affected(name: str) -> bool:
4141
return name == 'brotli' or name.startswith(('brotli.', 'apify_client.http_compressors'))

‎tests/unit/test_utils.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import gzip
44
import io
55
import json
6+
import sys
67
from base64 import b64decode
78
from datetime import timedelta
89
from http import HTTPStatus
10+
from types import ModuleType
911
from typing import TYPE_CHECKING, Any
1012
from unittest.mock import Mock
1113

@@ -23,6 +25,7 @@
2325
response_to_list,
2426
to_safe_id,
2527
)
28+
from apify_client._utils.try_import import FailedImport, try_import
2629
from apify_client.errors import ApifyApiError, InvalidResponseBodyError
2730

2831
if TYPE_CHECKING:
@@ -32,6 +35,34 @@
3235
_GZIPPED_DATA = gzip.compress(b'buffer data')
3336

3437

38+
def test_try_import_only_handles_the_named_missing_dependency() -> None:
39+
"""Optional-import handling must not hide a broken transitive dependency or module bug."""
40+
module_name = 'test_optional_import_module'
41+
sys.modules[module_name] = ModuleType(module_name)
42+
try:
43+
with (
44+
pytest.raises(ModuleNotFoundError, match='broken-transitive'),
45+
try_import(module_name, 'OptionalSymbol', dependency_name='optional-package'),
46+
):
47+
raise ModuleNotFoundError('broken-transitive', name='broken-transitive')
48+
assert not hasattr(sys.modules[module_name], 'OptionalSymbol')
49+
finally:
50+
del sys.modules[module_name]
51+
52+
53+
def test_try_import_records_the_named_missing_dependency() -> None:
54+
"""A genuinely missing optional dependency is converted to a failed-import placeholder."""
55+
module_name = 'test_missing_optional_import_module'
56+
sys.modules[module_name] = ModuleType(module_name)
57+
try:
58+
with try_import(module_name, 'OptionalSymbol', dependency_name='optional-package') as state:
59+
raise ModuleNotFoundError("No module named 'optional-package'", name='optional-package')
60+
assert state.available is False
61+
assert isinstance(sys.modules[module_name].OptionalSymbol, FailedImport)
62+
finally:
63+
del sys.modules[module_name]
64+
65+
3566
def test_to_safe_id() -> None:
3667
assert to_safe_id('abc') == 'abc'
3768
assert to_safe_id('abc/def') == 'abc~def'

0 commit comments

Comments
 (0)