Skip to content

Commit c5526c8

Browse files
committed
chore: add str and int enum tests to cover type subclasses
1 parent c39a4ab commit c5526c8

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

tests/test_console_exporter.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import re
99
import sys
1010
from datetime import datetime
11+
from enum import Enum
1112
from typing import Any
1213
from unittest import mock
1314

@@ -31,6 +32,15 @@
3132
from logfire.testing import TestExporter
3233
from tests.utils import ReadableSpanModel, SpanContextModel, exported_spans_as_models
3334

35+
if sys.version_info >= (3, 11): # pragma: no branch
36+
from enum import IntEnum, StrEnum
37+
else: # pragma: no cover
38+
39+
class StrEnum(str, Enum): ...
40+
41+
class IntEnum(int, Enum): ...
42+
43+
3444
tracer = trace.get_tracer('test')
3545

3646
NANOSECONDS_PER_SECOND = int(1e9)
@@ -1032,12 +1042,21 @@ def test_console_exporter_log_pydantic_root_model(capsys: pytest.CaptureFixture[
10321042
class Model(BaseModel):
10331043
name: str
10341044

1045+
class Color(StrEnum):
1046+
red = 'RED'
1047+
1048+
class Order(IntEnum):
1049+
one = 1
1050+
10351051
RootWithModel = RootModel[Model]
10361052
RootWithStr = RootModel[str]
10371053
RootWithInt = RootModel[int]
10381054
RootWithFloat = RootModel[float]
10391055
RootWithBool = RootModel[bool]
10401056
RootWithNone = RootModel[None]
1057+
# enums (which are subclasses of their base types)
1058+
RootWithColor = RootModel[Color]
1059+
RootWithOrder = RootModel[Order]
10411060

10421061
model = Model(name='with_model')
10431062
root_with_model = RootWithModel(root=model)
@@ -1046,6 +1065,8 @@ class Model(BaseModel):
10461065
root_with_float = RootWithFloat(2.0)
10471066
root_with_bool = RootWithBool(False)
10481067
root_with_none = RootWithNone(None)
1068+
root_with_color = RootWithColor(Color.red)
1069+
root_with_order = RootWithOrder(Order.one)
10491070

10501071
logfire.info(
10511072
'hi',
@@ -1060,6 +1081,10 @@ class Model(BaseModel):
10601081
with_bool_inner=root_with_bool.root,
10611082
with_none=root_with_none,
10621083
with_none_inner=root_with_none.root,
1084+
with_color=root_with_color,
1085+
with_color_inner=root_with_color.root,
1086+
with_order=root_with_order,
1087+
with_order_inner=root_with_order.root,
10631088
)
10641089

10651090
assert capsys.readouterr().out.splitlines() == snapshot(
@@ -1079,5 +1104,9 @@ class Model(BaseModel):
10791104
'│ with_bool_inner=False',
10801105
'│ with_none=None',
10811106
'│ with_none_inner=None',
1107+
"│ with_color=Color('RED')",
1108+
"│ with_color_inner=Color('RED')",
1109+
'│ with_order=Order(1)',
1110+
'│ with_order_inner=Order(1)',
10821111
]
10831112
)

tests/test_json_args.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333
from logfire.testing import TestExporter
3434

3535
if sys.version_info >= (3, 11): # pragma: no branch
36-
from enum import StrEnum
36+
from enum import IntEnum, StrEnum
3737
else: # pragma: no cover
3838

3939
class StrEnum(str, Enum): ...
4040

41+
class IntEnum(int, Enum): ...
42+
4143

4244
pandas.set_option('display.max_columns', 10)
4345
pandas.set_option('display.max_rows', 20)
@@ -1384,12 +1386,21 @@ def test_pydantic_root_model(exporter: TestExporter):
13841386
class Model(BaseModel):
13851387
name: str
13861388

1389+
class Color(StrEnum):
1390+
red = 'RED'
1391+
1392+
class Order(IntEnum):
1393+
one = 1
1394+
13871395
RootWithModel = RootModel[Model]
13881396
RootWithStr = RootModel[str]
13891397
RootWithInt = RootModel[int]
13901398
RootWithFloat = RootModel[float]
13911399
RootWithBool = RootModel[bool]
13921400
RootWithNone = RootModel[None]
1401+
# enums (which are subclasses of their base types)
1402+
RootWithColor = RootModel[Color]
1403+
RootWithOrder = RootModel[Order]
13931404

13941405
model = Model(name='with_model')
13951406
root_with_model = RootWithModel(root=model)
@@ -1398,6 +1409,8 @@ class Model(BaseModel):
13981409
root_with_float = RootWithFloat(2.0)
13991410
root_with_bool = RootWithBool(False)
14001411
root_with_none = RootWithNone(None)
1412+
root_with_color = RootWithColor(Color.red)
1413+
root_with_order = RootWithOrder(Order.one)
14011414

14021415
logfire.info(
14031416
'hi',
@@ -1412,6 +1425,10 @@ class Model(BaseModel):
14121425
with_bool_inner=root_with_bool.root,
14131426
with_none=root_with_none,
14141427
with_none_inner=root_with_none.root,
1428+
with_color=root_with_color,
1429+
with_color_inner=root_with_color.root,
1430+
with_order=root_with_order,
1431+
with_order_inner=root_with_order.root,
14151432
)
14161433

14171434
assert exporter.exported_spans_as_dict() == [
@@ -1442,7 +1459,11 @@ class Model(BaseModel):
14421459
'with_bool_inner': False,
14431460
'with_none': 'null',
14441461
'with_none_inner': 'null',
1445-
'logfire.json_schema': '{"type":"object","properties":{"with_model":{"type":"object","title":"Model","x-python-datatype":"PydanticModel"},"with_str":{"type":"string","x-python-datatype":"string","title":"str"},"with_str_inner":{},"with_int":{"type":"integer","x-python-datatype":"number","title":"int"},"with_int_inner":{},"with_float":{"type":"number","x-python-datatype":"number","title":"float"},"with_float_inner":{},"with_bool":{"type":"boolean","x-python-datatype":null,"title":"bool"},"with_bool_inner":{},"with_none":{"type":"null"},"with_none_inner":{"type":"null"}}}',
1462+
'with_color': '"RED"',
1463+
'with_color_inner': '"RED"',
1464+
'with_order': '1',
1465+
'with_order_inner': '1',
1466+
'logfire.json_schema': '{"type":"object","properties":{"with_model":{"type":"object","title":"Model","x-python-datatype":"PydanticModel"},"with_str":{"type":"string","x-python-datatype":"string","title":"str"},"with_str_inner":{},"with_int":{"type":"integer","x-python-datatype":"number","title":"int"},"with_int_inner":{},"with_float":{"type":"number","x-python-datatype":"number","title":"float"},"with_float_inner":{},"with_bool":{"type":"boolean","x-python-datatype":null,"title":"bool"},"with_bool_inner":{},"with_none":{"type":"null"},"with_none_inner":{"type":"null"},"with_color":{"type":"string","x-python-datatype":"string","title":"Color"},"with_color_inner":{"type":"string","title":"Color","x-python-datatype":"Enum","enum":["RED"]},"with_order":{"type":"integer","x-python-datatype":"number","title":"Order"},"with_order_inner":{"type":"integer","title":"Order","x-python-datatype":"Enum","enum":[1]}}}',
14461467
},
14471468
}
14481469
]

0 commit comments

Comments
 (0)