We provide several mypy
plugins to fix existing issues
and improve type-safety of things developers commonly use:
returns_plugin
to solve untyped decorator issue and add better Curry support
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.
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.
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
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
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.
ctx (FunctionContext
) –
Type
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?
We use the first passed parameter as the first argument to the first passed function
We use parameter + function to check the call and reveal types of current pipeline step
We iterate through all passed function and use previous return type as a new parameter to call current function
ctx (FunctionContext
) –
Type
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.
ctx (FunctionContext
) –
Type