Skip to content
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

Add typing to Histogram/Summary/Gauge #759

Merged
merged 4 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -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
28 changes: 14 additions & 14 deletions prometheus_client/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def set_function(self, f):

self._raise_if_not_observable()

def samples(self) -> Iterable[Sample]:
def samples(_: Gauge) -> Iterable[Sample]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the only one thing which is needed to enable disallow_incomplete_defs.

return (Sample('', {}, float(f()), None, None),)

self._child_samples = types.MethodType(samples, self)
Expand Down Expand Up @@ -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__(
Expand All @@ -553,7 +553,7 @@ def __init__(self,
)
self._kwargs['buckets'] = buckets

def _prepare_buckets(self, buckets):
def _prepare_buckets(self, buckets: Sequence[float]) -> None:
csmarchbanks marked this conversation as resolved.
Show resolved Hide resolved
buckets = [float(b) for b in buckets]
if buckets != sorted(buckets):
# This is probably an error on the part of the user,
Expand All @@ -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',)
Expand All @@ -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:
csmarchbanks marked this conversation as resolved.
Show resolved Hide resolved
"""Observe the given amount.

The amount is usually positive or zero. Negative values are
Expand All @@ -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.
Expand Down