Skip to content

Commit 32f37cc

Browse files
lionpelouxclaude
andcommitted
Define test classes at top of file instead of inline
As suggested by @DouweM, moved ResultGeneric and StringData classes to the top of the test file (after Person class) instead of defining them inline within the test function. This approach: - Avoids the string module approach (create_module) - Provides clean tool names in snapshots without function scope - Follows the existing pattern in the test file Tool names are now clean: final_result_ResultGenericStringData and final_result_ResultGenericint. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 2a7cdb5 commit 32f37cc

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

tests/test_agent.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from collections.abc import AsyncIterable, Callable
77
from dataclasses import dataclass, replace
88
from datetime import timezone
9-
from typing import Any, Literal, Union
9+
from typing import Any, Generic, Literal, TypeVar, Union
1010

1111
import httpx
1212
import pytest
@@ -97,6 +97,21 @@ class Person(BaseModel):
9797
name: str
9898

9999

100+
# Generic classes for testing tool name sanitization with generic types
101+
T = TypeVar('T')
102+
103+
104+
class ResultGeneric(BaseModel, Generic[T]):
105+
"""A generic result class."""
106+
107+
value: T
108+
success: bool
109+
110+
111+
class StringData(BaseModel):
112+
text: str
113+
114+
100115
def test_result_list_of_models_with_stringified_response():
101116
def return_list(_: list[ModelMessage], info: AgentInfo) -> ModelResponse:
102117
assert info.output_tools is not None
@@ -638,21 +653,8 @@ def validate_output(ctx: RunContext[None], o: Any) -> Any:
638653

639654
def test_output_type_generic_class_name_sanitization():
640655
"""Test that generic class names with brackets are properly sanitized."""
641-
from typing import Generic, TypeVar
642-
643-
T = TypeVar('T')
644-
645-
class Result(BaseModel, Generic[T]):
646-
"""A generic result class."""
647-
648-
value: T
649-
success: bool
650-
651-
class StringData(BaseModel):
652-
text: str
653-
654-
# This will have a name like "Result[StringData]" which needs sanitization
655-
output_type = [Result[StringData], Result[int]]
656+
# This will have a name like "ResultGeneric[StringData]" which needs sanitization
657+
output_type = [ResultGeneric[StringData], ResultGeneric[int]]
656658

657659
m = TestModel()
658660
agent = Agent(m, output_type=output_type)
@@ -664,12 +666,7 @@ class StringData(BaseModel):
664666
assert len(m.last_model_request_parameters.output_tools) == 2
665667

666668
tool_names = [tool.name for tool in m.last_model_request_parameters.output_tools]
667-
assert tool_names == snapshot(
668-
[
669-
'final_result_Resulttest_output_type_generic_class_name_sanitizationlocalsStringData',
670-
'final_result_Resultint',
671-
]
672-
)
669+
assert tool_names == snapshot(['final_result_ResultGenericStringData', 'final_result_ResultGenericint'])
673670

674671

675672
def test_output_type_with_two_descriptions():

0 commit comments

Comments
 (0)