chore(core): add mypy pydantic plugin (#32604)

This helps to remove a bunch of mypy false positives.
This commit is contained in:
Christophe Bornet
2025-08-19 15:39:53 +02:00
committed by GitHub
parent b470c79f1d
commit 02d6b9106b
18 changed files with 45 additions and 48 deletions

View File

@@ -810,7 +810,7 @@ def test_parse_with_different_pydantic_2_v1() -> None:
# Can't get pydantic to work here due to the odd typing of tryig to support
# both v1 and v2 in the same codebase.
parser = PydanticToolsParser(tools=[Forecast]) # type: ignore[list-item]
parser = PydanticToolsParser(tools=[Forecast])
message = AIMessage(
content="",
tool_calls=[

View File

@@ -13,7 +13,7 @@ from langchain_core.language_models import ParrotFakeChatModel
from langchain_core.output_parsers import PydanticOutputParser
from langchain_core.output_parsers.json import JsonOutputParser
from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.utils.pydantic import TBaseModel
from langchain_core.utils.pydantic import PydanticBaseModel, TBaseModel
class ForecastV2(pydantic.BaseModel):
@@ -43,7 +43,7 @@ def test_pydantic_parser_chaining(
model = ParrotFakeChatModel()
parser = PydanticOutputParser(pydantic_object=pydantic_object) # type: ignore[type-var]
parser = PydanticOutputParser[PydanticBaseModel](pydantic_object=pydantic_object)
chain = prompt | model | parser
res = chain.invoke({})
@@ -66,7 +66,9 @@ def test_pydantic_parser_validation(pydantic_object: TBaseModel) -> None:
model = ParrotFakeChatModel()
parser = PydanticOutputParser(pydantic_object=pydantic_object) # type: ignore[arg-type,var-annotated]
parser: PydanticOutputParser[PydanticBaseModel] = PydanticOutputParser(
pydantic_object=pydantic_object
)
chain = bad_prompt | model | parser
with pytest.raises(OutputParserException):
chain.invoke({})
@@ -88,7 +90,7 @@ def test_json_parser_chaining(
model = ParrotFakeChatModel()
parser = JsonOutputParser(pydantic_object=pydantic_object) # type: ignore[arg-type]
parser = JsonOutputParser(pydantic_object=pydantic_object)
chain = prompt | model | parser
res = chain.invoke({})
@@ -171,7 +173,7 @@ def test_pydantic_output_parser_type_inference() -> None:
# Ignoring mypy error that appears in python 3.8, but not 3.11.
# This seems to be functionally correct, so we'll ignore the error.
pydantic_parser = PydanticOutputParser(pydantic_object=SampleModel)
pydantic_parser = PydanticOutputParser[SampleModel](pydantic_object=SampleModel)
schema = pydantic_parser.get_output_schema().model_json_schema()
assert schema == {
@@ -202,5 +204,5 @@ def test_format_instructions_preserves_language() -> None:
)
)
parser = PydanticOutputParser(pydantic_object=Foo)
parser = PydanticOutputParser[Foo](pydantic_object=Foo)
assert description in parser.get_format_instructions()

View File

@@ -348,7 +348,7 @@ def test_prompt_invalid_template_format() -> None:
PromptTemplate(
input_variables=input_variables,
template=template,
template_format="bar", # type: ignore[arg-type]
template_format="bar",
)

View File

@@ -73,7 +73,7 @@ class MyOtherRunnable(RunnableSerializable[str, str]):
def test_doubly_set_configurable() -> None:
"""Test that setting a configurable field with a default value works."""
runnable = MyRunnable(my_property="a") # type: ignore[call-arg]
runnable = MyRunnable(my_property="a")
configurable_runnable = runnable.configurable_fields(
my_property=ConfigurableField(
id="my_property",
@@ -86,7 +86,7 @@ def test_doubly_set_configurable() -> None:
def test_alias_set_configurable() -> None:
runnable = MyRunnable(my_property="a") # type: ignore[call-arg]
runnable = MyRunnable(my_property="a")
configurable_runnable = runnable.configurable_fields(
my_property=ConfigurableField(
id="my_property_alias",
@@ -104,7 +104,7 @@ def test_alias_set_configurable() -> None:
def test_field_alias_set_configurable() -> None:
runnable = MyRunnable(my_property_alias="a")
runnable = MyRunnable(my_property_alias="a") # type: ignore[call-arg]
configurable_runnable = runnable.configurable_fields(
my_property=ConfigurableField(
id="my_property",
@@ -122,7 +122,7 @@ def test_field_alias_set_configurable() -> None:
def test_config_passthrough() -> None:
runnable = MyRunnable(my_property="a") # type: ignore[call-arg]
runnable = MyRunnable(my_property="a")
configurable_runnable = runnable.configurable_fields(
my_property=ConfigurableField(
id="my_property",
@@ -158,7 +158,7 @@ def test_config_passthrough() -> None:
def test_config_passthrough_nested() -> None:
runnable = MyRunnable(my_property="a") # type: ignore[call-arg]
runnable = MyRunnable(my_property="a")
configurable_runnable = runnable.configurable_fields(
my_property=ConfigurableField(
id="my_property",

View File

@@ -347,7 +347,7 @@ def test_using_secret_from_env_as_default_factory(
secret: SecretStr = Field(default_factory=secret_from_env("TEST_KEY"))
# Pass the secret as a parameter
foo = Foo(secret="super_secret") # type: ignore[arg-type]
foo = Foo(secret="super_secret")
assert foo.secret.get_secret_value() == "super_secret"
# Set the environment variable