Skip to content

Commit c144de9

Browse files
committed
Switch Enum types to StrEnum
The base Enum type has interesting behaviour when it comes to equality. Depending on how they were imported, you may well encounter a situation where a variable containing an Enum variant does not equal the Enum variant itself, not with `==` nor with `is`. This is very counter-intuitive and even broke HFS authentication in tests. Luckily, all our Enum types are string-backed already, so we can switch them to inherit from StrEnum. This means they for all intents and purposes behave like regular strings, and equality checks on them check equality of their `.value` strings. Import path no longer matters and ObeliskKind.HFS will always equal ObeliskKind.HFS, no matter the context.
1 parent 56bda22 commit c144de9

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/obelisk/types.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from enum import Enum
1+
from enum import StrEnum
22
from typing import List, Any, Optional
33

44
from pydantic import BaseModel
55

66

7-
class IngestMode(Enum):
7+
class IngestMode(StrEnum):
88
"""
99
Classic Obelisk accepts three ways of submitting new data.
1010
This integrates with the concept of Streams,
@@ -22,7 +22,7 @@ class IngestMode(Enum):
2222
STORE_ONLY = 'store_only'
2323

2424

25-
class TimestampPrecision(Enum):
25+
class TimestampPrecision(StrEnum):
2626
"""
2727
When ingesting data it is important to specify which precision provided UNIX timestamps are in.
2828
If a provided timestamp is in seconds,
@@ -42,14 +42,14 @@ class Datapoint(BaseModel, extra='allow'):
4242
dataset: Optional[str] = None
4343
metric: Optional[str] = None
4444
source: Optional[str] = None
45-
userId: Optional[int] = None # Only if HFS and no other name for field
45+
userId: Optional[int] = None # Only if HFS and no other name for field
4646

4747

4848
class QueryResult(BaseModel):
4949
items: List[Datapoint]
5050
cursor: Optional[str] = None
5151

5252

53-
class ObeliskKind(Enum):
53+
class ObeliskKind(StrEnum):
5454
CLASSIC = 'classic'
5555
HFS = 'hfs'

0 commit comments

Comments
 (0)