-
-
Notifications
You must be signed in to change notification settings - Fork 90
Make pandas, numpy, and matplotlib optional dependencies with lazy loading and helpful error messages #309
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
74b9105
Initial plan
Copilot d6dd79d
Make numpy import lazy in prometheus_connect.py
Copilot 2447357
Add optional dependencies support via extras_require
Copilot 74f8772
Fix test module detection logic to avoid false positives
Copilot 33e0289
Refactor test code to reduce duplication
Copilot 7832b91
Bump version to 0.7.0 for new release
Copilot b7f4b1e
Update README.md to reflect version 0.7.0
Copilot 08a7642
Keep pandas as a core dependency
Copilot c02129e
Fix numpy reload issue in TestLazyImports
Copilot 1c8d53b
Remove unused importlib import from test_lazy_imports.py
Copilot c2db4c4
Refactor test_lazy_imports.py to reduce code duplication
Copilot cefc56c
Make pandas an optional dependency again
Copilot abc430e
Add use-case oriented extras and helpful error messages
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| requests | ||
| dateparser |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """Test lazy imports to ensure pandas/matplotlib are not loaded unnecessarily.""" | ||
| import unittest | ||
| import sys | ||
| import subprocess | ||
|
|
||
|
|
||
| class TestLazyImports(unittest.TestCase): | ||
| """Test that PrometheusConnect can be imported without loading heavy dependencies.""" | ||
|
|
||
| def _run_in_subprocess(self, code, fail_map): | ||
| """Run code in a subprocess and check exit codes against fail_map. | ||
|
|
||
| Args: | ||
| code: Python code to execute in subprocess | ||
| fail_map: Dictionary mapping exit codes to error messages | ||
|
|
||
| Raises: | ||
| AssertionError: If subprocess exits with a code in fail_map or any non-zero code | ||
| """ | ||
| result = subprocess.run( | ||
| [sys.executable, '-c', code], | ||
| capture_output=True, | ||
| text=True | ||
| ) | ||
|
|
||
| if result.returncode in fail_map: | ||
| self.fail(fail_map[result.returncode]) | ||
| elif result.returncode != 0: | ||
| # Include both stdout and stderr for better debugging | ||
| output = [] | ||
| if result.stdout: | ||
| output.append(f"stdout: {result.stdout}") | ||
| if result.stderr: | ||
| output.append(f"stderr: {result.stderr}") | ||
| output_str = "\n".join(output) if output else "no output" | ||
| self.fail(f"Subprocess failed with code {result.returncode}: {output_str}") | ||
|
|
||
| def test_prometheus_connect_import_without_pandas_matplotlib_numpy(self): | ||
| """Test that importing PrometheusConnect doesn't load pandas, matplotlib, or numpy.""" | ||
| # Run in a subprocess to avoid affecting other tests | ||
| code = """ | ||
| import sys | ||
| from prometheus_api_client import PrometheusConnect | ||
|
|
||
| # Check that pandas, matplotlib, and numpy are not loaded | ||
| pandas_loaded = any(m == 'pandas' or m.startswith('pandas.') for m in sys.modules.keys()) | ||
| matplotlib_loaded = any(m == 'matplotlib' or m.startswith('matplotlib.') for m in sys.modules.keys()) | ||
| numpy_loaded = any(m == 'numpy' or m.startswith('numpy.') for m in sys.modules.keys()) | ||
|
|
||
| if pandas_loaded: | ||
| sys.exit(1) | ||
| if matplotlib_loaded: | ||
| sys.exit(2) | ||
| if numpy_loaded: | ||
| sys.exit(3) | ||
| sys.exit(0) | ||
| """ | ||
| fail_map = { | ||
| 1: "pandas should not be loaded when importing PrometheusConnect", | ||
| 2: "matplotlib should not be loaded when importing PrometheusConnect", | ||
| 3: "numpy should not be loaded when importing PrometheusConnect", | ||
| } | ||
| self._run_in_subprocess(code, fail_map) | ||
|
|
||
| def test_prometheus_connect_instantiation_without_numpy(self): | ||
| """Test that PrometheusConnect can be instantiated without loading numpy.""" | ||
| # Run in a subprocess to avoid affecting other tests | ||
| code = """ | ||
|
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. same as above |
||
| import sys | ||
| from prometheus_api_client import PrometheusConnect | ||
|
|
||
| pc = PrometheusConnect(url='http://test.local:9090') | ||
|
|
||
| # Check that numpy is still not loaded after instantiation | ||
| numpy_loaded = any(m == 'numpy' or m.startswith('numpy.') for m in sys.modules.keys()) | ||
|
|
||
| if numpy_loaded: | ||
| sys.exit(1) | ||
| if pc is None: | ||
| sys.exit(2) | ||
| sys.exit(0) | ||
| """ | ||
| fail_map = { | ||
| 1: "numpy should not be loaded when instantiating PrometheusConnect", | ||
| 2: "PrometheusConnect should be instantiated successfully", | ||
| } | ||
| self._run_in_subprocess(code, fail_map) | ||
|
|
||
| def test_metric_import_loads_pandas(self): | ||
| """Test that importing Metric does load pandas (expected behavior).""" | ||
| # This test doesn't remove modules, so it won't cause reload issues | ||
| from prometheus_api_client import Metric | ||
|
|
||
| # Check that pandas is loaded (this is expected for Metric) | ||
| pandas_loaded = any(m == 'pandas' or m.startswith('pandas.') for m in sys.modules.keys()) | ||
| self.assertTrue(pandas_loaded, "pandas should be loaded when importing Metric") | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: Consider using
textwrap.dedentfor multi-line text.