-
Notifications
You must be signed in to change notification settings - Fork 569
test: Import integrations with empty shadow modules #5150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f02d43a
3055936
e5bbbc2
f417060
0ea8f7e
5e6878e
535991e
18bf400
5a1459e
e68637d
2a49088
ab23a74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,6 +163,7 @@ | |
| "trytond", | ||
| "typer", | ||
| "integration_deactivation", | ||
| "shadowed_module", | ||
| ], | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import sys | ||
| import ast | ||
| import types | ||
| import pkgutil | ||
| import importlib | ||
| import pathlib | ||
| import pytest | ||
|
|
||
| from sentry_sdk import integrations | ||
| from sentry_sdk.integrations import _DEFAULT_INTEGRATIONS, Integration | ||
|
|
||
|
|
||
| def pytest_generate_tests(metafunc): | ||
| """ | ||
| All submodules of sentry_sdk.integrations are picked up, so modules | ||
| without a subclass of sentry_sdk.integrations.Integration are also tested | ||
| for poorly gated imports. | ||
|
|
||
| This approach was chosen to keep the implementation simple. | ||
| """ | ||
| if "integration_submodule_name" in metafunc.fixturenames: | ||
| submodule_names = { | ||
| submodule_name | ||
| for _, submodule_name, _ in pkgutil.walk_packages(integrations.__path__) | ||
| } | ||
|
|
||
| metafunc.parametrize( | ||
| "integration_submodule_name", | ||
| # Temporarily skip some integrations | ||
| submodule_names | ||
| - { | ||
| "clickhouse_driver", | ||
| "grpc", | ||
| "litellm", | ||
| "opentelemetry", | ||
| "pure_eval", | ||
| "ray", | ||
| "trytond", | ||
| "typer", | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def find_unrecognized_dependencies(tree): | ||
| """ | ||
| Finds unrecognized imports in the AST for a Python module. In an empty | ||
| environment the set of non-standard library modules is returned. | ||
| """ | ||
| unrecognized_dependencies = set() | ||
| package_name = lambda name: name.split(".")[0] | ||
|
|
||
| for node in ast.walk(tree): | ||
| if isinstance(node, ast.Import): | ||
| for alias in node.names: | ||
| root = package_name(alias.name) | ||
|
|
||
| try: | ||
| if not importlib.util.find_spec(root): | ||
| unrecognized_dependencies.add(root) | ||
| except ValueError: | ||
| continue | ||
|
|
||
| elif isinstance(node, ast.ImportFrom): | ||
| # if node.level is not 0 the import is relative | ||
| if node.level > 0 or node.module is None: | ||
| continue | ||
|
|
||
| root = package_name(node.module) | ||
|
|
||
| try: | ||
| if not importlib.util.find_spec(root): | ||
| unrecognized_dependencies.add(root) | ||
| except ValueError: | ||
| continue | ||
|
|
||
| return unrecognized_dependencies | ||
|
|
||
|
|
||
| @pytest.mark.skipif( | ||
| sys.version_info < (3, 7), reason="asyncpg imports __future__.annotations" | ||
| ) | ||
| def test_shadowed_modules_when_importing_integrations( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I understand what's going on here, but it takes some effort to grasp. Maybe a good case for additional comments in the function to better explain what's happening step-by-step?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, that's a good call! Added a few comments. |
||
| sentry_init, integration_submodule_name | ||
| ): | ||
| """ | ||
| Check that importing integrations for third-party module raises an | ||
| DidNotEnable exception when the associated module is shadowed by an empty | ||
| module. | ||
|
|
||
| An integration is determined to be for a third-party module if it cannot | ||
| be imported in the environment in which the tests run. | ||
| """ | ||
| module_path = f"sentry_sdk.integrations.{integration_submodule_name}" | ||
| try: | ||
| # If importing the integration succeeds in the current environment, assume | ||
| # that the integration has no non-standard imports. | ||
| importlib.import_module(module_path) | ||
| return | ||
| except integrations.DidNotEnable: | ||
| spec = importlib.util.find_spec(module_path) | ||
| source = pathlib.Path(spec.origin).read_text(encoding="utf-8") | ||
| tree = ast.parse(source, filename=spec.origin) | ||
| integration_dependencies = find_unrecognized_dependencies(tree) | ||
|
|
||
| # For each non-standard import, create an empty shadow module to | ||
| # emulate an empty "agents.py" or analogous local module that | ||
| # shadows the package. | ||
| for dependency in integration_dependencies: | ||
| sys.modules[dependency] = types.ModuleType(dependency) | ||
|
|
||
| # Importing the integration must raise DidNotEnable, since the | ||
| # SDK catches the exception type when attempting to activate | ||
| # auto-enabling integrations. | ||
| with pytest.raises(integrations.DidNotEnable): | ||
| importlib.import_module(module_path) | ||
|
|
||
| for dependency in integration_dependencies: | ||
| del sys.modules[dependency] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What determines what ends up in the ignore list here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are packages for which the test fails. I've taken a brief look at all of them and there's no false positives.