Skip to content

Commit 8e8e93d

Browse files
authored
Mypy plugin for typed decorators (#88)
* WIP: first working prototype * Also type @impure * Working mypy plugin * Sorts deps
1 parent ace1312 commit 8e8e93d

File tree

15 files changed

+320
-33
lines changed

15 files changed

+320
-33
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ install:
2020
script:
2121
- poetry run flake8 returns tests docs
2222
- poetry run mypy returns tests/**/*.py
23-
- poetry run pytest
23+
- poetry run pytest tests
24+
# Temporary work-around of
25+
# https://github.com/mkurnikov/pytest-mypy-plugins/issues/2
26+
- poetry run pytest -p no:cov -o addopts="" --mypy-ini-file=setup.cfg typesafety
2427
- poetry run doc8 -q docs
2528
- poetry check
2629
- poetry run pip check

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ We also use `wemake_python_styleguide` to enforce the code quality.
3434
To run all tests:
3535

3636
```bash
37-
pytest
37+
pytest tests
38+
pytest -p no:cov -o addopts="" --mypy-ini-file=setup.cfg typesafety
3839
```
3940

4041
To run linting:

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ Make your functions return something meaningful, typed, and safe!
2424
pip install returns
2525
```
2626

27+
You might also need to [configure](https://returns.readthedocs.io/en/latest/pages/container.html#type-safety)
28+
`mypy` correctly and install our plugin:
29+
30+
```cfg
31+
[mypy]
32+
plugins =
33+
returns.contrib.mypy.decorator_plugin
34+
```
35+
2736
Make sure you know how to get started, [check out our docs](https://returns.readthedocs.io/en/latest/)!
2837

2938

docs/pages/container.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ since we are using ``__slots__`` for better performance and strictness.
250250

251251
Well, nothing is **really** immutable in python, but you were warned.
252252

253+
.. _type-safety:
253254

254255
Type safety
255256
-----------
@@ -266,8 +267,11 @@ compatible ``.pyi`` files together with the source code.
266267
In this case these types will be available to users
267268
when they install our application.
268269

269-
However, this is still good old ``python`` type system,
270-
and it has its drawbacks.
270+
We also ship custom ``mypy`` plugins to overcome some existing problems,
271+
please make sure to use them,
272+
since they increase your developer experience and type-safety:
273+
274+
- ``decorator_plugin`` to solve untyped `decorator issue <https://github.com/python/mypy/issues/3157>`_
271275

272276
You can have a look at the suggested ``mypy``
273277
`configuration <https://github.com/dry-python/returns/blob/master/setup.cfg>`_

docs/pages/io.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ with ``@impure`` for better readability and clearness:
168168
Limitations
169169
~~~~~~~~~~~
170170

171-
There's one limitation in typing
172-
that we are facing right now
173-
due to `mypy issue <https://github.com/python/mypy/issues/3157>`_.
171+
Typing will only work correctly
172+
if :ref:`decorator_plugin <type-safety>` is used.
173+
This happens due to `mypy issue <https://github.com/python/mypy/issues/3157>`_.
174174

175175

176176
FAQ

docs/pages/result.rst

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -209,29 +209,9 @@ Supports both async and regular functions.
209209
Limitations
210210
~~~~~~~~~~~
211211

212-
There's one limitation in typing
213-
that we are facing right now
214-
due to `mypy issue <https://github.com/python/mypy/issues/3157>`_:
215-
216-
.. code:: python
217-
218-
from returns.result import safe
219-
220-
@safe
221-
def function(param: int) -> int:
222-
return param
223-
224-
reveal_type(function)
225-
# Actual => def (*Any, **Any) -> builtins.int
226-
# Expected => def (int) -> builtins.int
227-
228-
This effect can be reduced
229-
with the help of `Design by Contract <https://en.wikipedia.org/wiki/Design_by_contract>`_
230-
with these implementations:
231-
232-
- https://github.com/deadpixi/contracts
233-
- https://github.com/orsinium/deal
234-
- https://github.com/Parquery/icontract
212+
Typing will only work correctly
213+
if :ref:`decorator_plugin <type-safety>` is used.
214+
This happens due to `mypy issue <https://github.com/python/mypy/issues/3157>`_.
235215

236216

237217
API Reference

poetry.lock

Lines changed: 69 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pytest = "^4.6"
4747
pytest-cov = "^2.7"
4848
pytest-randomly = "^3.0"
4949
pytest-asyncio = "^0.10.0"
50+
pytest-mypy-plugins = "^0.3.0"
5051

5152
sphinx = "^2.1"
5253
sphinx-autodoc-typehints = "^1.6"

returns/contrib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-

returns/contrib/mypy/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-

0 commit comments

Comments
 (0)