8000 Simplify type annotations for number types (#781) · chnacib/client_python@789b24a · GitHub
[go: up one dir, main page]

Skip to content

Commit 789b24a

Browse files
authored
Simplify type annotations for number types (prometheus#781)
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>
1 parent 3c91b3f commit 789b24a

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

prometheus_client/metrics.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def _metric_init(self) -> None:
270270
self._labelvalues)
271271
self._created = time.time()
272272

273-
def inc(self, amount: Union[int, float] = 1, exemplar: Optional[Dict[str, str]] = None) -> None:
273+
def inc(self, amount: float = 1, exemplar: Optional[Dict[str, str]] = None) -> None:
274274
"""Increment counter by the given amount."""
< 10000 code>275275
self._raise_if_not_observable()
276276
if amount < 0:
@@ -369,17 +369,17 @@ def _metric_init(self) -> None:
369369
multiprocess_mode=self._multiprocess_mode
370370
)
371371

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

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

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

465-
def observe(self, amount: Union[int, float]) -> None:
465+
def observe(self, amount: float) -> None:
466466
"""Observe the given amount.
467467
468468
The amount is usually positive or zero. Negative values are
@@ -539,7 +539,7 @@ def __init__(self,
539539
unit: str = '',
540540
registry: Optional[CollectorRegistry] = REGISTRY,
541541
_labelvalues: Optional[Sequence[str]] = None,
542-
buckets: Sequence[Union[float, int, str]] = DEFAULT_BUCKETS,
542+
buckets: Sequence[Union[float, str]] = DEFAULT_BUCKETS,
543543
):
544544
self._prepare_buckets(buckets)
545545
super().__init__(
@@ -554,7 +554,7 @@ def __init__(self,
554554
)
555555
self._kwargs['buckets'] = buckets
556556

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

583-
def observe(self, amount: Union[int, float], exemplar: Optional[Dict[str, str]] = None) -> None:
583+
def observe(self, amount: float, exemplar: Optional[Dict[str, str]] = None) -> None:
584584
"""Observe the given amount.
585585
586586
The amount is usually positive or zero. Negative values are

prometheus_client/metrics_core.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, name: str, documentation: str, typ: str, unit: str = ''):
3636
self.type: str = typ
3737
self.samples: List[Sample] = []
3838

