Skip to content

Commit e2b6724

Browse files
ronakrmclaude
andcommitted
Add tests and fix type checking for 100% coverage
- Add test_cache_point_in_otel_message_parts to cover CachePoint in otel conversion - Add test_cache_control_unsupported_param_type to cover unsupported param error - Use .get() for TypedDict access to avoid type checking errors - Add type: ignore for testing protected method - Restore pragma: lax no cover on google.py file_data handling 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent cece974 commit e2b6724

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

tests/models/test_anthropic.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,40 @@ async def test_cache_point_with_image_content(allow_model_requests: None):
401401
assert 'cache_control' not in content[1]
402402

403403

404+
async def test_cache_point_in_otel_message_parts(allow_model_requests: None):
405+
"""Test that CachePoint is handled correctly in otel message parts conversion."""
406+
from pydantic_ai.agent import InstrumentationSettings
407+
from pydantic_ai.messages import UserPromptPart
408+
409+
# Create a UserPromptPart with CachePoint
410+
part = UserPromptPart(content=['text before', CachePoint(), 'text after'])
411+
412+
# Convert to otel message parts
413+
settings = InstrumentationSettings(include_content=True)
414+
otel_parts = part.otel_message_parts(settings)
415+
416+
# Should have 2 text parts, CachePoint is skipped
417+
assert len(otel_parts) == 2
418+
assert otel_parts[0]['type'] == 'text'
419+
assert otel_parts[0].get('content') == 'text before'
420+
assert otel_parts[1]['type'] == 'text'
421+
assert otel_parts[1].get('content') == 'text after'
422+
423+
424+
def test_cache_control_unsupported_param_type():
425+
"""Test that cache control raises error for unsupported param types."""
426+
427+
from pydantic_ai.exceptions import UserError
428+
from pydantic_ai.models.anthropic import AnthropicModel
429+
430+
# Create a list with an unsupported param type (document)
431+
# We'll use a mock document block param
432+
params: list[dict[str, Any]] = [{'type': 'document', 'source': {'data': 'test'}}]
433+
434+
with pytest.raises(UserError, match='Cache control not supported for param type: document'):
435+
AnthropicModel._add_cache_control_to_last_param(params) # type: ignore[arg-type] # Testing internal method
436+
437+
404438
async def test_async_request_text_response(allow_model_requests: None):
405439
c = completion_message(
406440
[BetaTextBlock(text='world', type='text')],

0 commit comments

Comments
 (0)