core: Add ruff rules TC (#29268)

See https://docs.astral.sh/ruff/rules/#flake8-type-checking-tc
Some fixes done for TC001,TC002 and TC003 but these rules are excluded
since they don't play well with Pydantic.

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
Christophe Bornet
2025-02-26 20:39:05 +01:00
committed by GitHub
parent 9cd20080fc
commit b3885c124f
48 changed files with 208 additions and 113 deletions

View File

@@ -60,7 +60,6 @@ from langchain_core.runnables.config import (
run_in_executor,
)
from langchain_core.runnables.graph import Graph
from langchain_core.runnables.schema import StreamEvent
from langchain_core.runnables.utils import (
AddableDict,
AnyConfigurableField,
@@ -94,6 +93,7 @@ if TYPE_CHECKING:
from langchain_core.runnables.fallbacks import (
RunnableWithFallbacks as RunnableWithFallbacksT,
)
from langchain_core.runnables.schema import StreamEvent
from langchain_core.tools import BaseTool
from langchain_core.tracers.log_stream import (
RunLog,

View File

@@ -7,6 +7,7 @@ from collections.abc import AsyncIterator, Iterator, Sequence
from collections.abc import Mapping as Mapping
from functools import wraps
from typing import (
TYPE_CHECKING,
Any,
Callable,
Optional,
@@ -26,7 +27,6 @@ from langchain_core.runnables.config import (
get_executor_for_config,
merge_configs,
)
from langchain_core.runnables.graph import Graph
from langchain_core.runnables.utils import (
AnyConfigurableField,
ConfigurableField,
@@ -39,6 +39,9 @@ from langchain_core.runnables.utils import (
get_unique_config_specs,
)
if TYPE_CHECKING:
from langchain_core.runnables.graph import Graph
class DynamicRunnable(RunnableSerializable[Input, Output]):
"""Serializable Runnable that can be dynamically configured.

View File

@@ -2,7 +2,6 @@ from __future__ import annotations
import inspect
from collections import defaultdict
from collections.abc import Sequence
from dataclasses import dataclass, field
from enum import Enum
from typing import (
@@ -18,11 +17,13 @@ from typing import (
)
from uuid import UUID, uuid4
from pydantic import BaseModel
from langchain_core.utils.pydantic import _IgnoreUnserializable, is_basemodel_subclass
if TYPE_CHECKING:
from collections.abc import Sequence
from pydantic import BaseModel
from langchain_core.runnables.base import Runnable as RunnableType

View File

@@ -5,7 +5,7 @@ from __future__ import annotations
import asyncio
import inspect
import threading
from collections.abc import AsyncIterator, Awaitable, Iterator, Mapping
from collections.abc import Awaitable
from typing import (
TYPE_CHECKING,
Any,
@@ -32,7 +32,6 @@ from langchain_core.runnables.config import (
get_executor_for_config,
patch_config,
)
from langchain_core.runnables.graph import Graph
from langchain_core.runnables.utils import (
AddableDict,
ConfigurableFieldSpec,
@@ -42,10 +41,13 @@ from langchain_core.utils.iter import safetee
from langchain_core.utils.pydantic import create_model_v2
if TYPE_CHECKING:
from collections.abc import AsyncIterator, Iterator, Mapping
from langchain_core.callbacks.manager import (
AsyncCallbackManagerForChainRun,
CallbackManagerForChainRun,
)
from langchain_core.runnables.graph import Graph
def identity(x: Other) -> Other:

View File

@@ -1,8 +1,9 @@
from __future__ import annotations
from collections.abc import AsyncIterator, Iterator, Mapping
from collections.abc import Mapping
from itertools import starmap
from typing import (
TYPE_CHECKING,
Any,
Callable,
Optional,
@@ -31,6 +32,9 @@ from langchain_core.runnables.utils import (
get_unique_config_specs,
)
if TYPE_CHECKING:
from collections.abc import AsyncIterator, Iterator
class RouterInput(TypedDict):
"""Router input.

View File

@@ -2,11 +2,13 @@
from __future__ import annotations
from collections.abc import Sequence
from typing import Any, Literal, Union
from typing import TYPE_CHECKING, Any, Literal, Union
from typing_extensions import NotRequired, TypedDict
if TYPE_CHECKING:
from collections.abc import Sequence
class EventData(TypedDict, total=False):
"""Data associated with a streaming event."""

View File

@@ -6,19 +6,11 @@ import ast
import asyncio
import inspect
import textwrap
from collections.abc import (
AsyncIterable,
AsyncIterator,
Awaitable,
Coroutine,
Iterable,
Mapping,
Sequence,
)
from functools import lru_cache
from inspect import signature
from itertools import groupby
from typing import (
TYPE_CHECKING,
Any,
Callable,
NamedTuple,
@@ -30,11 +22,22 @@ from typing import (
from typing_extensions import TypeGuard, override
from langchain_core.runnables.schema import StreamEvent
# Re-export create-model for backwards compatibility
from langchain_core.utils.pydantic import create_model as create_model
if TYPE_CHECKING:
from collections.abc import (
AsyncIterable,
AsyncIterator,
Awaitable,
Coroutine,
Iterable,
Mapping,
Sequence,
)
from langchain_core.runnables.schema import StreamEvent
Input = TypeVar("Input", contravariant=True)
# Output type should implement __concat__, as eg str, list, dict do
Output = TypeVar("Output", covariant=True)