-
Notifications
You must be signed in to change notification settings - Fork 22k
style(core): fix mypy no-any-return violations #34204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Mason Daugherty (mdrxy)
merged 8 commits into
langchain-ai:master
from
cbornet:core-no-return-any
Dec 27, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6f65bfd
chore(core): fix RunnablePick method return types
cbornet 58ae69a
cast remaining return types
cbornet ae22c7f
fix
cbornet 3c9256b
cr
mdrxy 3b5be0f
cr
mdrxy 8826c21
Changes following review
cbornet 5d5e90f
Changes following review
cbornet 370ed3f
Merge branch 'master' into core-no-return-any
mdrxy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -302,7 +302,7 @@ def __add__(self, other: Any) -> ChatPromptTemplate: | |
| from langchain_core.prompts.chat import ChatPromptTemplate # noqa: PLC0415 | ||
|
|
||
| prompt = ChatPromptTemplate(messages=[self]) | ||
| return prompt + other | ||
| return prompt.__add__(other) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mypy doesn't figure the |
||
|
|
||
| def pretty_repr( | ||
| self, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,14 +65,19 @@ | |
| def _get_type(v: Any) -> str: | ||
| """Get the type associated with the object for serialization purposes.""" | ||
| if isinstance(v, dict) and "type" in v: | ||
| return v["type"] | ||
| if hasattr(v, "type"): | ||
| return v.type | ||
| msg = ( | ||
| f"Expected either a dictionary with a 'type' key or an object " | ||
| f"with a 'type' attribute. Instead got type {type(v)}." | ||
| ) | ||
| raise TypeError(msg) | ||
| result = v["type"] | ||
| elif hasattr(v, "type"): | ||
| result = v.type | ||
| else: | ||
| msg = ( | ||
| f"Expected either a dictionary with a 'type' key or an object " | ||
| f"with a 'type' attribute. Instead got type {type(v)}." | ||
| ) | ||
| raise TypeError(msg) | ||
| if not isinstance(result, str): | ||
| msg = f"Expected 'type' to be a str, got {type(result).__name__}" | ||
| raise TypeError(msg) | ||
| return result | ||
|
|
||
|
|
||
| AnyMessage = Annotated[ | ||
|
|
@@ -215,8 +220,11 @@ def message_chunk_to_message(chunk: BaseMessage) -> BaseMessage: | |
| ignore_keys = ["type"] | ||
| if isinstance(chunk, AIMessageChunk): | ||
| ignore_keys.extend(["tool_call_chunks", "chunk_position"]) | ||
| return chunk.__class__.__mro__[1]( | ||
| **{k: v for k, v in chunk.__dict__.items() if k not in ignore_keys} | ||
| return cast( | ||
| "BaseMessage", | ||
| chunk.__class__.__mro__[1]( | ||
| **{k: v for k, v in chunk.__dict__.items() if k not in ignore_keys} | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -1112,6 +1120,32 @@ def list_token_counter(messages: Sequence[BaseMessage]) -> int: | |
| raise ValueError(msg) | ||
|
|
||
|
|
||
| _SingleMessage = BaseMessage | str | dict[str, Any] | ||
| _T = TypeVar("_T", bound=_SingleMessage) | ||
| # A sequence of _SingleMessage that is NOT a bare str | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is needed because otherwise |
||
| _MultipleMessages = Sequence[_T] | ||
|
|
||
|
|
||
| @overload | ||
| def convert_to_openai_messages( | ||
| messages: _SingleMessage, | ||
| *, | ||
| text_format: Literal["string", "block"] = "string", | ||
| include_id: bool = False, | ||
| pass_through_unknown_blocks: bool = True, | ||
| ) -> dict: ... | ||
|
|
||
|
|
||
| @overload | ||
| def convert_to_openai_messages( | ||
| messages: _MultipleMessages, | ||
| *, | ||
| text_format: Literal["string", "block"] = "string", | ||
| include_id: bool = False, | ||
| pass_through_unknown_blocks: bool = True, | ||
| ) -> list[dict]: ... | ||
|
|
||
|
|
||
| def convert_to_openai_messages( | ||
| messages: MessageLikeRepresentation | Sequence[MessageLikeRepresentation], | ||
| *, | ||
|
|
@@ -1207,7 +1241,7 @@ def convert_to_openai_messages( | |
| err = f"Unrecognized {text_format=}, expected one of 'string' or 'block'." | ||
| raise ValueError(err) | ||
|
|
||
| oai_messages: list = [] | ||
| oai_messages: list[dict] = [] | ||
|
|
||
| if is_single := isinstance(messages, (BaseMessage, dict, str)): | ||
| messages = [messages] | ||
|
|
@@ -1774,7 +1808,11 @@ def _get_message_openai_role(message: BaseMessage) -> str: | |
| if isinstance(message, ToolMessage): | ||
| return "tool" | ||
| if isinstance(message, SystemMessage): | ||
| return message.additional_kwargs.get("__openai_role__", "system") | ||
| role = message.additional_kwargs.get("__openai_role__", "system") | ||
| if not isinstance(role, str): | ||
| msg = f"Expected '__openai_role__' to be a str, got {type(role).__name__}" | ||
| raise TypeError(msg) | ||
| return role | ||
| if isinstance(message, FunctionMessage): | ||
| return "function" | ||
| if isinstance(message, ChatMessage): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
calling bool() here doesn't change the behavior as
_should_streamis always called in aifcondition.