Skip to content

Commit

Permalink
Import TypedDict from typing_extensions, add ruff rule (#2910)
Browse files Browse the repository at this point in the history
Within `libs/langgraph`, change all `TypedDict` imports to come from
`typing_extensions` rather than `typing`, as `pydantic` doesn't like the
latter.

Additionally, add a ruff rule to ban these imports too (so this doesn't
regress).

Solves  #2909.
  • Loading branch information
hinthornw authored Jan 4, 2025
2 parents ce90086 + c865e8c commit 4798443
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 19 deletions.
4 changes: 3 additions & 1 deletion libs/langgraph/bench/fanout_to_subgraph.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion libs/langgraph/langgraph/graph/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
Literal,
Optional,
Sequence,
TypedDict,
Union,
cast,
)
Expand All @@ -22,6 +21,7 @@
convert_to_messages,
message_chunk_to_message,
)
from typing_extensions import TypedDict

from langgraph.graph.state import StateGraph

Expand Down
2 changes: 1 addition & 1 deletion libs/langgraph/langgraph/prebuilt/chat_agent_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class Agent,Tools otherClass
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(
Expand Down
3 changes: 2 additions & 1 deletion libs/langgraph/langgraph/prebuilt/tool_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion libs/langgraph/langgraph/prebuilt/tool_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libs/langgraph/langgraph/pregel/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
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
Expand Down
6 changes: 3 additions & 3 deletions libs/langgraph/langgraph/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
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,
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion libs/langgraph/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions libs/langgraph/tests/test_interruption.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
3 changes: 2 additions & 1 deletion libs/langgraph/tests/test_large_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
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
Expand Down
2 changes: 1 addition & 1 deletion libs/langgraph/tests/test_large_cases_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
AsyncIterator,
Literal,
Optional,
TypedDict,
Union,
cast,
)
Expand All @@ -21,6 +20,7 @@
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
Expand Down
2 changes: 1 addition & 1 deletion libs/langgraph/tests/test_pregel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
Optional,
Sequence,
Tuple,
TypedDict,
Union,
get_type_hints,
)
Expand All @@ -36,6 +35,7 @@
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
Expand Down
2 changes: 1 addition & 1 deletion libs/langgraph/tests/test_pregel_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
Literal,
Optional,
Tuple,
TypedDict,
Union,
)
from uuid import UUID
Expand All @@ -34,6 +33,7 @@
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
Expand Down
3 changes: 2 additions & 1 deletion libs/langgraph/tests/test_tracing_interops.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 1 addition & 2 deletions libs/langgraph/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
List,
Literal,
Optional,
TypedDict,
TypeVar,
Union,
)
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
Expand Down

0 comments on commit 4798443

Please sign in to comment.