core: Improve mypy config (#30737)

* Cleanup mypy config
* Add mypy `strict` rules except `disallow_any_generics`,
`warn_return_any` and `strict_equality` (TODO)
* Add mypy `strict_byte` rule
* Add mypy support for PEP702 `@deprecated` decorator
* Bump mypy version to 1.15

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
This commit is contained in:
Christophe Bornet
2025-04-11 22:35:13 +02:00
committed by GitHub
parent bb2c2fd885
commit 42944f3499
60 changed files with 222 additions and 237 deletions

View File

@@ -8,7 +8,7 @@ from typing import (
import pytest
from pydantic import BaseModel
from syrupy import SnapshotAssertion
from syrupy.assertion import SnapshotAssertion
from typing_extensions import override
from langchain_core.callbacks import CallbackManagerForLLMRun

View File

@@ -2,7 +2,7 @@ from typing import Any, Optional
from packaging import version
from pydantic import BaseModel
from syrupy import SnapshotAssertion
from syrupy.assertion import SnapshotAssertion
from typing_extensions import override
from langchain_core.language_models import FakeListLLM
@@ -10,7 +10,8 @@ from langchain_core.output_parsers.list import CommaSeparatedListOutputParser
from langchain_core.output_parsers.string import StrOutputParser
from langchain_core.output_parsers.xml import XMLOutputParser
from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.runnables.base import Runnable, RunnableConfig
from langchain_core.runnables import RunnableConfig
from langchain_core.runnables.base import Runnable
from langchain_core.runnables.graph import Edge, Graph, Node
from langchain_core.runnables.graph_mermaid import _escape_node_label
from langchain_core.utils.pydantic import (

View File

@@ -40,4 +40,6 @@ def test_all_imports() -> None:
def test_imports_for_specific_funcs() -> None:
"""Test that a few specific imports in more internal namespaces."""
# create_model implementation has been moved to langchain_core.utils.pydantic
from langchain_core.runnables.utils import create_model # noqa: F401
from langchain_core.runnables.utils import ( # type: ignore[attr-defined] # noqa: F401
create_model,
)

View File

@@ -20,7 +20,7 @@ from freezegun import freeze_time
from packaging import version
from pydantic import BaseModel, Field
from pytest_mock import MockerFixture
from syrupy import SnapshotAssertion
from syrupy.assertion import SnapshotAssertion
from typing_extensions import TypedDict, override
from langchain_core.callbacks.manager import (
@@ -557,7 +557,7 @@ def test_lambda_schemas(snapshot: SnapshotAssertion) -> None:
}
second_lambda = lambda x, y: (x["hello"], x["bye"], y["bah"]) # noqa: E731
assert RunnableLambda(second_lambda).get_input_jsonschema() == { # type: ignore[arg-type]
assert RunnableLambda(second_lambda).get_input_jsonschema() == {
"title": "RunnableLambdaInput",
"type": "object",
"properties": {"hello": {"title": "Hello"}, "bye": {"title": "Bye"}},
@@ -1012,7 +1012,7 @@ def test_passthrough_tap(mocker: MockerFixture) -> None:
seq: Runnable = RunnablePassthrough(mock) | fake | RunnablePassthrough(mock)
assert seq.invoke("hello", my_kwarg="value") == 5 # type: ignore[call-arg]
assert seq.invoke("hello", my_kwarg="value") == 5
assert mock.call_args_list == [
mocker.call("hello", my_kwarg="value"),
mocker.call(5),
@@ -4488,7 +4488,7 @@ def test_runnable_branch_invoke() -> None:
branch = RunnableBranch[int, int](
(lambda x: x > 100, raise_value_error),
# mypy cannot infer types from the lambda
(lambda x: x > 0 and x < 5, lambda x: x + 1), # type: ignore[misc]
(lambda x: x > 0 and x < 5, lambda x: x + 1),
(lambda x: x > 5, lambda x: x * 10),
lambda x: x - 1,
)

View File

@@ -2759,9 +2759,7 @@ async def test_custom_event_root_dispatch_with_in_tool() -> None:
return x + 1
# Ignoring type due to @tool not returning correct type annotations
events = await _collect_events(
foo.astream_events({"x": 2}, version="v2") # type: ignore[attr-defined]
)
events = await _collect_events(foo.astream_events({"x": 2}, version="v2"))
_assert_events_equal_allow_superset_metadata(
events,
[

View File

@@ -163,13 +163,13 @@ async def test_config_traceable_async_handoff() -> None:
def my_great_grandchild_function(a: int) -> int:
return my_great_great_grandchild_function(a)
@RunnableLambda # type: ignore[arg-type,attr-defined]
@RunnableLambda # type: ignore[arg-type]
async def my_grandchild_function(a: int) -> int:
return my_great_grandchild_function.invoke(a)
@traceable
async def my_child_function(a: int) -> int:
return await my_grandchild_function.ainvoke(a) * 3 # type: ignore[arg-type,attr-defined]
return await my_grandchild_function.ainvoke(a) * 3 # type: ignore[arg-type]
@traceable()
async def my_function(a: int) -> int:
@@ -337,7 +337,7 @@ class TestRunnableSequenceParallelTraceNesting:
assert posts[i]["name"] == name
dotted_order = posts[i]["dotted_order"]
if prev_dotted_order is not None and not str(
expected_parents[name]
expected_parents[name] # type: ignore[index]
).startswith("RunnableParallel"):
assert dotted_order > prev_dotted_order, (
f"{name} not after {name_order[i - 1]}"