Skip to content

Commit 4a751cb

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 7259061 commit 4a751cb

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
@@ -393,6 +393,40 @@ async def test_cache_point_with_image_content(allow_model_requests: None):
393393
assert 'cache_control' not in content[1]
394394

395395

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

0 commit comments

Comments
 (0)