From 3c515e713f3df986cca016b8f117f0de05c5e08b Mon Sep 17 00:00:00 2001 From: Yury Pliner Date: Fri, 28 Jan 2022 16:51:53 +0000 Subject: [PATCH] Add typing to Histogram Also we could set disallow_incomplete_defs to True --- mypy.ini | 1 + prometheus_client/metrics.py | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/mypy.ini b/mypy.ini index 1470c9b2..fe372d07 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,6 +1,7 @@ [mypy] exclude = prometheus_client/decorator.py|prometheus_client/twisted|tests/test_twisted.py implicit_reexport = False +disallow_incomplete_defs = True [mypy-prometheus_client.decorator] follow_imports = skip diff --git a/prometheus_client/metrics.py b/prometheus_client/metrics.py index be61f05c..c15b33b8 100644 --- a/prometheus_client/metrics.py +++ b/prometheus_client/metrics.py @@ -413,7 +413,7 @@ def set_function(self, f): self._raise_if_not_observable() - def samples(self) -> Iterable[Sample]: + def samples(_: Gauge) -> Iterable[Sample]: return (Sample('', {}, float(f()), None, None),) self._child_samples = types.MethodType(samples, self) @@ -530,15 +530,15 @@ def create_response(request): DEFAULT_BUCKETS = (.005, .01, .025, .05, .075, .1, .25, .5, .75, 1.0, 2.5, 5.0, 7.5, 10.0, INF) def __init__(self, - name, - documentation, - labelnames=(), - namespace='', - subsystem='', - unit='', - registry=REGISTRY, - _labelvalues=None, - buckets=DEFAULT_BUCKETS, + name: str, + documentation: str, + labelnames: Iterable[str] = (), + namespace: str = '', + subsystem: str = '', + unit: str = '', + registry: Optional[CollectorRegistry] = REGISTRY, + _labelvalues: Optional[Sequence[str]] = None, + buckets: Sequence[float] = DEFAULT_BUCKETS, ): self._prepare_buckets(buckets) super().__init__( @@ -553,7 +553,7 @@ def __init__(self, ) self._kwargs['buckets'] = buckets - def _prepare_buckets(self, buckets): + def _prepare_buckets(self, buckets: Sequence[float]) -> None: buckets = [float(b) for b in buckets] if buckets != sorted(buckets): # This is probably an error on the part of the user, @@ -565,7 +565,7 @@ def _prepare_buckets(self, buckets): raise ValueError('Must have at least two buckets') self._upper_bounds = buckets - def _metric_init(self): + def _metric_init(self) -> None: self._buckets = [] self._created = time.time() bucket_labelnames = self._labelnames + ('le',) @@ -579,7 +579,7 @@ def _metric_init(self): self._labelvalues + (floatToGoString(b),)) ) - def observe(self, amount, exemplar=None): + def observe(self, amount: float = 1, exemplar: Optional[Dict[str, str]] = None) -> None: """Observe the given amount. The amount is usually positive or zero. Negative values are @@ -599,7 +599,7 @@ def observe(self, amount, exemplar=None): self._buckets[i].set_exemplar(Exemplar(exemplar, amount, time.time())) break - def time(self): + def time(self) -> Timer: """Time a block of code or function, and observe the duration in seconds. Can be used as a function decorator or context manager.