1010from sentry_sdk .integrations import _DEFAULT_INTEGRATIONS , Integration
1111
1212
13+ def pytest_generate_tests (metafunc ):
14+ """
15+ All submodules of sentry_sdk.integrations are picked up, so modules
16+ without a subclass of sentry_sdk.integrations.Integration are also tested
17+ for poorly gated imports.
18+
19+ This approach was chosen to keep the implementation simple.
20+ """
21+ if "integration_submodule_name" in metafunc .fixturenames :
22+ submodule_names = {
23+ submodule_name
24+ for _ , submodule_name , _ in pkgutil .walk_packages (integrations .__path__ )
25+ }
26+
27+ metafunc .parametrize (
28+ "integration_submodule_name" ,
29+ # Temporarily skip some integrations
30+ submodule_names
31+ - {
32+ "clickhouse_driver" ,
33+ "grpc" ,
34+ "litellm" ,
35+ "opentelemetry" ,
36+ "pure_eval" ,
37+ "ray" ,
38+ "trytond" ,
39+ "typer" ,
40+ },
41+ )
42+
43+
1344def find_unrecognized_dependencies (tree ):
1445 """
1546 Finds unrecognized imports in the AST for a Python module. In an empty
@@ -48,7 +79,10 @@ def find_unrecognized_dependencies(tree):
4879@pytest .mark .skipif (
4980 sys .version_info < (3 , 7 ), reason = "asyncpg imports __future__.annotations"
5081)
51- def test_shadowed_modules_when_importing_integrations (sentry_init ):
82+ @pytest .mark .forked # Importing modules can cause side-effects
83+ def test_shadowed_modules_when_importing_integrations (
84+ sentry_init , integration_submodule_name
85+ ):
5286 """
5387 Check that importing integrations for third-party module raises an
5488 DidNotEnable exception when the associated module is shadowed by an empty
@@ -57,31 +91,16 @@ def test_shadowed_modules_when_importing_integrations(sentry_init):
5791 An integration is determined to be for a third-party module if it cannot
5892 be imported in the environment in which the tests run.
5993 """
60- for _ , submodule_name , _ in pkgutil .walk_packages (integrations .__path__ ):
61- module_path = f"sentry_sdk.integrations.{ submodule_name } "
62-
63- # Temporary skip list
64- if submodule_name in (
65- "clickhouse_driver" ,
66- "grpc" ,
67- "litellm" ,
68- "opentelemetry" ,
69- "pure_eval" ,
70- "ray" ,
71- "trytond" ,
72- "typer" ,
73- ):
74- continue
75-
76- try :
94+ module_path = f"sentry_sdk.integrations.{ integration_submodule_name } "
95+ try :
96+ importlib .import_module (module_path )
97+ return
98+ except integrations .DidNotEnable :
99+ spec = importlib .util .find_spec (module_path )
100+ source = pathlib .Path (spec .origin ).read_text (encoding = "utf-8" )
101+ tree = ast .parse (source , filename = spec .origin )
102+ integration_dependencies = find_unrecognized_dependencies (tree )
103+ for dependency in integration_dependencies :
104+ sys .modules [dependency ] = types .ModuleType (dependency )
105+ with pytest .raises (integrations .DidNotEnable ):
77106 importlib .import_module (module_path )
78- continue
79- except integrations .DidNotEnable :
80- spec = importlib .util .find_spec (module_path )
81- source = pathlib .Path (spec .origin ).read_text (encoding = "utf-8" )
82- tree = ast .parse (source , filename = spec .origin )
83- integration_dependencies = find_unrecognized_dependencies (tree )
84- for dependency in integration_dependencies :
85- sys .modules [dependency ] = types .ModuleType (dependency )
86- with pytest .raises (integrations .DidNotEnable ):
87- importlib .import_module (module_path )
0 commit comments