Skip to content

Commit 7b49cb8

Browse files
committed
Add type_suffix helper
1 parent f043836 commit 7b49cb8

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/obelisk/asynchronous/core.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import httpx
1616
import json
1717
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
1919
from typing_extensions import Self
2020
from numbers import Number
2121

@@ -24,6 +24,18 @@
2424
"""The possible types of data Obelisk can accept"""
2525

2626

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+
2739
Aggregator = Literal['last', 'min', 'mean', 'max', 'count', 'stddev']
2840
"""Type of aggregation Obelisk can process"""
2941

@@ -56,14 +68,7 @@ class IncomingDatapoint(BaseModel):
5668

5769
@model_validator(mode='after')
5870
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)
6772

6873
if suffix == 'number' and not isinstance(self.value, Number):
6974
raise ValueError(f"Type suffix mismatch, expected number, got {type(self.value)}")

0 commit comments

Comments
 (0)