-
Notifications
You must be signed in to change notification settings - Fork 16.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core: Use Blockbuster to detect blocking calls in asyncio during tests #29043
Changes from all commits
32c0a93
45e808e
350fc67
3d80e9a
36ac6bf
66ec4be
c871011
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,41 @@ | ||
"""Configuration for unit tests.""" | ||
|
||
from collections.abc import Sequence | ||
from collections.abc import Iterator, Sequence | ||
from importlib import util | ||
from uuid import UUID | ||
|
||
import pytest | ||
from blockbuster import BlockBuster, blockbuster_ctx | ||
from pytest import Config, Function, Parser | ||
from pytest_mock import MockerFixture | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def blockbuster() -> Iterator[BlockBuster]: | ||
with blockbuster_ctx("langchain_core") as bb: | ||
for func in ["os.stat", "os.path.abspath"]: | ||
( | ||
bb.functions[func] | ||
.can_block_in("langchain_core/_api/internal.py", "is_caller_internal") | ||
.can_block_in("langchain_core/runnables/base.py", "__repr__") | ||
.can_block_in( | ||
"langchain_core/beta/runnables/context.py", "aconfig_with_context" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will be dealt with in another PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! |
||
) | ||
) | ||
|
||
for func in ["os.stat", "io.TextIOWrapper.read"]: | ||
bb.functions[func].can_block_in( | ||
"langsmith/client.py", "_default_retry_config" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably something to be fixed in LangSmith. |
||
) | ||
|
||
for bb_function in bb.functions.values(): | ||
bb_function.can_block_in( | ||
"freezegun/api.py", "_get_cached_module_attributes" | ||
) | ||
|
||
yield bb | ||
|
||
|
||
def pytest_addoption(parser: Parser) -> None: | ||
"""Add custom command line options to pytest.""" | ||
parser.addoption( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import base64 | ||
import tempfile | ||
import warnings | ||
from pathlib import Path | ||
from typing import Any, Union, cast | ||
|
@@ -727,44 +725,39 @@ async def test_chat_tmpl_from_messages_multipart_image() -> None: | |
async def test_chat_tmpl_from_messages_multipart_formatting_with_path() -> None: | ||
"""Verify that we cannot pass `path` for an image as a variable.""" | ||
in_mem = "base64mem" | ||
in_file_data = "base64file01" | ||
|
||
with tempfile.NamedTemporaryFile(delete=True, suffix=".jpg") as temp_file: | ||
temp_file.write(base64.b64decode(in_file_data)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's useless to use a real file here. |
||
temp_file.flush() | ||
|
||
template = ChatPromptTemplate.from_messages( | ||
[ | ||
("system", "You are an AI assistant named {name}."), | ||
( | ||
"human", | ||
[ | ||
{"type": "text", "text": "What's in this image?"}, | ||
{ | ||
"type": "image_url", | ||
"image_url": "data:image/jpeg;base64,{in_mem}", | ||
}, | ||
{ | ||
"type": "image_url", | ||
"image_url": {"path": "{file_path}"}, | ||
}, | ||
], | ||
), | ||
] | ||
template = ChatPromptTemplate.from_messages( | ||
[ | ||
("system", "You are an AI assistant named {name}."), | ||
( | ||
"human", | ||
[ | ||
{"type": "text", "text": "What's in this image?"}, | ||
{ | ||
"type": "image_url", | ||
"image_url": "data:image/jpeg;base64,{in_mem}", | ||
}, | ||
{ | ||
"type": "image_url", | ||
"image_url": {"path": "{file_path}"}, | ||
}, | ||
], | ||
), | ||
] | ||
) | ||
with pytest.raises(ValueError): | ||
template.format_messages( | ||
name="R2D2", | ||
in_mem=in_mem, | ||
file_path="some/path", | ||
) | ||
with pytest.raises(ValueError): | ||
template.format_messages( | ||
name="R2D2", | ||
in_mem=in_mem, | ||
file_path=temp_file.name, | ||
) | ||
|
||
with pytest.raises(ValueError): | ||
await template.aformat_messages( | ||
name="R2D2", | ||
in_mem=in_mem, | ||
file_path=temp_file.name, | ||
) | ||
with pytest.raises(ValueError): | ||
await template.aformat_messages( | ||
name="R2D2", | ||
in_mem=in_mem, | ||
file_path="some/path", | ||
) | ||
|
||
|
||
def test_messages_placeholder() -> None: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RunnableLambda's
__repr__
callsget_lambda_source
which is blocking. It should probably be cached.