Skip to content

Commit

Permalink
Simplify type annotations for number types (#781)
Browse files Browse the repository at this point in the history
int values are acceptable to float variables:
https://www.python.org/dev/peps/pep-0484/#the-numeric-tower

Signed-off-by: Yiyang Zhan <pon.zhan@gmail.com>
  • Loading branch information
zhanpon committed Mar 9, 2022
1 parent 3c91b3f commit 789b24a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
16 changes: 8 additions & 8 deletions prometheus_client/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def _metric_init(self) -> None:
self._labelvalues)
self._created = time.time()

def inc(self, amount: Union[int, float] = 1, exemplar: Optional[Dict[str, str]] = None) -> None:
def inc(self, amount: float = 1, exemplar: Optional[Dict[str, str]] = None) -> None:
"""Increment counter by the given amount."""
self._raise_if_not_observable()
if amount < 0:
Expand Down Expand Up @@ -369,17 +369,17 @@ def _metric_init(self) -> None:
multiprocess_mode=self._multiprocess_mode
)

def inc(self, amount: Union[int, float] = 1) -> None:
def inc(self, amount: float = 1) -> None:
"""Increment gauge by the given amount."""
self._raise_if_not_observable()
self._value.inc(amount)

def dec(self, amount: Union[int, float] = 1) -> None:
def dec(self, amount: float = 1) -> None:
"""Decrement gauge by the given amount."""
self._raise_if_not_observable()
self._value.inc(-amount)

def set(self, value: Union[int, float]) -> None:
def set(self, value: float) -> None:
"""Set gauge to the given value."""
self._raise_if_not_observable()
self._value.set(float(value))
Expand Down Expand Up @@ -462,7 +462,7 @@ def _metric_init(self) -> None:
self._sum = values.ValueClass(self._type, self._name, self._name + '_sum', self._labelnames, self._labelvalues)
self._created = time.time()

def observe(self, amount: Union[int, float]) -> None:
def observe(self, amount: float) -> None:
"""Observe the given amount.
The amount is usually positive or zero. Negative values are
Expand Down Expand Up @@ -539,7 +539,7 @@ def __init__(self,
unit: str = '',
registry: Optional[CollectorRegistry] = REGISTRY,
_labelvalues: Optional[Sequence[str]] = None,
buckets: Sequence[Union[float, int, str]] = DEFAULT_BUCKETS,
buckets: Sequence[Union[float, str]] = DEFAULT_BUCKETS,
):
self._prepare_buckets(buckets)
super().__init__(
Expand All @@ -554,7 +554,7 @@ def __init__(self,
)
self._kwargs['buckets'] = buckets

def _prepare_buckets(self, source_buckets: Sequence[Union[float, int, str]]) -> None:
def _prepare_buckets(self, source_buckets: Sequence[Union[float, str]]) -> None:
buckets = [float(b) for b in source_buckets]
if buckets != sorted(buckets):
# This is probably an error on the part of the user,
Expand All @@ -580,7 +580,7 @@ def _metric_init(self) -> None:
self._labelvalues + (floatToGoString(b),))
)

def observe(self, amount: Union[int, float], exemplar: Optional[Dict[str, str]] = None) -> None:
def observe(self, amount: float, exemplar: Optional[Dict[str, str]] = None) -> None:
"""Observe the given amount.
The amount is usually positive or zero. Negative values are
Expand Down
26 changes: 13 additions & 13 deletions prometheus_client/metrics_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, name: str, documentation: str, typ: str, unit: str = ''):
self.type: str = typ
self.samples: List[Sample] = []

