From 7256752f482cb4e5ac652b41bc4d6ae793585e8e Mon Sep 17 00:00:00 2001 From: Johannes Mario Meissner Date: Tue, 31 Dec 2024 22:14:16 +0900 Subject: [PATCH 1/2] Import TypedDict from typing_extensions, add ruff rule --- libs/langgraph/README.md | 3 ++- libs/langgraph/bench/fanout_to_subgraph.py | 4 +++- libs/langgraph/langgraph/graph/message.py | 2 +- libs/langgraph/langgraph/prebuilt/chat_agent_executor.py | 2 +- libs/langgraph/langgraph/prebuilt/tool_node.py | 3 ++- libs/langgraph/langgraph/prebuilt/tool_validator.py | 3 ++- libs/langgraph/langgraph/pregel/debug.py | 2 +- libs/langgraph/langgraph/types.py | 6 +++--- libs/langgraph/pyproject.toml | 5 ++++- libs/langgraph/tests/test_interruption.py | 3 +-- libs/langgraph/tests/test_large_cases.py | 3 ++- libs/langgraph/tests/test_large_cases_async.py | 2 +- libs/langgraph/tests/test_pregel.py | 2 +- libs/langgraph/tests/test_pregel_async.py | 2 +- libs/langgraph/tests/test_tracing_interops.py | 3 ++- libs/langgraph/tests/test_utils.py | 3 +-- 16 files changed, 28 insertions(+), 20 deletions(-) diff --git a/libs/langgraph/README.md b/libs/langgraph/README.md index a05b4ce97..113dabe33 100644 --- a/libs/langgraph/README.md +++ b/libs/langgraph/README.md @@ -67,7 +67,8 @@ export LANGSMITH_API_KEY=lsv2_sk_... ``` ```python -from typing import Annotated, Literal, TypedDict +from typing import Annotated, Literal +from typing_extensions import TypedDict from langchain_core.messages import HumanMessage from langchain_anthropic import ChatAnthropic diff --git a/libs/langgraph/bench/fanout_to_subgraph.py b/libs/langgraph/bench/fanout_to_subgraph.py index 6b0f52379..612d0fbf2 100644 --- a/libs/langgraph/bench/fanout_to_subgraph.py +++ b/libs/langgraph/bench/fanout_to_subgraph.py @@ -1,5 +1,7 @@ import operator -from typing import Annotated, TypedDict +from typing import Annotated + +from typing_extensions import TypedDict from langgraph.constants import END, START, Send from langgraph.graph.state import StateGraph diff --git a/libs/langgraph/langgraph/graph/message.py b/libs/langgraph/langgraph/graph/message.py index e63ebee83..ed6322b5b 100644 --- a/libs/langgraph/langgraph/graph/message.py +++ b/libs/langgraph/langgraph/graph/message.py @@ -8,7 +8,6 @@ from typing import ( Literal, Optional, Sequence, - TypedDict, Union, cast, ) @@ -22,6 +21,7 @@ from langchain_core.messages import ( convert_to_messages, message_chunk_to_message, ) +from typing_extensions import TypedDict from langgraph.graph.state import StateGraph diff --git a/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py b/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py index b5207d167..da70f9e87 100644 --- a/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py +++ b/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py @@ -381,7 +381,7 @@ def create_react_agent( Add complex prompt with custom graph state: ```pycon - >>> from typing import TypedDict + >>> from typing_extensions import TypedDict >>> >>> from langgraph.managed import IsLastStep >>> prompt = ChatPromptTemplate.from_messages( diff --git a/libs/langgraph/langgraph/prebuilt/tool_node.py b/libs/langgraph/langgraph/prebuilt/tool_node.py index 5692c750d..30a4a54e3 100644 --- a/libs/langgraph/langgraph/prebuilt/tool_node.py +++ b/libs/langgraph/langgraph/prebuilt/tool_node.py @@ -601,7 +601,8 @@ def tools_condition( >>> from langgraph.prebuilt import ToolNode, tools_condition >>> from langgraph.graph.message import add_messages ... - >>> from typing import TypedDict, Annotated + >>> from typing import Annotated + >>> from typing_extensions import TypedDict ... >>> @tool >>> def divide(a: float, b: float) -> int: diff --git a/libs/langgraph/langgraph/prebuilt/tool_validator.py b/libs/langgraph/langgraph/prebuilt/tool_validator.py index 401a35db7..3239905b6 100644 --- a/libs/langgraph/langgraph/prebuilt/tool_validator.py +++ b/libs/langgraph/langgraph/prebuilt/tool_validator.py @@ -74,7 +74,8 @@ class ValidationNode(RunnableCallable): Examples: Example usage for re-prompting the model to generate a valid response: - >>> from typing import Literal, Annotated, TypedDict + >>> from typing import Literal, Annotated + >>> from typing_extensions import TypedDict ... >>> from langchain_anthropic import ChatAnthropic >>> from pydantic import BaseModel, validator diff --git a/libs/langgraph/langgraph/pregel/debug.py b/libs/langgraph/langgraph/pregel/debug.py index 95b30f118..cc398a599 100644 --- a/libs/langgraph/langgraph/pregel/debug.py +++ b/libs/langgraph/langgraph/pregel/debug.py @@ -10,13 +10,13 @@ from typing import ( Mapping, Optional, Sequence, - TypedDict, Union, ) from uuid import UUID from langchain_core.runnables.config import RunnableConfig from langchain_core.utils.input import get_bolded_text, get_colored_text +from typing_extensions import TypedDict from langgraph.channels.base import BaseChannel from langgraph.checkpoint.base import Checkpoint, CheckpointMetadata, PendingWrite diff --git a/libs/langgraph/langgraph/types.py b/libs/langgraph/langgraph/types.py index 53dc6bd57..fa1adafe6 100644 --- a/libs/langgraph/langgraph/types.py +++ b/libs/langgraph/langgraph/types.py @@ -13,14 +13,13 @@ from typing import ( Optional, Sequence, Type, - TypedDict, TypeVar, Union, cast, ) from langchain_core.runnables import Runnable, RunnableConfig -from typing_extensions import Self +from typing_extensions import Self, TypedDict from langgraph.checkpoint.base import ( BaseCheckpointSaver, @@ -373,7 +372,8 @@ def interrupt(value: Any) -> Any: Example: ```python import uuid - from typing import TypedDict, Optional + from typing import Optional + from typing_extensions import TypedDict from langgraph.checkpoint.memory import MemorySaver from langgraph.constants import START diff --git a/libs/langgraph/pyproject.toml b/libs/langgraph/pyproject.toml index 4197552bd..f720cbb9f 100644 --- a/libs/langgraph/pyproject.toml +++ b/libs/langgraph/pyproject.toml @@ -38,7 +38,7 @@ py-spy = "^0.3.14" types-requests = "^2.32.0.20240914" [tool.ruff] -lint.select = [ "E", "F", "I" ] +lint.select = [ "E", "F", "I", "TID251" ] lint.ignore = [ "E501" ] line-length = 88 indent-width = 4 @@ -52,6 +52,9 @@ line-ending = "auto" docstring-code-format = false docstring-code-line-length = "dynamic" +[tool.ruff.lint.flake8-tidy-imports.banned-api] +"typing.TypedDict".msg = "Use typing_extensions.TypedDict instead." + [tool.mypy] # https://mypy.readthedocs.io/en/stable/config_file.html disallow_untyped_defs = "True" diff --git a/libs/langgraph/tests/test_interruption.py b/libs/langgraph/tests/test_interruption.py index d4e618ddf..aa543cf00 100644 --- a/libs/langgraph/tests/test_interruption.py +++ b/libs/langgraph/tests/test_interruption.py @@ -1,7 +1,6 @@ -from typing import TypedDict - import pytest from pytest_mock import MockerFixture +from typing_extensions import TypedDict from langgraph.graph import END, START, StateGraph from tests.conftest import ( diff --git a/libs/langgraph/tests/test_large_cases.py b/libs/langgraph/tests/test_large_cases.py index f3f399f38..fd14ab8f9 100644 --- a/libs/langgraph/tests/test_large_cases.py +++ b/libs/langgraph/tests/test_large_cases.py @@ -4,13 +4,14 @@ import re import time from contextlib import contextmanager from dataclasses import replace -from typing import Annotated, Any, Iterator, Literal, Optional, TypedDict, Union, cast +from typing import Annotated, Any, Iterator, Literal, Optional, Union, cast import httpx import pytest from langchain_core.runnables import RunnableConfig, RunnableMap, RunnablePick from pytest_mock import MockerFixture from syrupy import SnapshotAssertion +from typing_extensions import TypedDict from langgraph.channels.context import Context from langgraph.channels.last_value import LastValue diff --git a/libs/langgraph/tests/test_large_cases_async.py b/libs/langgraph/tests/test_large_cases_async.py index f6e807fca..04ea431e4 100644 --- a/libs/langgraph/tests/test_large_cases_async.py +++ b/libs/langgraph/tests/test_large_cases_async.py @@ -9,7 +9,6 @@ from typing import ( AsyncIterator, Literal, Optional, - TypedDict, Union, cast, ) @@ -21,6 +20,7 @@ from langchain_core.runnables import RunnableConfig, RunnablePick from pydantic import BaseModel from pytest_mock import MockerFixture from syrupy import SnapshotAssertion +from typing_extensions import TypedDict from langgraph.channels.context import Context from langgraph.channels.last_value import LastValue diff --git a/libs/langgraph/tests/test_pregel.py b/libs/langgraph/tests/test_pregel.py index b08cc87a7..1d55cf316 100644 --- a/libs/langgraph/tests/test_pregel.py +++ b/libs/langgraph/tests/test_pregel.py @@ -21,7 +21,6 @@ from typing import ( Optional, Sequence, Tuple, - TypedDict, Union, get_type_hints, ) @@ -36,6 +35,7 @@ from langchain_core.runnables import ( from langsmith import traceable from pytest_mock import MockerFixture from syrupy import SnapshotAssertion +from typing_extensions import TypedDict from langgraph.channels.base import BaseChannel from langgraph.channels.binop import BinaryOperatorAggregate diff --git a/libs/langgraph/tests/test_pregel_async.py b/libs/langgraph/tests/test_pregel_async.py index bb3e9ba08..e47313dae 100644 --- a/libs/langgraph/tests/test_pregel_async.py +++ b/libs/langgraph/tests/test_pregel_async.py @@ -19,7 +19,6 @@ from typing import ( Literal, Optional, Tuple, - TypedDict, Union, ) from uuid import UUID @@ -34,6 +33,7 @@ from langchain_core.runnables import ( from langchain_core.utils.aiter import aclosing from pytest_mock import MockerFixture from syrupy import SnapshotAssertion +from typing_extensions import TypedDict from langgraph.channels.base import BaseChannel from langgraph.channels.binop import BinaryOperatorAggregate diff --git a/libs/langgraph/tests/test_tracing_interops.py b/libs/langgraph/tests/test_tracing_interops.py index 5b458394b..d06896bd5 100644 --- a/libs/langgraph/tests/test_tracing_interops.py +++ b/libs/langgraph/tests/test_tracing_interops.py @@ -1,13 +1,14 @@ import json import sys import time -from typing import Any, Callable, Tuple, TypedDict, TypeVar +from typing import Any, Callable, Tuple, TypeVar from unittest.mock import MagicMock import langsmith as ls import pytest from langchain_core.runnables import RunnableConfig from langchain_core.tracers import LangChainTracer +from typing_extensions import TypedDict from langgraph.graph import StateGraph diff --git a/libs/langgraph/tests/test_utils.py b/libs/langgraph/tests/test_utils.py index 616f1a78f..e77cbd3a2 100644 --- a/libs/langgraph/tests/test_utils.py +++ b/libs/langgraph/tests/test_utils.py @@ -9,7 +9,6 @@ from typing import ( List, Literal, Optional, - TypedDict, TypeVar, Union, ) @@ -17,7 +16,7 @@ from unittest.mock import patch import langsmith import pytest -from typing_extensions import Annotated, NotRequired, Required +from typing_extensions import Annotated, NotRequired, Required, TypedDict from langgraph.graph import END, StateGraph from langgraph.graph.graph import CompiledGraph From c865e8c070f3a6097b6cafb381898ee0fe391c10 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Thu, 2 Jan 2025 10:01:35 +0000 Subject: [PATCH 2/2] Update README.md --- libs/langgraph/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libs/langgraph/README.md b/libs/langgraph/README.md index 113dabe33..a05b4ce97 100644 --- a/libs/langgraph/README.md +++ b/libs/langgraph/README.md @@ -67,8 +67,7 @@ export LANGSMITH_API_KEY=lsv2_sk_... ``` ```python -from typing import Annotated, Literal -from typing_extensions import TypedDict +from typing import Annotated, Literal, TypedDict from langchain_core.messages import HumanMessage from langchain_anthropic import ChatAnthropic