39-
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:
39+
def add_sample(self, name: str, labels: Dict[str, str], value: float, timestamp: Optional[Union[Timestamp, float]] = None, exemplar: Optional[Exemplar] = None) -> None:
4040
"""Add a sample to the metric.
4141
4242
Internal-only, do not use."""
@@ -77,7 +77,7 @@ class UnknownMetricFamily(Metric):
7777
def __init__(self,
7878
name: str,
7979
documentation: str,
80-
value: Optional[Union[int, float]] = None,
80+
value: Optional[float] = None,
8181
labels: Optional[Sequence[str]] = None,
8282
unit: str = '',
8383
):
@@ -90,7 +90,7 @@ def __init__(self,
9090
if value is not None:
9191
self.add_metric([], value)
9292

93-
def add_metric(self, labels: Sequence[str], value: Union[int, float], timestamp: Optional[Union[Timestamp, float]] = None) -> None:
93+
def add_metric(self, labels: Sequence[str], value: float, timestamp: Optional[Union[Timestamp, float]] = None) -> None:
9494
"""Add a metric to the metric family.
9595
Args:
9696
labels: A list of label values
@@ -112,7 +112,7 @@ class CounterMetricFamily(Metric):
112112
def __init__(self,
113113
name: str,
114114
documentation: str,
115-
value: Optional[Union[int, float]] = None,
115+
value: Optional[float] = None,
116116
labels: Sequence[str] = None,
117117
created: Optional[float] = None,
118118
unit: str = '',
@@ -131,7 +131,7 @@ def __init__(self,
131131

132132
def add_metric(self,
133133
labels: Sequence[str],
134-
value: Union[int, float],
134+
value: float,
135135
created: Optional[float] = None,
136136
timestamp: Optional[Union[Timestamp, float]] = None,
137137
) -> None:
@@ -156,7 +156,7 @@ class GaugeMetricFamily(Metric):
156156
def __init__(self,
157157
name: str,
158158
documentation: str,
159-
value: Optional[Union[int, float]] = None,
159+
value: Optional[float] = None,
160160
labels: Optional[Sequence[str]] = None,
161161
unit: str = '',
162162
):
@@ -169,7 +169,7 @@ def __init__(self,
169169
if value is not None:
170170
self.add_metric([], value)
171171

172-
def add_metric(self, labels: Sequence[str], value: Union[int, float], timestamp: Optional[Union[Timestamp, float]] = None) -> None:
172+
def add_metric(self, labels: Sequence[str], value: float, timestamp: Optional[Union[Timestamp, float]] = None) -> None:
173173
"""Add a metric to the metric family.
174174
175175
Args:
@@ -189,7 +189,7 @@ def __init__(self,
189189
name: str,
190190
documentation: str,
191191
count_value: Optional[int] = None,
192-
sum_value: Optional[Union[int, float]] = None,
192+
sum_value: Optional[float] = None,
193193
labels: Optional[Sequence[str]] = None,
194194
unit: str = '',
195195
):
@@ -208,7 +208,7 @@ def __init__(self,
208208
def add_metric(self,
209209
labels: Sequence[str],
210210
count_value: int,
211-
sum_value: Union[int, float],
211+
sum_value: float,
212212
timestamp:
213213
Optional[Union[float, Timestamp]] = None
214214
) -> None:
@@ -233,7 +233,7 @@ def __init__(self,
233233
name: str,
234234
documentation: str,
235235
buckets: Optional[Sequence[Union[Tuple[str, float], Tuple[str, float, Exemplar]]]] = None,
236-
sum_value: Optional[Union[int, float]] = None,
236+
sum_value: Optional[float] = None,
237237
labels: Optional[Sequence[str]] = None,
238238
unit: str = '',
239239
):
@@ -251,7 +251,7 @@ def __init__(self,
251251
def add_metric(self,
252252
labels: Sequence[str],
253253
buckets: Sequence[Union[Tuple[str, float], Tuple[str, float, Exemplar]]],
254-
sum_value: Optional[Union[int, float]],
254+
sum_value: Optional[float],
255255
timestamp: Optional[Union[Timestamp, float]] = None) -> None:
256256
"""Add a metric to the metric family.
257257
@@ -295,7 +295,7 @@ def __init__(self,
295295
name: str,
296296
documentation: str,
297297
buckets: Optional[Sequence[Tuple[str, float]]] = None,
298-
gsum_value: Optional[Union[int, float]] = None,
298+
gsum_value: Optional[float] = None,
299299
labels: Optional[Sequence[str]] = None,
300300
unit: str = '',
301301
):
@@ -311,7 +311,7 @@ def __init__(self,
311311
def add_metric(self,
312312
labels: Sequence[str],
313313
buckets: Sequence[Tuple[str, float]],
314-
gsum_value: Optional[Union[int, float]],
314+
gsum_value: Optional[float],
315315
timestamp: Optional[Union[float, Timestamp]] = None,
316316
) -> None:
317317
"""Add a metric to the metric family.

prometheus_client/samples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class Timestamp:
55
"""A nanosecond-resolution timestamp."""
66

7-
def __init__(self, sec: Union[int, float], nsec: Union[int, float]) -> None:
7+
def __init__(self, sec: float, nsec: float) -> None:
88
if nsec < 0 or nsec >= 1e9:
99
raise ValueError(f"Invalid value for nanoseconds in Timestamp: {nsec}")
1010
if sec < 0:
@@ -45,6 +45,6 @@ class Exemplar(NamedTuple):
4545
class Sample(NamedTuple):
4646
name: str
4747
labels: Dict[str, str]
48-
value: Union[int, float]
48+
value: float
4949
timestamp: Optional[Union[float, Timestamp]] = None
5050
exemplar: Optional[Exemplar] = None

0 commit comments

Comments
 (0)
0