On 2017-08-10 03:26, Fraser Tweedale wrote:
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.
This should be an issue with the right editor. For example PyCharm
supports stub files and displays type annotations. But I'm ok with both
external stub files and full inline comments. Since the FreeIPA team is
going to be both author and consumer of type annotations, the inline
comment style might be more appropriate. Unfortunately inline coments
can't be automated with stubgen. :/
Christian
--
Christian Heimes
Senior Software Engineer, Identity Management and Platform Security
Red Hat GmbH,
http://www.de.redhat.com/, Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Michael Cunningham, Michael
O'Neill, Eric Shander