def add_sample(self, name: str, labels: Dict[str, str], value: Union[int, float], timestamp: Optional[Union[Timestamp, float]] = None, exemplar: Optional[Exemplar] = None) -> None:
def add_sample(self, name: str, labels: Dict[str, str], value: float, timestamp: Optional[Union[Timestamp, float]] = None, exemplar: Optional[Exemplar] = None) -> None:
"""Add a sample to the metric.
Internal-only, do not use."""
Expand Down Expand Up @@ -77,7 +77,7 @@ class UnknownMetricFamily(Metric):
def __init__(self,
name: str,
documentation: str,
value: Optional[Union[int, float]] = None,
value: Optional[float] = None,
labels: Optional[Sequence[str]] = None,
unit: str = '',
):
Expand All @@ -90,7 +90,7 @@ def __init__(self,
if value is not None:
self.add_metric([], value)

def add_metric(self, labels: Sequence[str], value: Union[int, float], timestamp: Optional[Union[Timestamp, float]] = None) -> None:
def add_metric(self, labels: Sequence[str], value: float, timestamp: Optional[Union[Timestamp, float]] = None) -> None:
"""Add a metric to the metric family.
Args:
labels: A list of label values
Expand All @@ -112,7 +112,7 @@ class CounterMetricFamily(Metric):
def __init__(self,
name: str,
documentation: str,
value: Optional[Union[int, float]] = None,
value: Optional[float] = None,
labels: Sequence[str] = None,
created: Optional[float] = None,
unit: str = '',
Expand All @@ -131,7 +131,7 @@ def __init__(self,

def add_metric(self,
labels: Sequence[str],
value: Union[int, float],
value: float,
created: Optional[float] = None,
timestamp: Optional[Union[Timestamp, float]] = None,
) -> None:
Expand All @@ -156,7 +156,7 @@ class GaugeMetricFamily(Metric):
def __init__(self,
name: str,
documentation: str,
value: Optional[Union[int, float]] = None,
value: Optional[float] = None,
labels: Optional[Sequence[str]] = None,
unit: str = '',
):
Expand All @@ -169,7 +169,7 @@ def __init__(self,
if value is not None:
self.add_metric([], value)

def add_metric(self, labels: Sequence[str], value: Union[int, float], timestamp: Optional[Union[Timestamp, float]] = None) -> None:
def add_metric(self, labels: Sequence[str], value: float, timestamp: Optional[Union[Timestamp, float]] = None) -> None:
"""Add a metric to the metric family.
Args:
Expand All @@ -189,7 +189,7 @@ def __init__(self,
name: str,
documentation: str,
count_value: Optional[int] = None,
sum_value: Optional[Union[int, float]] = None,
sum_value: Optional[float] = None,
labels: Optional[Sequence[str]] = None,
unit: str = '',
):
Expand All @@ -208,7 +208,7 @@ def __init__(self,
def add_metric(self,
labels: Sequence[str],
count_value: int,
sum_value: Union[int, float],
sum_value: float,
timestamp:
Optional[Union[float, Timestamp]] = None
) -> None:
Expand All @@ -233,7 +233,7 @@ def __init__(self,
name: str,
documentation: str,
buckets: Optional[Sequence[Union[Tuple[str, float], Tuple[str, float, Exemplar]]]] = None,
sum_value: Optional[Union[int, float]] = None,
sum_value: Optional[float] = None,
labels: Optional[Sequence[str]] = None,
unit: str = '',
):
Expand All @@ -251,7 +251,7 @@ def __init__(self,
def add_metric(self,
labels: Sequence[str],
buckets: Sequence[Union[Tuple[str, float], Tuple[str, float, Exemplar]]],
sum_value: Optional[Union[int, float]],
sum_value: Optional[float],
timestamp: Optional[Union[Timestamp, float]] = None) -> None:
"""Add a metric to the metric family.
Expand Down Expand Up @@ -295,7 +295,7 @@ def __init__(self,
name: str,
documentation: str,
buckets: Optional[Sequence[Tuple[str, float]]] = None,
gsum_value: Optional[Union[int, float]] = None,
gsum_value: Optional[float] = None,
labels: Optional[Sequence[str]] = None,
unit: str = '',
):
Expand All @@ -311,7 +311,7 @@ def __init__(self,
def add_metric(self,
labels: Sequence[str],
buckets: Sequence[Tuple[str, float]],
gsum_value: Optional[Union[int, float]],
gsum_value: Optional[float],
timestamp: Optional[Union[float, Timestamp]] = None,
) -> None:
"""Add a metric to the metric family.
Expand Down
4 changes: 2 additions & 2 deletions prometheus_client/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class Timestamp:
"""A nanosecond-resolution timestamp."""

def __init__(self, sec: Union[int, float], nsec: Union[int, float]) -> None:
def __init__(self, sec: float, nsec: float) -> None:
if nsec < 0 or nsec >= 1e9:
raise ValueError(f"Invalid value for nanoseconds in Timestamp: {nsec}")
if sec < 0:
Expand Down Expand Up @@ -45,6 +45,6 @@ class Exemplar(NamedTuple):
class Sample(NamedTuple):
name: str
labels: Dict[str, str]
value: Union[int, float]
value: float
timestamp: Optional[Union[float, Timestamp]] = None
exemplar: Optional[Exemplar] = None

0 comments on commit 789b24a

Please sign in to comment.