Skip to content

academy.mypy_plugin

Academy mypy plugin.

This plugin enables mypy to perform static type inference on Handle types.

For example, the return type of an action invocation on a remote agent via a handle is a Future wrapping the return type of the action.

from academy.behavior import Behavior, action
from academy.handle import Handle

class Example(Behavior):
    @action
    def get_value(self) -> int: ...

handle: Handle[Example]

reveal_type(handle.get_value())
# note: Revealed type is "Future[int]"
Without the plugin, mypy will default to Any.

Note

The plugin makes a best effort to warn users about incorrect use of agent handles. This includes raising errors when accessing attributes of a behavior, rather than methods, via a handle and when incorrect parameter types are passed to an action. However, the plugin cannot distinguish which callable attributes of a handle are annotated as actions, but this will still produce an error at runtime.

Enable the plugin by adding academy.mypy_plugin to the list of plugins in your mypy config file.

  • pyproject.toml
    [tools.mypy]
    plugins = ["academy.mypy_plugin"]
    
  • mypy.ini and setup.cfg
    [mypy]
    plugins = academy.mypy_plugin