Skip to content

Add the nuke-di mypy plugin to Mypy plugins - #158

Open
troyan-dy wants to merge 1 commit into
typeddjango:masterfrom
troyan-dy:add-nuke-di
Open

troyan-dy wants to merge 1 commit into
typeddjango:masterfrom
troyan-dy:add-nuke-di

Conversation

@troyan-dy

Copy link
Copy Markdown

Adds the mypy plugin of nuke-di to Mypy plugins, in alphabetical order.

nuke-di is dependency injection driven by type hints: a client asks for its dependencies in its type-hinted __init__, a function in its signature. The plugin, nuke_di.mypy, turns the runtime check of the dependency tree into a static one:

  • At every resolve(), inject(), @job and @worker it walks the __init__ of every client the call would build, as the container does, and reports what the container would raise, with the same message and the path to it: an argument that is not a client, a missing type hint, a circular dependency. Where the container stops at the first error, the plugin reports them all.
  • inject() is typed as the function without its injected arguments (def (user_id: int) -> Coroutine[Any, Any, str] instead of Callable[..., ...]).
$ mypy tree.py
tree.py:39: error: Argument "users" of "Profiles.__init__" is UserRepository, which is not a client (resolving Checkout -> Profiles)  [nuke-di]
tree.py:43: error: Circular dependency: settle -> Orders -> Payments -> Orders  [nuke-di]

Works with mypy 1.13+, with and without the cache. Docs: Checking the tree with mypy.

Disclosure: I am the author.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant