On Wed, Aug 09, 2017 at 10:18:33AM +0200, Christian Heimes via FreeIPA-devel wrote:
On 2017-08-08 08:04, Fraser Tweedale via FreeIPA-devel wrote:
> Hi team,
>
> At PyCon Australia on the weekend I was reminded of PEP-484 type
> hinting** and the Mypy type checker for Python.
>
> With focus of FreeIPA project shifting more towards stability,
> quality and maintainability, and with Python 3 porting work nearly
> wrapped up, now is the time to think about how we can get more
> confidence in our code not just from tests, but from the code
> itself. Static checking of annotated types can help us there, and
> Mypy can let us begin to do this when writing new code or
> refactoring old code. Furthermore there is a benefit for IDE-users
> where plugins can use type annotations to provide better completion
> suggestions, etc. For an overview of Mypy please see the PyCon AU
> talk[1] or the docs[2].
>
> [1]
https://www.youtube.com/watch?v=mXfsMDM3LwQ
> [2]
http://mypy.readthedocs.io/en/latest/index.html
>
> So, what's the plan? Alongside my other tasks, I'm going to start
> looking at how we could use Mypy in FreeIPA CI, and see what it is
> like using types in some of the areas I'm familiar with e.g.
> ipalib.x509. Based on my findings I'll update the team on the wins
> and challenges and we can decide how to proceed from there.
Felipe ask me about typing and Mypy a couple of weeks ago. It's a good
idea and we should do it. But I advise against typing information in the
source code. FreeIPA should use external stub files for two reasons.
First of all it is required to stay compatible with Python 2. And more
importantly it's faster. FreeIPA's CLI scripts already take several
hundred milliseconds to execute. Typing would slow them down even further.
I disagree with using stub files. Types should be declared where
the functions are defined. Types are documentation and proximity is
important (for humans).
Fortunately, Mypy supports "type comments" in addition to PEP 3107
function annotations. Mypy groks them but CPython will treat them
as comments and discard. This allows us to use type hints with no
runtime cost for the CLI scripts.
It's rather easy to auto-generate stub files -- assuming you are
running
on Fedora and have all Python 3 dependencies installed:
$ sudo dnf install python3-mypy
$ echo "api.bootstrap(ra_plugin='dogtag')" >> ipalib/__init__.py
$ mkdir out
$ PYTHONPATH=. stubgen --recursive ipaclient ipalib ipaplatform
ipapython ipaserver
The api.bootstrap() call is required. Otherwise stubgen cannot import a
bunch of plugin files.
Thanks for this additional info.
Cheers,
Fraser