Skip to content

Commit 0ec5fe8

Browse files
committed
Mack async work in python 3.7
1 parent d8dd55a commit 0ec5fe8

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

tests/integrations/anthropic/test_anthropic.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
from unittest import mock
22

3+
try:
4+
from unittest.mock import AsyncMock
5+
except ImportError:
6+
7+
class AsyncMock(mock.MagicMock):
8+
async def __call__(self, *args, **kwargs):
9+
return super(AsyncMock, self).__call__(*args, **kwargs)
10+
11+
312
import pytest
413
from anthropic import AsyncAnthropic, Anthropic, AnthropicError, AsyncStream, Stream
514
from anthropic.types import MessageDeltaUsage, TextDelta, Usage
@@ -140,7 +149,7 @@ async def test_nonstreaming_create_message_async(
140149
)
141150
events = capture_events()
142151
client = AsyncAnthropic(api_key="z")
143-
client.messages._post = mock.AsyncMock(return_value=EXAMPLE_MESSAGE)
152+
client.messages._post = AsyncMock(return_value=EXAMPLE_MESSAGE)
144153

145154
messages = [
146155
{
@@ -344,7 +353,7 @@ async def test_streaming_create_message_async(
344353
send_default_pii=send_default_pii,
345354
)
346355
events = capture_events()
347-
client.messages._post = mock.AsyncMock(return_value=returned_stream)
356+
client.messages._post = AsyncMock(return_value=returned_stream)
348357

349358
messages = [
350359
{
@@ -611,7 +620,7 @@ async def test_streaming_create_message_with_input_json_delta_async(
611620
send_default_pii=send_default_pii,
612621
)
613622
events = capture_events()
614-
client.messages._post = mock.AsyncMock(return_value=returned_stream)
623+
client.messages._post = AsyncMock(return_value=returned_stream)
615624

616625
messages = [
617626
{
@@ -683,7 +692,7 @@ async def test_exception_message_create_async(sentry_init, capture_events):
683692
events = capture_events()
684693

685694
client = AsyncAnthropic(api_key="z")
686-
client.messages._post = mock.AsyncMock(
695+
client.messages._post = AsyncMock(
687696
side_effect=AnthropicError("API rate limit reached")
688697
)
689698
with pytest.raises(AnthropicError):
@@ -732,7 +741,7 @@ async def test_span_origin_async(sentry_init, capture_events):
732741
events = capture_events()
733742

734743
client = AsyncAnthropic(api_key="z")
735-
client.messages._post = mock.AsyncMock(return_value=EXAMPLE_MESSAGE)
744+
client.messages._post = AsyncMock(return_value=EXAMPLE_MESSAGE)
736745

737746
messages = [
738747
{

0 commit comments

Comments
 (0)