|
15 | 15 | import httpx |
16 | 16 | import json |
17 | 17 | from pydantic import BaseModel, AwareDatetime, ConfigDict, Field, ValidationError, model_validator |
18 | | -from typing import Annotated, AsyncIterator, Dict, Iterator, List, Literal, Optional, Any, get_args |
| 18 | +from typing import Annotated, AsyncIterator, Dict, Iterator, List, Literal, Optional, Any, cast, get_args |
19 | 19 | from typing_extensions import Self |
20 | 20 | from numbers import Number |
21 | 21 |
|
|
24 | 24 | """The possible types of data Obelisk can accept""" |
25 | 25 |
|
26 | 26 |
|
| 27 | +def type_suffix(metric: str) -> DataType: |
| 28 | + split = metric.split('::') |
| 29 | + |
| 30 | + if len(split) != 2: |
| 31 | + raise ValueError("Incorrect amount of type qualifiers") |
| 32 | + |
| 33 | + suffix = split[1] |
| 34 | + if suffix not in get_args(DataType): |
| 35 | + raise ValueError(f"Invalid type suffix, should be one of {', '.join(get_args(DataType))}") |
| 36 | + return cast(DataType, suffix) |
| 37 | + |
| 38 | + |
27 | 39 | Aggregator = Literal['last', 'min', 'mean', 'max', 'count', 'stddev'] |
28 | 40 | """Type of aggregation Obelisk can process""" |
29 | 41 |
|
@@ -56,14 +68,7 @@ class IncomingDatapoint(BaseModel): |
56 | 68 |
|
57 | 69 | @model_validator(mode='after') |
58 | 70 | def check_metric_type(self) -> Self: |
59 | | - split = self.metric.split('::') |
60 | | - |
61 | | - if len(split) != 2: |
62 | | - raise ValueError("Incorrect amount of type qualifiers") |
63 | | - |
64 | | - suffix = split[1] |
65 | | - if suffix not in get_args(DataType): |
66 | | - raise ValueError(f"Invalid type suffix, should be one of {', '.join(get_args(DataType))}") |
| 71 | + suffix = type_suffix(self.metric) |
67 | 72 |
|
68 | 73 | if suffix == 'number' and not isinstance(self.value, Number): |
69 | 74 | raise ValueError(f"Type suffix mismatch, expected number, got {type(self.value)}") |
|
0 commit comments