-
Notifications
You must be signed in to change notification settings - Fork 0
fix: Make linkml an optional dependency for plugin imports #23
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,14 @@ | ||
| """LinkML validation plugins for reference validation.""" | ||
|
|
||
| from linkml_reference_validator.plugins.reference_validation_plugin import ( | ||
| ReferenceValidationPlugin, | ||
| ) | ||
| from importlib.util import find_spec | ||
|
|
||
| __all__ = ["ReferenceValidationPlugin"] | ||
| # `linkml` is an optional dependency. Expose the LinkML plugin only when LinkML is available. | ||
| __all__: list[str] | ||
| if find_spec("linkml") is not None and find_spec("linkml.validator") is not None: | ||
| from linkml_reference_validator.plugins.reference_validation_plugin import ( # noqa: F401 | ||
| ReferenceValidationPlugin, | ||
| ) | ||
|
|
||
| __all__ = ["ReferenceValidationPlugin"] | ||
| else: | ||
| __all__ = [] |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,51 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Tests for running without optional `linkml` dependency installed.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import importlib | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from importlib import util as importlib_util | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_cli_imports_without_linkml(monkeypatch): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Importing the CLI should not require `linkml` to be installed.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real_find_spec = importlib_util.find_spec | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def fake_find_spec(name: str, *args, **kwargs): # type: ignore[no-untyped-def] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if name == "linkml" or name.startswith("linkml."): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return real_find_spec(name, *args, **kwargs) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| monkeypatch.setattr(importlib_util, "find_spec", fake_find_spec) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Force a clean import of our package modules under the "no linkml" condition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for mod in list(sys.modules): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if mod.startswith("linkml_reference_validator"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| del sys.modules[mod] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cli = importlib.import_module("linkml_reference_validator.cli") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert getattr(cli, "app", None) is not None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_plugins_package_imports_without_linkml(monkeypatch): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Importing `linkml_reference_validator.plugins` should not require `linkml`.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| real_find_spec = importlib_util.find_spec | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def fake_find_spec(name: str, *args, **kwargs): # type: ignore[no-untyped-def] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if name == "linkml" or name.startswith("linkml."): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return real_find_spec(name, *args, **kwargs) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| monkeypatch.setattr(importlib_util, "find_spec", fake_find_spec) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for mod in list(sys.modules): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if mod.startswith("linkml_reference_validator"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| del sys.modules[mod] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+41
to
+43
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+7
to
+44
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_cli_imports_without_linkml(monkeypatch): | |
| """Importing the CLI should not require `linkml` to be installed.""" | |
| real_find_spec = importlib_util.find_spec | |
| def fake_find_spec(name: str, *args, **kwargs): # type: ignore[no-untyped-def] | |
| if name == "linkml" or name.startswith("linkml."): | |
| return None | |
| return real_find_spec(name, *args, **kwargs) | |
| monkeypatch.setattr(importlib_util, "find_spec", fake_find_spec) | |
| # Force a clean import of our package modules under the "no linkml" condition | |
| for mod in list(sys.modules): | |
| if mod.startswith("linkml_reference_validator"): | |
| del sys.modules[mod] | |
| cli = importlib.import_module("linkml_reference_validator.cli") | |
| assert getattr(cli, "app", None) is not None | |
| def test_plugins_package_imports_without_linkml(monkeypatch): | |
| """Importing `linkml_reference_validator.plugins` should not require `linkml`.""" | |
| real_find_spec = importlib_util.find_spec | |
| def fake_find_spec(name: str, *args, **kwargs): # type: ignore[no-untyped-def] | |
| if name == "linkml" or name.startswith("linkml."): | |
| return None | |
| return real_find_spec(name, *args, **kwargs) | |
| monkeypatch.setattr(importlib_util, "find_spec", fake_find_spec) | |
| for mod in list(sys.modules): | |
| if mod.startswith("linkml_reference_validator"): | |
| del sys.modules[mod] | |
| import pytest | |
| def test_cli_imports_without_linkml(): | |
| """Importing the CLI should not require `linkml` to be installed in environments where it is absent.""" | |
| # If `linkml` is installed in this test environment, we cannot reliably | |
| # assert behavior "without linkml", so skip instead of trying to fake it. | |
| if importlib_util.find_spec("linkml") is not None: | |
| pytest.skip("`linkml` is installed; cannot verify behavior without it.") | |
| # In an environment where `linkml` is truly absent, importing the CLI | |
| # module should still succeed. | |
| cli = importlib.import_module("linkml_reference_validator.cli") | |
| assert cli is not None | |
| def test_plugins_package_imports_without_linkml(): | |
| """Importing `linkml_reference_validator.plugins` should not require `linkml` when it is not installed.""" | |
| if importlib_util.find_spec("linkml") is not None: | |
| pytest.skip("`linkml` is installed; cannot verify behavior without it.") |
Copilot
AI
Jan 8, 2026
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.
The file has 4 trailing blank lines (lines 48-51). Python style guides typically recommend ending files with a single blank line.
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.
Module 'importlib' is imported with both 'import' and 'import from'.