22from threading import Lock
33import time
44import types
5+ from typing import Any , Callable , Dict , Optional , Sequence , Tuple , Type , TypeVar
56
67from . import values # retain this import style for testability
78from .context_managers import ExceptionCounter , InprogressTracker , Timer
89from .metrics_core import (
910 Metric , METRIC_LABEL_NAME_RE , METRIC_NAME_RE ,
1011 RESERVED_METRIC_LABEL_NAME_RE ,
1112)
12- from .registry import REGISTRY
13+ from .registry import CollectorRegistry , REGISTRY
1314from .utils import floatToGoString , INF
1415
16+ T = TypeVar ('T' , bound = 'MetricWrapperBase' )
17+ F = TypeVar ("F" , bound = Callable [..., Any ])
18+
1519if sys .version_info > (3 ,):
1620 unicode = str
1721 create_bound_method = types .MethodType
@@ -97,6 +101,7 @@ def __init__(self,
97101 registry = REGISTRY ,
98102 _labelvalues = None ,
99103 ):
104+ # type: (str, str, Sequence[str], str, str, str, CollectorRegistry, Optional[Sequence[str]]) -> None
100105 self ._name = _build_full_name (self ._type , name , namespace , subsystem , unit )
101106 self ._labelnames = _validate_labelnames (self , labelnames )
102107 self ._labelvalues = tuple (_labelvalues or ())
@@ -121,6 +126,7 @@ def __init__(self,
121126 registry .register (self )
122127
123128 def labels (self , * labelvalues , ** labelkwargs ):
129+ # type: (T, *str, **str) -> T
124130 """Return the child for the given labelset.
125131
126132 All metrics can have labels, allowing grouping of related time series.
@@ -187,6 +193,7 @@ def remove(self, *labelvalues):
187193 del self ._metrics [labelvalues ]
188194
189195 def clear (self ):
196+ # type: () -> None
190197 """Remove all labelsets from the metric"""
191198 with self ._lock :
192199 self ._metrics = {}
@@ -206,6 +213,7 @@ def _multi_samples(self):
206213 yield (suffix , dict (series_labels + list (sample_labels .items ())), value )
207214
208215 def _child_samples (self ): # pragma: no cover
216+ # type: () -> Sequence[Tuple[str, Dict[str, str], float]]
209217 raise NotImplementedError ('_child_samples() must be implemented by %r' % self )
210218
211219 def _metric_init (self ): # pragma: no cover
@@ -252,18 +260,21 @@ def f():
252260 _type = 'counter'
253261
254262 def _metric_init (self ):
263+ # type: () -> None
255264 self ._value = values .ValueClass (self ._type , self ._name , self ._name + '_total' , self ._labelnames ,
256265 self ._labelvalues )
257266 self ._created = time .time ()
258267
259268 def inc (self , amount = 1 ):
269+ # type: (float) -> None
260270 """Increment counter by the given amount."""
261271 self ._raise_if_not_observable ()
262272 if amount < 0 :
263273 raise ValueError ('Counters can only be incremented by non-negative amounts.' )
264274 self ._value .inc (amount )
265275
266276 def count_exceptions (self , exception = Exception ):
277+ # type: (Type[BaseException]) -> ExceptionCounter
267278 """Count exceptions in a block of code or function.
268279
269280 Can be used as a function decorator or context manager.
@@ -663,6 +674,7 @@ def __init__(self,
663674 _labelvalues = None ,
664675 states = None ,
665676 ):
677+ # type: (str, str, Sequence[str], str, str, str, CollectorRegistry, Optional[Sequence[str]], Optional[Sequence[str]]) -> None
666678 super (Enum , self ).__init__ (
667679 name = name ,
668680 documentation = documentation ,
@@ -684,6 +696,7 @@ def _metric_init(self):
684696 self ._lock = Lock ()
685697
686698 def state (self , state ):
699+ # type: (str) -> None
687700 """Set enum metric state."""
688701 self ._raise_if_not_observable ()
689702 with self ._lock :
0 commit comments