|
| 1 | +"""Tests for exception classes.""" |
| 2 | + |
| 3 | +from collections.abc import Callable |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from pydantic_ai import ModelRetry |
| 9 | +from pydantic_ai.exceptions import ( |
| 10 | + AgentRunError, |
| 11 | + ApprovalRequired, |
| 12 | + CallDeferred, |
| 13 | + IncompleteToolCall, |
| 14 | + ModelHTTPError, |
| 15 | + UnexpectedModelBehavior, |
| 16 | + UsageLimitExceeded, |
| 17 | + UserError, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize( |
| 22 | + 'exc_factory', |
| 23 | + [ |
| 24 | + lambda: ModelRetry('test'), |
| 25 | + lambda: CallDeferred(), |
| 26 | + lambda: ApprovalRequired(), |
| 27 | + lambda: UserError('test'), |
| 28 | + lambda: AgentRunError('test'), |
| 29 | + lambda: UnexpectedModelBehavior('test'), |
| 30 | + lambda: UsageLimitExceeded('test'), |
| 31 | + lambda: ModelHTTPError(500, 'model'), |
| 32 | + lambda: IncompleteToolCall('test'), |
| 33 | + ], |
| 34 | + ids=[ |
| 35 | + 'ModelRetry', |
| 36 | + 'CallDeferred', |
| 37 | + 'ApprovalRequired', |
| 38 | + 'UserError', |
| 39 | + 'AgentRunError', |
| 40 | + 'UnexpectedModelBehavior', |
| 41 | + 'UsageLimitExceeded', |
| 42 | + 'ModelHTTPError', |
| 43 | + 'IncompleteToolCall', |
| 44 | + ], |
| 45 | +) |
| 46 | +def test_exceptions_hashable(exc_factory: Callable[[], Any]): |
| 47 | + """Test that all exception classes are hashable and usable as keys.""" |
| 48 | + exc = exc_factory() |
| 49 | + |
| 50 | + # Does not raise TypeError |
| 51 | + _ = hash(exc) |
| 52 | + |
| 53 | + # Can be used in sets and dicts |
| 54 | + s = {exc} |
| 55 | + d = {exc: 'value'} |
| 56 | + |
| 57 | + assert exc in s |
| 58 | + assert d[exc] == 'value' |
0 commit comments