-
-
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 9 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| requests | ||
| dateparser | ||
| pandas>=1.4.0 |
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,88 @@ | ||
| """Test lazy imports to ensure pandas/matplotlib are not loaded unnecessarily.""" | ||
| import unittest | ||
| import sys | ||
| import subprocess | ||
| import importlib | ||
4n4nd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class TestLazyImports(unittest.TestCase): | ||
| """Test that PrometheusConnect can be imported without loading heavy dependencies.""" | ||
|
|
||
| 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 = """ | ||
|
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. nit: Consider using |
||
| 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) | ||
| """ | ||
| result = subprocess.run( | ||
4n4nd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| [sys.executable, '-c', code], | ||
| capture_output=True, | ||
| text=True | ||
| ) | ||
|
|
||
| if result.returncode == 1: | ||
| self.fail("pandas should not be loaded when importing PrometheusConnect") | ||
| elif result.returncode == 2: | ||
| self.fail("matplotlib should not be loaded when importing PrometheusConnect") | ||
| elif result.returncode == 3: | ||
| self.fail("numpy should not be loaded when importing PrometheusConnect") | ||
| elif result.returncode != 0: | ||
| self.fail(f"Subprocess failed with code {result.returncode}: {result.stderr}") | ||
|
|
||
| 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) | ||
| """ | ||
| result = subprocess.run( | ||
| [sys.executable, '-c', code], | ||
| capture_output=True, | ||
| text=True | ||
| ) | ||
|
|
||
| if result.returncode == 1: | ||
| self.fail("numpy should not be loaded when instantiating PrometheusConnect") | ||
| elif result.returncode == 2: | ||
| self.fail("PrometheusConnect should be instantiated successfully") | ||
| elif result.returncode != 0: | ||
| self.fail(f"Subprocess failed with code {result.returncode}: {result.stderr}") | ||
|
|
||
| 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.
Can we put some try/except here? If a user has installed the minimal version and ties calling this function, they will get a generic import error. IMO we should explicitly mention that they’ll need to install the
[numpy]or[full]version.I believe this suggestion applies for Metric, MetricPlot, and other classes as well
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.
Added try/except blocks with helpful error messages in commit abc430e:
get_metric_aggregation()suggests installing[analytics]or[all][dataframe]or[all]All error messages clearly guide users to the correct installation command.