Djangocong 2018 - Lille

 

Les outils pour améliorer votre code python

Par Stéphane "Twidi" Angel (twidi sur Github, Twitter...)

 

Sponsorisé par isshub.io

PEP

Python Extension Proposal

https://www.python.org/dev/peps/

PEP 20 -- The Zen of Python

 

 

 

 

 

Coding party

Écrivons un peu de python dans un fichier code.py.

code.py

 

shell

 

flake8

 

shell

 

pycodestyle

 

pyflakes

 

mccabe

 

shell

 

code.py

 

shell

 

 

PEP 8 -- Style Guide for Python Code

 

 

 


http://pep8.org

Correction

shell

 

code.py

 

code.py - corrigé

 

flake8 plugins

flake8-docstrings

 

pydocstyle

 

PEP 257 -- Docstring Conventions

 

A universal convention supplies all of maintainability, clarity,consistency, and a foundation for good programming habits too.What it doesn't do is insist that you follow it against your will.That's Python!"

—Tim Peters on comp.lang.python, 2001-06-16

Specification

flake8-docstrings

shell

 
 

code.py

 

shell

 

code.py

 

pylint

 

shell

 

shell

 

code.py

 

pylint.extensions.docparams

Parameter documentation checker
If you document the parameters of your functions, methods and constructors and their types systematically in your code this optional component might be useful for you.

Sphinx style, Google style, and Numpy style are supported.

Sphinx style

:param first: The first number to add
:type first: int
:param second: The second number to add
:type second: int
:returns: The result of the computation of first and second
:rtype: int

Google style

Args:
    first (int): The first number to add
    second (int): The second number to add

Returns:
    int: The result of the computation of first and second

Numpy style

Parameters
----------
first: int
    The first number to add
second: int
    The second number to add

Returns
-------
int
    The result of the computation of first and second

Installation

shell

 

.pylintrc

 

shell

 

code.py

 

flake8, suite

shell

 

code.py

 

MAIS...

Quand même, ça serait bien si...

Vraiment bien si...

On n'avait pas à...

Penser à tout ça...
Et qu'on avait...

Les auto-formateurs

Pour

On recommence

code.py

 

autopep8

A tool that automatically formats Python code to conform to the PEP 8 style guide.
It uses the pycodestyle utility to determine what parts of the code needs to be formatted. autopep8 is capable of fixing most of the formatting issues that can be reported by pycodestyle.

shell

 

shell

 

code2.py

 

yapf

Yet another Python formatter

A formatter for Python files
The ultimate goal is that the code YAPF produces is as good as the code that a programmer would write if they were following the style guide.

shell

 

shell

 

code2.py

 

black

The uncompromising Python code formatter
 

shell

 

shell

 

code.py

 

Autres exemples (black)

avant

 

après

 

avant

 

après

 

avant

 

après

 

MAIS... les docstrings ???

Les éditeurs/IDE

Example PyCharm

 

isort

Sort your python imports for you so you don't have to.

shell

 

avant

 

après

 

Typing annotations

PEP 3107 -- Function Annotations

This PEP introduces a syntax for adding arbitrary metadata annotations to Python functions.
 
 

PEP 484 -- Type Hints

[...]the community would benefit from a standard vocabulary and baseline tools within the standard library.
This PEP introduces a provisional module to provide these standard definitions and tools, along with some conventions for situations where annotations are not available.
 

PEP 526 -- Syntax for Variable Annotations

This PEP aims at adding syntax to Python for annotating the types of variables.
 

 

 

Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.

 

 

Python restera un langage dynamiquement typé, et les auteurs n'ont aucun désir de obligatoires les indications de type, jamais, même par convention.

 

Annotations

code.py

 

shell

 

mypy

Optional Static Typing for Python
 

shell

 

code.py

 

shell

 

code.py

 

shell

 

pyre

A performant type-checker for Python 3
We believe statically typing what are essentially fully dynamic languages gives our code stability and improves developer productivity.

shell

 

code.py

 

shell

 

Générateur d'annotations de type

pyannotate

Auto-generate PEP-484 annotations

MonkeyType

A system for Python that generates static type annotations by collecting runtime types

Final code

avant

 

après

 

Déroulement classique

  1. Configuration éditeur / IDE

    1. docstrings

    2. analyse de code

    3. ré-écriture automatique

  2. Coder

  3. Lancer les checks avant chaque commit (ou via commithook)

    1. lint

      1. black --check
      2. flake8
      3. pylint
      4. mypy
    2. tests

  4. Si OK

    1. push

    2. pull/merge-request...

  5. Revue de code

    1. ENJOY. Adieu:

      1. les pertes de temps,
      2. les amitiés rompues,
      3. Les jetages décran par la fenêtre,
      4. etc...

Conclusion

JUST DO IT

Vous et votre équipe me remercierez à la prochaine conf ;)

Example concret

mixt: https://github.com/twidi/mixt/

Merci

Take back control of your Github issues, pull requests, and notifications

Les slides seront publiés sur https://twidi.github.io/