Skip to content

Commit abc430e

Browse files
Copilot4n4nd
andcommitted
Add use-case oriented extras and helpful error messages
- Added 'analytics' extra for use-case oriented installation - Added try/except blocks with helpful error messages for missing dependencies: - NumPy: suggests installing [analytics] or [all] - Pandas: suggests installing [dataframe] or [all] - Updated README to highlight analytics extra instead of numpy - Error messages guide users to install correct extras - All 55 tests pass Co-authored-by: 4n4nd <22333506+4n4nd@users.noreply.github.com>
1 parent cefc56c commit abc430e

6 files changed

Lines changed: 39 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ If you only need `PrometheusConnect` without DataFrame support or plotting capab
2020

2121
To install only specific extras:
2222
- For DataFrame support: `pip install prometheus-api-client[dataframe]`
23-
- For numpy support: `pip install prometheus-api-client[numpy]`
23+
- For analytics/aggregation operations: `pip install prometheus-api-client[analytics]`
2424
- For plotting support: `pip install prometheus-api-client[plot]`
2525

2626
To install directly from this branch:

prometheus_api_client/metric.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
"""A Class for metric object."""
22
from copy import deepcopy
33
import datetime
4-
import pandas
4+
5+
try:
6+
import pandas
7+
except ImportError as e:
8+
raise ImportError(
9+
"Pandas is required for Metric class. "
10+
"Please install it with: pip install prometheus-api-client[dataframe] "
11+
"or pip install prometheus-api-client[all]"
12+
) from e
513

614
from prometheus_api_client.exceptions import MetricValueConversionError
715

prometheus_api_client/metric_range_df.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
"""A pandas.DataFrame subclass for Prometheus range vector responses."""
2-
from pandas import DataFrame, to_datetime
3-
from pandas._typing import Axes, Dtype
2+
try:
3+
from pandas import DataFrame, to_datetime
4+
from pandas._typing import Axes, Dtype
5+
except ImportError as e:
6+
raise ImportError(
7+
"Pandas is required for MetricRangeDataFrame class. "
8+
"Please install it with: pip install prometheus-api-client[dataframe] "
9+
"or pip install prometheus-api-client[all]"
10+
) from e
11+
412
from typing import Optional, Sequence
513

614
from prometheus_api_client.exceptions import MetricValueConversionError

prometheus_api_client/metric_snapshot_df.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
"""A pandas.DataFrame subclass for Prometheus query response."""
2-
from pandas import DataFrame, to_datetime
3-
from pandas._typing import Axes, Dtype
2+
try:
3+
from pandas import DataFrame, to_datetime
4+
from pandas._typing import Axes, Dtype
5+
except ImportError as e:
6+
raise ImportError(
7+
"Pandas is required for MetricSnapshotDataFrame class. "
8+
"Please install it with: pip install prometheus-api-client[dataframe] "
9+
"or pip install prometheus-api-client[all]"
10+
) from e
11+
412
from typing import Optional, Sequence
513

614
from prometheus_api_client.exceptions import MetricValueConversionError

prometheus_api_client/prometheus_connect.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,14 @@ def get_metric_aggregation(
568568
'max': 6.009373
569569
}
570570
"""
571-
import numpy
571+
try:
572+
import numpy
573+
except ImportError as e:
574+
raise ImportError(
575+
"NumPy is required for metric aggregation operations. "
576+
"Please install it with: pip install prometheus-api-client[analytics] "
577+
"or pip install prometheus-api-client[all]"
578+
) from e
572579

573580
if not isinstance(operations, list):
574581
raise TypeError("Operations can be only of type list")

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def get_version():
4040
"dataframe": ["pandas>=1.4.0"],
4141
"numpy": ["numpy"],
4242
"plot": ["matplotlib"],
43+
"analytics": ["numpy"],
4344
"all": ["pandas>=1.4.0", "numpy", "matplotlib"],
4445
},
4546
packages=setuptools.find_packages(),

0 commit comments

Comments
 (0)