Skip to content

Commit 05d1477

Browse files
authored
chore(standard-tests): enable ruff docstring-code-format (#32852)
1 parent 33c7f23 commit 05d1477

File tree

6 files changed

+207
-133
lines changed

6 files changed

+207
-133
lines changed

libs/standard-tests/langchain_tests/integration_tests/chat_models.py

Lines changed: 111 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@ def supports_audio_inputs(self) -> bool:
428428
on invoke and streaming responses. Defaults to ``True``.
429429
430430
``usage_metadata`` is an optional dict attribute on ``AIMessage``s that track input
431-
and output tokens. `See more. <https://python.langchain.com/api_reference/core/messages/langchain_core.messages.ai.UsageMetadata.html>`__
431+
and output tokens.
432+
`See more. <https://python.langchain.com/api_reference/core/messages/langchain_core.messages.ai.UsageMetadata.html>`__
432433
433434
Example:
434435
@@ -525,7 +526,8 @@ def supports_image_tool_message(self) -> bool:
525526
and stream.
526527
527528
``usage_metadata`` is an optional dict attribute on ``AIMessage``s that track input
528-
and output tokens. `See more. <https://python.langchain.com/api_reference/core/messages/langchain_core.messages.ai.UsageMetadata.html>`__
529+
and output tokens.
530+
`See more. <https://python.langchain.com/api_reference/core/messages/langchain_core.messages.ai.UsageMetadata.html>`__
529531
530532
It includes optional keys ``input_token_details`` and ``output_token_details``
531533
that can track usage details associated with special types of tokens, such as
@@ -549,7 +551,8 @@ def supports_image_tool_message(self) -> bool:
549551
def enable_vcr_tests(self) -> bool:
550552
return True
551553
552-
2. Configure VCR to exclude sensitive headers and other information from cassettes.
554+
2. Configure VCR to exclude sensitive headers and other information from
555+
cassettes.
553556
554557
.. important::
555558
VCR will by default record authentication headers and other sensitive
@@ -569,7 +572,9 @@ def enable_vcr_tests(self) -> bool:
569572
:caption: tests/conftest.py
570573
571574
import pytest
572-
from langchain_tests.conftest import _base_vcr_config as _base_vcr_config
575+
from langchain_tests.conftest import (
576+
_base_vcr_config as _base_vcr_config,
577+
)
573578
574579
_EXTRA_HEADERS = [
575580
# Specify additional headers to redact
@@ -603,8 +608,13 @@ def vcr_config(_base_vcr_config: dict) -> dict: # noqa: F811
603608
:caption: tests/conftest.py
604609
605610
import pytest
606-
from langchain_tests.conftest import CustomPersister, CustomSerializer
607-
from langchain_tests.conftest import _base_vcr_config as _base_vcr_config
611+
from langchain_tests.conftest import (
612+
CustomPersister,
613+
CustomSerializer,
614+
)
615+
from langchain_tests.conftest import (
616+
_base_vcr_config as _base_vcr_config,
617+
)
608618
from vcr import VCR
609619
610620
_EXTRA_HEADERS = [
@@ -648,10 +658,15 @@ def pytest_recording_configure(config: dict, vcr: VCR) -> None:
648658
649659
.. code-block:: python
650660
651-
from langchain_tests.conftest import CustomPersister, CustomSerializer
661+
from langchain_tests.conftest import (
662+
CustomPersister,
663+
CustomSerializer,
664+
)
652665
653666
cassette_path = "/path/to/tests/cassettes/TestClass_test.yaml.gz"
654-
requests, responses = CustomPersister().load_cassette(path, CustomSerializer())
667+
requests, responses = CustomPersister().load_cassette(
668+
path, CustomSerializer()
669+
)
655670
656671
3. Run tests to generate VCR cassettes.
657672
@@ -697,9 +712,9 @@ def test_invoke(self, model: BaseChatModel) -> None:
697712
.. code-block:: python
698713
699714
return ChatResult(
700-
generations=[ChatGeneration(
701-
message=AIMessage(content="Output text")
702-
)]
715+
generations=[
716+
ChatGeneration(message=AIMessage(content="Output text"))
717+
]
703718
)
704719
705720
"""
@@ -730,9 +745,9 @@ async def test_ainvoke(self, model: BaseChatModel) -> None:
730745
.. code-block:: python
731746
732747
return ChatResult(
733-
generations=[ChatGeneration(
734-
message=AIMessage(content="Output text")
735-
)]
748+
generations=[
749+
ChatGeneration(message=AIMessage(content="Output text"))
750+
]
736751
)
737752
738753
"""
@@ -763,9 +778,7 @@ def test_stream(self, model: BaseChatModel) -> None:
763778
764779
.. code-block:: python
765780
766-
yield ChatGenerationChunk(
767-
message=AIMessageChunk(content="chunk text")
768-
)
781+
yield ChatGenerationChunk(message=AIMessageChunk(content="chunk text"))
769782
770783
"""
771784
num_chunks = 0
@@ -800,9 +813,7 @@ async def test_astream(self, model: BaseChatModel) -> None:
800813
801814
.. code-block:: python
802815
803-
yield ChatGenerationChunk(
804-
message=AIMessageChunk(content="chunk text")
805-
)
816+
yield ChatGenerationChunk(message=AIMessageChunk(content="chunk text"))
806817
807818
"""
808819
num_chunks = 0
@@ -920,10 +931,11 @@ def test_double_messages_conversation(self, model: BaseChatModel) -> None:
920931
because this test is the "basic case" without double messages.
921932
922933
If that test passes those but not this one, you should verify that:
923-
1. Your model API can handle double messages, or the integration should merge messages before sending them to the API.
934+
1. Your model API can handle double messages, or the integration should
935+
merge messages before sending them to the API.
924936
2. The response is a valid :class:`~langchain_core.messages.AIMessage`
925937
926-
""" # noqa: E501
938+
"""
927939
messages = [
928940
SystemMessage("hello"),
929941
SystemMessage("hello"),
@@ -1002,25 +1014,27 @@ def supported_usage_metadata_details(self) -> dict:
10021014
.. code-block:: python
10031015
10041016
return ChatResult(
1005-
generations=[ChatGeneration(
1006-
message=AIMessage(
1007-
content="Output text",
1008-
usage_metadata={
1009-
"input_tokens": 350,
1010-
"output_tokens": 240,
1011-
"total_tokens": 590,
1012-
"input_token_details": {
1013-
"audio": 10,
1014-
"cache_creation": 200,
1015-
"cache_read": 100,
1017+
generations=[
1018+
ChatGeneration(
1019+
message=AIMessage(
1020+
content="Output text",
1021+
usage_metadata={
1022+
"input_tokens": 350,
1023+
"output_tokens": 240,
1024+
"total_tokens": 590,
1025+
"input_token_details": {
1026+
"audio": 10,
1027+
"cache_creation": 200,
1028+
"cache_read": 100,
1029+
},
1030+
"output_token_details": {
1031+
"audio": 10,
1032+
"reasoning": 200,
1033+
},
10161034
},
1017-
"output_token_details": {
1018-
"audio": 10,
1019-
"reasoning": 200,
1020-
}
1021-
}
1035+
)
10221036
)
1023-
)]
1037+
]
10241038
)
10251039
10261040
Check also that the response includes a ``'model_name'`` key in its
@@ -1173,29 +1187,31 @@ def supported_usage_metadata_details(self) -> dict:
11731187
.. code-block:: python
11741188
11751189
yield ChatResult(
1176-
generations=[ChatGeneration(
1177-
message=AIMessage(
1178-
content="Output text",
1179-
usage_metadata={
1180-
"input_tokens": (
1181-
num_input_tokens if is_first_chunk else 0
1182-
),
1183-
"output_tokens": 11,
1184-
"total_tokens": (
1185-
11+num_input_tokens if is_first_chunk else 11
1186-
),
1187-
"input_token_details": {
1188-
"audio": 10,
1189-
"cache_creation": 200,
1190-
"cache_read": 100,
1190+
generations=[
1191+
ChatGeneration(
1192+
message=AIMessage(
1193+
content="Output text",
1194+
usage_metadata={
1195+
"input_tokens": (
1196+
num_input_tokens if is_first_chunk else 0
1197+
),
1198+
"output_tokens": 11,
1199+
"total_tokens": (
1200+
11 + num_input_tokens if is_first_chunk else 11
1201+
),
1202+
"input_token_details": {
1203+
"audio": 10,
1204+
"cache_creation": 200,
1205+
"cache_read": 100,
1206+
},
1207+
"output_token_details": {
1208+
"audio": 10,
1209+
"reasoning": 200,
1210+
},
11911211
},
1192-
"output_token_details": {
1193-
"audio": 10,
1194-
"reasoning": 200,
1195-
}
1196-
}
1212+
)
11971213
)
1198-
)]
1214+
]
11991215
)
12001216
12011217
Check also that the aggregated response includes a ``'model_name'`` key
@@ -1538,20 +1554,25 @@ def has_tool_calling(self) -> bool:
15381554
15391555
If this test fails, check that:
15401556
1541-
1. The model can correctly handle message histories that include ``AIMessage`` objects with ``""`` content.
1542-
2. The ``tool_calls`` attribute on ``AIMessage`` objects is correctly handled and passed to the model in an appropriate format.
1543-
3. The model can correctly handle ``ToolMessage`` objects with string content and arbitrary string values for ``tool_call_id``.
1557+
1. The model can correctly handle message histories that include
1558+
``AIMessage`` objects with ``""`` content.
1559+
2. The ``tool_calls`` attribute on ``AIMessage`` objects is correctly
1560+
handled and passed to the model in an appropriate format.
1561+
3. The model can correctly handle ``ToolMessage`` objects with string
1562+
content and arbitrary string values for ``tool_call_id``.
15441563
15451564
You can ``xfail`` the test if tool calling is implemented but this format
15461565
is not supported.
15471566
15481567
.. code-block:: python
15491568
15501569
@pytest.mark.xfail(reason=("Not implemented."))
1551-
def test_tool_message_histories_string_content(self, *args: Any) -> None:
1570+
def test_tool_message_histories_string_content(
1571+
self, *args: Any
1572+
) -> None:
15521573
super().test_tool_message_histories_string_content(*args)
15531574
1554-
""" # noqa: E501
1575+
"""
15551576
if not self.has_tool_calling:
15561577
pytest.skip("Test requires tool calling.")
15571578

@@ -1625,9 +1646,12 @@ def has_tool_calling(self) -> bool:
16251646
16261647
If this test fails, check that:
16271648
1628-
1. The model can correctly handle message histories that include ``AIMessage`` objects with list content.
1629-
2. The ``tool_calls`` attribute on ``AIMessage`` objects is correctly handled and passed to the model in an appropriate format.
1630-
3. The model can correctly handle ToolMessage objects with string content and arbitrary string values for ``tool_call_id``.
1649+
1. The model can correctly handle message histories that include
1650+
``AIMessage`` objects with list content.
1651+
2. The ``tool_calls`` attribute on ``AIMessage`` objects is correctly
1652+
handled and passed to the model in an appropriate format.
1653+
3. The model can correctly handle ToolMessage objects with string content
1654+
and arbitrary string values for ``tool_call_id``.
16311655
16321656
You can ``xfail`` the test if tool calling is implemented but this format
16331657
is not supported.
@@ -1638,7 +1662,7 @@ def has_tool_calling(self) -> bool:
16381662
def test_tool_message_histories_list_content(self, *args: Any) -> None:
16391663
super().test_tool_message_histories_list_content(*args)
16401664
1641-
""" # noqa: E501
1665+
"""
16421666
if not self.has_tool_calling:
16431667
pytest.skip("Test requires tool calling.")
16441668

@@ -1766,13 +1790,15 @@ def has_tool_calling(self) -> bool:
17661790
.. code-block:: python
17671791
17681792
@pytest.mark.xfail(reason=("Does not support tool_choice."))
1769-
def test_tool_calling_with_no_arguments(self, model: BaseChatModel) -> None:
1793+
def test_tool_calling_with_no_arguments(
1794+
self, model: BaseChatModel
1795+
) -> None:
17701796
super().test_tool_calling_with_no_arguments(model)
17711797
17721798
Otherwise, in the case that only one tool is bound, ensure that
17731799
``tool_choice`` supports the string ``'any'`` to force calling that tool.
17741800
1775-
""" # noqa: E501
1801+
"""
17761802
if not self.has_tool_calling:
17771803
pytest.skip("Test requires tool calling.")
17781804

@@ -2317,7 +2343,6 @@ def test_pdf_inputs(self, model: BaseChatModel) -> None:
23172343
.. code-block:: python
23182344
23192345
class TestMyChatModelIntegration(ChatModelIntegrationTests):
2320-
23212346
@property
23222347
def supports_pdf_inputs(self) -> bool:
23232348
return False
@@ -2394,7 +2419,6 @@ def test_audio_inputs(self, model: BaseChatModel) -> None:
23942419
.. code-block:: python
23952420
23962421
class TestMyChatModelIntegration(ChatModelIntegrationTests):
2397-
23982422
@property
23992423
def supports_audio_inputs(self) -> bool:
24002424
return False
@@ -2704,7 +2728,8 @@ def test_anthropic_inputs(self, model: BaseChatModel) -> None:
27042728
"content": [
27052729
{
27062730
"type": "text",
2707-
"text": "green is a great pick! that's my sister's favorite color", # noqa: E501
2731+
"text": "green is a great pick! "
2732+
"that's my sister's favorite color",
27082733
}
27092734
],
27102735
"is_error": False,
@@ -2732,14 +2757,17 @@ def supports_anthropic_inputs(self) -> bool:
27322757
27332758
If this test fails, check that:
27342759
2735-
1. The model can correctly handle message histories that include message objects with list content.
2736-
2. The ``tool_calls`` attribute on AIMessage objects is correctly handled and passed to the model in an appropriate format.
2737-
3. ``HumanMessage``s with "tool_result" content blocks are correctly handled.
2760+
1. The model can correctly handle message histories that include message
2761+
objects with list content.
2762+
2. The ``tool_calls`` attribute on AIMessage objects is correctly handled
2763+
and passed to the model in an appropriate format.
2764+
3. ``HumanMessage``s with "tool_result" content blocks are correctly
2765+
handled.
27382766
27392767
Otherwise, if Anthropic tool call and result formats are not supported,
27402768
set the ``supports_anthropic_inputs`` property to False.
27412769
2742-
""" # noqa: E501
2770+
"""
27432771
if not self.supports_anthropic_inputs:
27442772
pytest.skip("Model does not explicitly support Anthropic inputs.")
27452773

@@ -3017,13 +3045,15 @@ def test_unicode_tool_call_integration(
30173045
30183046
Args:
30193047
model: The chat model to test
3020-
tool_choice: Tool choice parameter to pass to ``bind_tools()`` (provider-specific)
3021-
force_tool_call: Whether to force a tool call (use ``tool_choice=True`` if None)
3048+
tool_choice: Tool choice parameter to pass to ``bind_tools()``
3049+
(provider-specific)
3050+
force_tool_call: Whether to force a tool call
3051+
(use ``tool_choice=True`` if None)
30223052
30233053
Tests that Unicode characters in tool call arguments are preserved correctly,
30243054
not escaped as ``\\uXXXX`` sequences.
30253055
3026-
""" # noqa: E501
3056+
"""
30273057
if not self.has_tool_calling:
30283058
pytest.skip("Test requires tool calling support.")
30293059

0 commit comments

Comments
 (0)