Skip to content

feat: adding typing to melleatools#959

Open
akihikokuroda wants to merge 3 commits intogenerative-computing:mainfrom
akihikokuroda:typingmelleatools
Open

feat: adding typing to melleatools#959
akihikokuroda wants to merge 3 commits intogenerative-computing:mainfrom
akihikokuroda:typingmelleatools

Conversation

@akihikokuroda
Copy link
Copy Markdown
Member

@akihikokuroda akihikokuroda commented Apr 29, 2026

Misc PR

Type of PR

  • Bug Fix
  • New Feature
  • Documentation
  • Other

Description

mellea/backends/tools.py

  • Generic type parameters: Introduced ParamSpec (P) and TypeVar (R) to represent tool parameter specifications and return types
  • MelleaTool class: Made generic as MelleaTool[P, R] to track callable signatures end-to-end
  • Type-safe run() method: Updated signature from run(self, *args: Any, **kwargs: Any) -> Any to run(self, *args: P.args, **kwargs: P.kwargs) -> R,
    preserving caller type information
  • Factory methods: Updated from_callable(), from_langchain(), and from_smolagents() return types to MelleaTool[P, R] (or MelleaTool[P, Any] where
    type info is unavailable)
  • Decorator overloads: Enhanced @tool decorator overloads to preserve parameter and return types through the entire decoration chain
  • Docstrings: Added detailed type parameter documentation and improved examples showing IDE type hints in action

mellea/core/base.py

  • AbstractMelleaTool base class: Made generic as AbstractMelleaTool[P, R] with Generic[P, R]
  • Abstract run() method: Updated signature to use P.args, P.kwargs, and return type R
  • Type parameter documentation: Added Type parameters: section to class docstring explaining P and R
  • Import additions: Added ParamSpec to imports for type specification

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code as added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used: claude

@akihikokuroda akihikokuroda requested review from a team, jakelorocco and nrfulton as code owners April 29, 2026 14:37
@github-actions
Copy link
Copy Markdown
Contributor

The PR description has been updated. Please fill out the template for your PR to be reviewed.

@akihikokuroda akihikokuroda changed the title adding typing to melleatools feat: adding typing to melleatools Apr 29, 2026
@github-actions github-actions Bot added the enhancement New feature or request label Apr 29, 2026
@akihikokuroda akihikokuroda marked this pull request as draft April 29, 2026 14:52
@akihikokuroda akihikokuroda marked this pull request as ready for review April 29, 2026 19:17
Comment thread mellea/backends/tools.py
from collections.abc import Callable, Generator, Iterable, Mapping, Sequence
from typing import Any, Literal, overload
from typing import Any, Generic, Literal, ParamSpec, TypeVar, overload

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Generic is imported but not used — MelleaTool picks up genericity via AbstractMelleaTool[P, R]. Project ignores F401 so CI won't complain, but it's dead code.

Suggested change
from typing import Any, Literal, ParamSpec, TypeVar, overload

Comment thread mellea/backends/tools.py
@classmethod
def from_langchain(cls, tool: Any) -> "MelleaTool":
def from_langchain(cls, tool: Any) -> "MelleaTool[P, Any]":
"""Create a MelleaTool from a LangChain tool object.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: MelleaTool[P, Any] with a module-level P collapses to MelleaTool[..., Any] under pyright and MelleaTool[Any, Any] under mypy — both type-check fine, but spelling it as MelleaTool[..., Any] is the explicit form for "unknown parameter spec" and matches what pyright shows. Worth updating the docstring Returns: line too.

Suggested change
"""Create a MelleaTool from a LangChain tool object.
def from_langchain(cls, tool: Any) -> "MelleaTool[..., Any]":

Comment thread mellea/backends/tools.py
@classmethod
def from_smolagents(cls, tool: Any) -> "MelleaTool":
def from_smolagents(cls, tool: Any) -> "MelleaTool[P, Any]":
"""Create a Tool from a HuggingFace smolagents tool object.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Same as from_langchainMelleaTool[..., Any] is the idiomatic spelling for "unknown parameter spec". Worth updating the docstring Returns: line too.

Suggested change
"""Create a Tool from a HuggingFace smolagents tool object.
def from_smolagents(cls, tool: Any) -> "MelleaTool[..., Any]":

Comment thread mellea/backends/tools.py
from .model_options import ModelOption

P = ParamSpec("P")
R = TypeVar("R")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Both mellea/core/base.py (lines 958-959) and this module declare their own P = ParamSpec("P") / R = TypeVar("R"). Since MelleaTool subclasses AbstractMelleaTool, tools.py could import P, R from base.py for a single identity. Minor.

Copy link
Copy Markdown
Contributor

@planetf1 planetf1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left some minor nits, but otherwise LGTM

@akihikokuroda
Copy link
Copy Markdown
Member Author

@planetf1 Thanks for review. I fixed them.

Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: investigate adding typing to MelleaTools

2 participants