Skip to content

Commit cd7dce6

Browse files
authored
standard-tests: Add ruff rule UP (pyupgrade) (#31842)
See https://docs.astral.sh/ruff/rules/#pyupgrade-up All auto-fixed
1 parent 802d2bf commit cd7dce6

File tree

16 files changed

+63
-71
lines changed

16 files changed

+63
-71
lines changed

libs/standard-tests/langchain_tests/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from abc import ABC
2-
from typing import Type
32

43

54
class BaseStandardTests(ABC):
@@ -16,7 +15,7 @@ def test_no_overrides_DO_NOT_OVERRIDE(self) -> None:
1615
# find path to standard test implementations
1716
comparison_class = None
1817

19-
def explore_bases(cls: Type) -> None:
18+
def explore_bases(cls: type) -> None:
2019
nonlocal comparison_class
2120
for base in cls.__bases__:
2221
if base.__module__.startswith("langchain_tests."):

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"""
88

99
from abc import abstractmethod
10-
from typing import AsyncGenerator, Generator, Generic, Tuple, TypeVar
10+
from collections.abc import AsyncGenerator, Generator
11+
from typing import Generic, TypeVar
1112

1213
import pytest
1314
from langchain_core.stores import BaseStore
@@ -38,11 +39,11 @@ def kv_store(self) -> BaseStore[str, V]:
3839

3940
@abstractmethod
4041
@pytest.fixture()
41-
def three_values(self) -> Tuple[V, V, V]:
42+
def three_values(self) -> tuple[V, V, V]:
4243
"""Three example values that will be used in the tests."""
4344
pass
4445

45-
def test_three_values(self, three_values: Tuple[V, V, V]) -> None:
46+
def test_three_values(self, three_values: tuple[V, V, V]) -> None:
4647
"""Test that the fixture provides three values."""
4748
assert isinstance(three_values, tuple)
4849
assert len(three_values) == 3
@@ -53,7 +54,7 @@ def test_kv_store_is_empty(self, kv_store: BaseStore[str, V]) -> None:
5354
assert kv_store.mget(keys) == [None, None, None]
5455

5556
def test_set_and_get_values(
56-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
57+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
5758
) -> None:
5859
"""Test setting and getting values in the key-value store."""
5960
foo = three_values[0]
@@ -72,7 +73,7 @@ def test_store_still_empty(self, kv_store: BaseStore[str, V]) -> None:
7273
assert kv_store.mget(keys) == [None]
7374

7475
def test_delete_values(
75-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
76+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
7677
) -> None:
7778
"""Test deleting values from the key-value store."""
7879
foo = three_values[0]
@@ -83,7 +84,7 @@ def test_delete_values(
8384
assert kv_store.mget(["foo", "bar"]) == [None, bar]
8485

8586
def test_delete_bulk_values(
86-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
87+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
8788
) -> None:
8889
"""Test that we can delete several values at once."""
8990
foo, bar, buz = three_values
@@ -98,7 +99,7 @@ def test_delete_missing_keys(self, kv_store: BaseStore[str, V]) -> None:
9899
kv_store.mdelete(["foo", "bar", "baz"])
99100

100101
def test_set_values_is_idempotent(
101-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
102+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
102103
) -> None:
103104
"""Setting values by key should be idempotent."""
104105
foo, bar, _ = three_values
@@ -109,7 +110,7 @@ def test_set_values_is_idempotent(
109110
assert sorted(kv_store.yield_keys()) == ["bar", "foo"]
110111

111112
def test_get_can_get_same_value(
112-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
113+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
113114
) -> None:
114115
"""Test that the same value can be retrieved multiple times."""
115116
foo, bar, _ = three_values
@@ -119,7 +120,7 @@ def test_get_can_get_same_value(
119120
assert kv_store.mget(["foo", "bar", "foo", "bar"]) == [foo, bar, foo, bar]
120121

121122
def test_overwrite_values_by_key(
122-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
123+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
123124
) -> None:
124125
"""Test that we can overwrite values by key using mset."""
125126
foo, bar, buzz = three_values
@@ -134,7 +135,7 @@ def test_overwrite_values_by_key(
134135
assert kv_store.mget(["foo", "bar"]) == [buzz, bar]
135136

136137
def test_yield_keys(
137-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
138+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
138139
) -> None:
139140
"""Test that we can yield keys from the store."""
140141
foo, bar, _buzz = three_values
@@ -169,11 +170,11 @@ async def kv_store(self) -> BaseStore[str, V]:
169170

170171
@abstractmethod
171172
@pytest.fixture()
172-
def three_values(self) -> Tuple[V, V, V]:
173+
def three_values(self) -> tuple[V, V, V]:
173174
"""Three example values that will be used in the tests."""
174175
pass
175176

176-
async def test_three_values(self, three_values: Tuple[V, V, V]) -> None:
177+
async def test_three_values(self, three_values: tuple[V, V, V]) -> None:
177178
"""Test that the fixture provides three values."""
178179
assert isinstance(three_values, tuple)
179180
assert len(three_values) == 3
@@ -184,7 +185,7 @@ async def test_kv_store_is_empty(self, kv_store: BaseStore[str, V]) -> None:
184185
assert await kv_store.amget(keys) == [None, None, None]
185186

186187
async def test_set_and_get_values(
187-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
188+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
188189
) -> None:
189190
"""Test setting and getting values in the key-value store."""
190191
foo = three_values[0]
@@ -203,7 +204,7 @@ async def test_store_still_empty(self, kv_store: BaseStore[str, V]) -> None:
203204
assert await kv_store.amget(keys) == [None]
204205

205206
async def test_delete_values(
206-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
207+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
207208
) -> None:
208209
"""Test deleting values from the key-value store."""
209210
foo = three_values[0]
@@ -214,7 +215,7 @@ async def test_delete_values(
214215
assert await kv_store.amget(["foo", "bar"]) == [None, bar]
215216

216217
async def test_delete_bulk_values(
217-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
218+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
218219
) -> None:
219220
"""Test that we can delete several values at once."""
220221
foo, bar, buz = three_values
@@ -229,7 +230,7 @@ async def test_delete_missing_keys(self, kv_store: BaseStore[str, V]) -> None:
229230
await kv_store.amdelete(["foo", "bar", "baz"])
230231

231232
async def test_set_values_is_idempotent(
232-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
233+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
233234
) -> None:
234235
"""Setting values by key should be idempotent."""
235236
foo, bar, _ = three_values
@@ -240,7 +241,7 @@ async def test_set_values_is_idempotent(
240241
assert sorted(kv_store.yield_keys()) == ["bar", "foo"]
241242

242243
async def test_get_can_get_same_value(
243-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
244+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
244245
) -> None:
245246
"""Test that the same value can be retrieved multiple times."""
246247
foo, bar, _ = three_values
@@ -255,7 +256,7 @@ async def test_get_can_get_same_value(
255256
]
256257

257258
async def test_overwrite_values_by_key(
258-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
259+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
259260
) -> None:
260261
"""Test that we can overwrite values by key using mset."""
261262
foo, bar, buzz = three_values
@@ -270,7 +271,7 @@ async def test_overwrite_values_by_key(
270271
assert await kv_store.amget(["foo", "bar"]) == [buzz, bar]
271272

272273
async def test_yield_keys(
273-
self, kv_store: BaseStore[str, V], three_values: Tuple[V, V, V]
274+
self, kv_store: BaseStore[str, V], three_values: tuple[V, V, V]
274275
) -> None:
275276
"""Test that we can yield keys from the store."""
276277
foo, bar, _buzz = three_values

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import base64
22
import inspect
33
import json
4-
from typing import Any, List, Literal, Optional, cast
4+
from typing import Annotated, Any, Literal, Optional, cast
55
from unittest.mock import MagicMock
66

77
import httpx
@@ -29,7 +29,7 @@
2929
from pydantic.v1 import BaseModel as BaseModelV1
3030
from pydantic.v1 import Field as FieldV1
3131
from pytest_benchmark.fixture import BenchmarkFixture # type: ignore[import-untyped]
32-
from typing_extensions import Annotated, TypedDict
32+
from typing_extensions import TypedDict
3333
from vcr.cassette import Cassette
3434

3535
from langchain_tests.unit_tests.chat_models import (
@@ -2680,7 +2680,7 @@ def supports_anthropic_inputs(self) -> bool:
26802680
"cache_control": {"type": "ephemeral"},
26812681
}
26822682

2683-
human_content: List[dict] = [
2683+
human_content: list[dict] = [
26842684
{
26852685
"type": "text",
26862686
"text": "what's your favorite color in this image",

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import List
2-
31
from langchain_core.embeddings import Embeddings
42

53
from langchain_tests.unit_tests.embeddings import EmbeddingsTests
@@ -49,7 +47,7 @@ def test_embed_query(self, model: Embeddings) -> None:
4947
""" # noqa: E501
5048
embedding_1 = model.embed_query("foo")
5149

52-
assert isinstance(embedding_1, List)
50+
assert isinstance(embedding_1, list)
5351
assert isinstance(embedding_1[0], float)
5452

5553
embedding_2 = model.embed_query("bar")
@@ -71,7 +69,7 @@ def test_embed_documents(self, model: Embeddings) -> None:
7169
embeddings = model.embed_documents(documents)
7270

7371
assert len(embeddings) == len(documents)
74-
assert all(isinstance(embedding, List) for embedding in embeddings)
72+
assert all(isinstance(embedding, list) for embedding in embeddings)
7573
assert all(isinstance(embedding[0], float) for embedding in embeddings)
7674
assert len(embeddings[0]) > 0
7775
assert all(len(embedding) == len(embeddings[0]) for embedding in embeddings)
@@ -88,7 +86,7 @@ async def test_aembed_query(self, model: Embeddings) -> None:
8886
""" # noqa: E501
8987
embedding_1 = await model.aembed_query("foo")
9088

91-
assert isinstance(embedding_1, List)
89+
assert isinstance(embedding_1, list)
9290
assert isinstance(embedding_1[0], float)
9391

9492
embedding_2 = await model.aembed_query("bar")
@@ -110,7 +108,7 @@ async def test_aembed_documents(self, model: Embeddings) -> None:
110108
embeddings = await model.aembed_documents(documents)
111109

112110
assert len(embeddings) == len(documents)
113-
assert all(isinstance(embedding, List) for embedding in embeddings)
111+
assert all(isinstance(embedding, list) for embedding in embeddings)
114112
assert all(isinstance(embedding[0], float) for embedding in embeddings)
115113
assert len(embeddings[0]) > 0
116114
assert all(len(embedding) == len(embeddings[0]) for embedding in embeddings)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import inspect
1212
import uuid
1313
from abc import ABC, abstractmethod
14-
from typing import AsyncGenerator, Generator
14+
from collections.abc import AsyncGenerator, Generator
1515

1616
import pytest
1717
from langchain_core.documents import Document

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from abc import abstractmethod
2-
from typing import Type
32

43
import pytest
54
from langchain_core.documents import Document
@@ -15,7 +14,7 @@ class RetrieversIntegrationTests(BaseStandardTests):
1514

1615
@property
1716
@abstractmethod
18-
def retriever_constructor(self) -> Type[BaseRetriever]:
17+
def retriever_constructor(self) -> type[BaseRetriever]:
1918
"""
2019
A BaseRetriever subclass to be tested.
2120
"""

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import inspect
66
import os
77
from abc import abstractmethod
8-
from typing import Any, Dict, List, Literal, Optional, Tuple, Type
8+
from typing import Any, Literal, Optional
99
from unittest import mock
1010

1111
import pytest
@@ -78,7 +78,7 @@ class ChatModelTests(BaseStandardTests):
7878

7979
@property
8080
@abstractmethod
81-
def chat_model_class(self) -> Type[BaseChatModel]:
81+
def chat_model_class(self) -> type[BaseChatModel]:
8282
"""The chat model class to test, e.g., ``ChatParrotLink``."""
8383
...
8484

@@ -214,9 +214,9 @@ def enable_vcr_tests(self) -> bool:
214214
@property
215215
def supported_usage_metadata_details(
216216
self,
217-
) -> Dict[
217+
) -> dict[
218218
Literal["invoke", "stream"],
219-
List[
219+
list[
220220
Literal[
221221
"audio_input",
222222
"audio_output",
@@ -804,7 +804,7 @@ def standard_chat_model_params(self) -> dict:
804804
return params
805805

806806
@property
807-
def init_from_env_params(self) -> Tuple[dict, dict, dict]:
807+
def init_from_env_params(self) -> tuple[dict, dict, dict]:
808808
"""(tuple) environment variables, additional initialization args, and expected
809809
instance attributes for testing initialization from environment variables."""
810810
return {}, {}, {}
@@ -958,7 +958,7 @@ class ExpectedParams(BaseModelV1):
958958
ls_model_type: Literal["chat"]
959959
ls_temperature: Optional[float]
960960
ls_max_tokens: Optional[int]
961-
ls_stop: Optional[List[str]]
961+
ls_stop: Optional[list[str]]
962962

963963
ls_params = model._get_ls_params()
964964
try:

libs/standard-tests/langchain_tests/unit_tests/embeddings.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
from abc import abstractmethod
3-
from typing import Tuple, Type
43
from unittest import mock
54

65
import pytest
@@ -17,7 +16,7 @@ class EmbeddingsTests(BaseStandardTests):
1716

1817
@property
1918
@abstractmethod
20-
def embeddings_class(self) -> Type[Embeddings]: ...
19+
def embeddings_class(self) -> type[Embeddings]: ...
2120

2221
@property
2322
def embedding_model_params(self) -> dict:
@@ -103,7 +102,7 @@ def test_init(self) -> None:
103102
assert model is not None
104103

105104
@property
106-
def init_from_env_params(self) -> Tuple[dict, dict, dict]:
105+
def init_from_env_params(self) -> tuple[dict, dict, dict]:
107106
"""This property is used in unit tests to test initialization from environment
108107
variables. It should return a tuple of three dictionaries that specify the
109108
environment variables, additional initialization args, and expected instance

libs/standard-tests/langchain_tests/unit_tests/tools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from abc import abstractmethod
3-
from typing import Tuple, Type, Union
3+
from typing import Union
44
from unittest import mock
55

66
import pytest
@@ -19,7 +19,7 @@ class ToolsTests(BaseStandardTests):
1919

2020
@property
2121
@abstractmethod
22-
def tool_constructor(self) -> Union[Type[BaseTool], BaseTool]:
22+
def tool_constructor(self) -> Union[type[BaseTool], BaseTool]:
2323
"""
2424
Returns a class or instance of a tool to be tested.
2525
"""
@@ -64,7 +64,7 @@ class ToolsUnitTests(ToolsTests):
6464
"""
6565

6666
@property
67-
def init_from_env_params(self) -> Tuple[dict, dict, dict]:
67+
def init_from_env_params(self) -> tuple[dict, dict, dict]:
6868
"""Return env vars, init args, and expected instance attrs for initializing
6969
from env vars."""
7070
return {}, {}, {}

libs/standard-tests/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ ignore_missing_imports = true
5555
target-version = "py39"
5656

5757
[tool.ruff.lint]
58-
select = ["E", "F", "I", "T201"]
58+
select = ["E", "F", "I", "T201", "UP",]
59+
ignore = ["UP007",]
5960

6061
[tool.coverage.run]
6162
omit = ["tests/*"]

0 commit comments

Comments
 (0)