mypy plugin

We provide several mypy plugins to fix existing issues and improve type-safety of things developers commonly use:

Installation

To install any mypy plugin add it to the plugins section of the config file.

[mypy]
plugins =
  returns.contrib.mypy.returns_plugin

We recommend to always add our plugin as the first one in chain.

Configuration

You can have a look at the suggested mypy configuration in our own repository.

You can also use nitpick tool to enforce the same mypy configuration for all your projects.

We recommend to use our own setup. Add this to your pyproject.toml:

[tool.nitpick]
style = "https://raw.githubusercontent.com/wemake-services/wemake-python-styleguide/master/styles/mypy.toml"

And use flake8 to lint that configuration defined in the setup matches yours. This will allow to keep them in sync with the upstream.

Supported features

  • curry feature allows to write typed curried functions

  • partial feature allows to write typed partial application

  • flow feature allows to write better typed functional pipelines

  • decorators allows to infer types of functions that are decorated with @safe, @maybe, @impure, etc

  • pointfree provides better typing inference for some problematic Pointfree helpers

API Reference

Plugin defenition

graph TD; _ReturnsPlugin Plugin --> _ReturnsPlugin

Custom mypy plugin to solve the temporary problem with python typing.

Important: we don’t do anything ugly here. We only solve problems of the current typing implementation.

mypy API docs are here: https://mypy.readthedocs.io/en/latest/extending_mypy.html

We use pytest-mypy-plugins to test that it works correctly, see: https://github.com/mkurnikov/pytest-mypy-plugins

plugin(version)[source]

Plugin’s public API and entrypoint.

Parameters

version (str) –

Return type

Type[Plugin]

Curry

graph TD; _ArgTree _CurryFunctionOverloads
analyze(ctx)[source]

Returns proper type for curried functions.

Parameters

ctx (FunctionContext) –

Return type

Type

Partial

graph TD; _AppliedArgs _PartialFunctionReducer
analyze(ctx)[source]

This hook is used to make typed curring a thing in returns project.

This plugin is a temporary solution to the problem. It should be later replaced with the official way of doing things. One day functions will have better API and we plan to submit this plugin into mypy core plugins, so it would not be required.

Internally we just reduce the original function’s argument count. And drop some of them from function’s signature.

Parameters

ctx (FunctionContext) –

Return type

Type

Flow

graph TD;
analyze(ctx)[source]

Helps to analyze flow function calls.

By default, mypy cannot infer and check this function call:

>>> from returns.pipeline import flow
>>> assert flow(
...     1,
...     lambda x: x + 1,
...     lambda y: y / 2,
... ) == 1.0

But, this plugin can! It knows all the types for all lambda functions in the pipeline. How?

  1. We use the first passed parameter as the first argument to the first passed function

  2. We use parameter + function to check the call and reveal types of current pipeline step

  3. We iterate through all passed function and use previous return type as a new parameter to call current function

Parameters

ctx (FunctionContext) –

Return type

Type

Decorators

graph TD;
analyze(ctx)[source]

Changes a type of a decorator.

This problem appears when we try to change the return type of the function. However, currently it is impossible due to this bug: https://github.com/python/mypy/issues/3157

It uses the passed function to copy its type. We only copy arguments and return type is defined by type annotations.

Parameters

ctx (FunctionContext) –

Return type

Type

Pointfree

graph TD;
analyze(ctx)[source]

Analyzes several pointfree functions.

Removes intermediate Protocol instances.

Parameters

ctx (FunctionContext) –

Return type

Type