text-splitters: Add ruff rule UP (pyupgrade) (#31841)

See https://docs.astral.sh/ruff/rules/#pyupgrade-up
All auto-fixed except `typing.AbstractSet` -> `collections.abc.Set`
This commit is contained in:
Christophe Bornet
2025-07-03 16:11:35 +02:00
committed by GitHub
parent 911b0b69ea
commit 802d2bf249
13 changed files with 106 additions and 115 deletions

View File

@@ -1,7 +1,7 @@
"""Configuration for unit tests."""
from collections.abc import Sequence
from importlib import util
from typing import Dict, Sequence
import pytest
from pytest import Config, Function, Parser
@@ -39,7 +39,7 @@ def pytest_collection_modifyitems(config: Config, items: Sequence[Function]) ->
"""
# Mapping from the name of a package to whether it is installed or not.
# Used to avoid repeated calls to `util.find_spec`
required_pkgs_info: Dict[str, bool] = {}
required_pkgs_info: dict[str, bool] = {}
only_extended = config.getoption("--only-extended") or False
only_core = config.getoption("--only-core") or False

View File

@@ -3,7 +3,7 @@
import random
import re
import string
from typing import Any, Callable, List, Tuple
from typing import Any, Callable
import pytest
from langchain_core.documents import Document
@@ -282,7 +282,7 @@ def test_create_documents_with_metadata() -> None:
],
)
def test_create_documents_with_start_index(
splitter: TextSplitter, text: str, expected_docs: List[Document]
splitter: TextSplitter, text: str, expected_docs: list[Document]
) -> None:
"""Test create documents method."""
docs = splitter.create_documents([text])
@@ -333,7 +333,7 @@ def test_iterative_text_splitter_discard_separator() -> None:
]
def __test_iterative_text_splitter(chunk_size: int, keep_separator: bool) -> List[str]:
def __test_iterative_text_splitter(chunk_size: int, keep_separator: bool) -> list[str]:
chunk_size += 1 if keep_separator else 0
splitter = RecursiveCharacterTextSplitter(
@@ -2224,7 +2224,7 @@ def test_haskell_code_splitter() -> None:
@pytest.fixture
@pytest.mark.requires("bs4")
def html_header_splitter_splitter_factory() -> Callable[
[List[Tuple[str, str]]], HTMLHeaderTextSplitter
[list[tuple[str, str]]], HTMLHeaderTextSplitter
]:
"""
Fixture to create an HTMLHeaderTextSplitter instance with given headers.
@@ -2232,7 +2232,7 @@ def html_header_splitter_splitter_factory() -> Callable[
"""
def _create_splitter(
headers_to_split_on: List[Tuple[str, str]],
headers_to_split_on: list[tuple[str, str]],
) -> HTMLHeaderTextSplitter:
return HTMLHeaderTextSplitter(headers_to_split_on=headers_to_split_on)
@@ -2426,9 +2426,9 @@ def html_header_splitter_splitter_factory() -> Callable[
@pytest.mark.requires("bs4")
def test_html_header_text_splitter(
html_header_splitter_splitter_factory: Any,
headers_to_split_on: List[Tuple[str, str]],
headers_to_split_on: list[tuple[str, str]],
html_input: str,
expected_documents: List[Document],
expected_documents: list[Document],
test_case: str,
) -> None:
"""
@@ -2582,9 +2582,9 @@ def test_html_header_text_splitter(
@pytest.mark.requires("bs4")
def test_additional_html_header_text_splitter(
html_header_splitter_splitter_factory: Any,
headers_to_split_on: List[Tuple[str, str]],
headers_to_split_on: list[tuple[str, str]],
html_content: str,
expected_output: List[Document],
expected_output: list[Document],
test_case: str,
) -> None:
"""
@@ -2653,9 +2653,9 @@ def test_additional_html_header_text_splitter(
@pytest.mark.requires("bs4")
def test_html_no_headers_with_multiple_splitters(
html_header_splitter_splitter_factory: Any,
headers_to_split_on: List[Tuple[str, str]],
headers_to_split_on: list[tuple[str, str]],
html_content: str,
expected_output: List[Document],
expected_output: list[Document],
test_case: str,
) -> None:
"""
@@ -3572,7 +3572,7 @@ def test_character_text_splitter_chunk_size_effect(
is_regex: bool,
text: str,
chunk_size: int,
expected: List[str],
expected: list[str],
) -> None:
splitter = CharacterTextSplitter(
separator=separator,