8000 Add type annotations to parser.py · draftcode/client_python@66ecd51 · GitHub
[go: up one dir, main page]

Skip to content

Commit 66ecd51

Browse files
committed
Add type annotations to parser.py
Signed-off-by: Yiyang Zhan <pon.zhan@gmail.com>
1 parent fd4da6c commit 66ecd51

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

prometheus_client/parser.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import io as StringIO
22
import re
3+
from typing import Dict, Iterable, List, Match, Optional, TextIO, Tuple
34

45
from .metrics_core import Metric
56
from .samples import Sample
67

78

8-
def text_string_to_metric_families(text):
9+
def text_string_to_metric_families(text: str) -> Iterable[Metric]:
910
"""Parse Prometheus text format from a unicode string.
1011
1112
See text_fd_to_metric_families.
@@ -20,32 +21,32 @@ def text_string_to_metric_families(text):
2021
}
2122

2223

23-
def replace_escape_sequence(match):
24+
def replace_escape_sequence(match: Match[str]) -> str:
2425
return ESCAPE_SEQUENCES[match.group(0)]
2526

2627

2728
HELP_ESCAPING_RE = re.compile(r'\\[\\n]')
2829
ESCAPING_RE = re.compile(r'\\[\\n"]')
2930

3031

31-
def _replace_help_escaping(s):
32+
def _replace_help_escaping(s: str) -> str:
3233
return HELP_ESCAPING_RE.sub(replace_escape_sequence, s)
3334

3435

35-
def _replace_escaping(s):
36+
def _replace_escaping(s: str) -> str:
3637
return ESCAPING_RE.sub(replace_escape_sequence, s)
3738

3839

39-
def _is_character_escaped(s, charpos):
40+
def _is_character_escaped(s: str, charpos: int) -> bool:
4041
num_bslashes = 0
4142
while (charpos > num_bslashes
4243
and s[charpos - 1 - num_bslashes] == '\\'):
4344
num_bslashes += 1
4445
return num_bslashes % 2 == 1
4546

4647

47-
def _parse_labels(labels_string):
48-
labels = {}
48+
def _parse_labels(labels_string: str) -> Dict[str, str]:
49+
labels: Dict[str, str] = {}
4950
# Return if we don't have valid labels
5051
if "=" not in labels_string:
5152
return labels
@@ -95,7 +96,7 @@ def _parse_labels(labels_string):
9596

9697

9798
# If we have multiple values only consider the first
98-
def _parse_value_and_timestamp(s):
99+
def _parse_value_and_timestamp(s: str) -> Tuple[float, Optional[float]]:
99100
s = s.lstrip()
100101
separator = " "
101102
if separator not in s:
@@ -108,7 +109,7 @@ def _parse_value_and_timestamp(s):
108109
return value, timestamp
109110

110111

111-
def _parse_sample(text):
112+
def _parse_sample(text: str) -> Sample:
112113
# Detect the labels in the text
113114
try:
114115
label_start, label_end = text.index("{"), text.rindex("}")
@@ -133,7 +134,7 @@ def _parse_sample(text):
133134
return Sample(name, {}, value, timestamp)
134135

135136

136-
def text_fd_to_metric_families(fd):
137+
def text_fd_to_metric_families(fd: TextIO) -> Iterable[Metric]:
137138
"""Parse Prometheus text format from a file descriptor.
138139
139140
This is a laxer parser than the main Go parser,
@@ -145,10 +146,10 @@ def text_fd_to_metric_families(fd):
145146
name = ''
146147
documentation = ''
147148
typ = 'untyped'
148-
samples = []
149+
samples: List[Sample] = []
149150
allowed_names = []
150151

151-
def build_metric(name, documentation, typ, samples):
152+
def build_metric(name: str, documentation: str, typ: str, samples: List[Sample]) -> Metric:
152153
# Munge counters into OpenMetrics representation
153154
# used internally.
154155
if typ == 'counter':

0 commit comments

Comments
 (0)
0