RFC: Enable type annotations and Mypy checks on Python projects in Fedora Infrastructure

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi everyone,

RFC: Enable type annotations and Mypy checks on Python projects in Fedora Infrastructure


We have many Python applications under Fedora Infrastructure, and we
also on a path to move to the Python3 world. I am proposing the
following changes for our Python applications:

* Add type annotations in any new code we write.
* Add type annotations in code we change
* Enable Mypy checks for every pull requests and commits in CI.
* During Flock we try to enable type annotations in our favorite Python applications.


## What is Type Annotation

This is a feature of Python3, also backported to the Python2 world using
the python2-typing module. Type annotations or type hints are purely for
external type checking tools, they do not affect the Python interpreter
anyway. PEP-484 discusses more on the type hints [1].

## Example (which works in both Python2 and Python3)

    def add(a, b):
	# type: (int, int) -> int
	"Adds two numbers"
	return a + b



## What is Mypy?

Mypy [2] is an open source type checking tool, most of the developers
are in Dropbox. Mypy can be used on a codebase gradually, means we don't
have to add all the type information in one go. We can slowly keep
improving the type annotations.

## What are benefits of enable type annotations and Mypy?


### Readability of the code

Because of the checks, we will always have right data types mentioned,
which increases the readability of the code a lot for any future
maintainer.

### Finding actual logical bugs

There are times when we have functions which generally returns a
boolean, but it some corner cases, maybe it returns an empty string or
None value. These may cause runtime errors. Using Mypy on your codebase
improves the chances of finding any such issue in the development cycle
only.

### Refactoring becomes easier

As the tool itself checks for all of the API use cases, refactoring
larger codebases will be easier with Mypy.


## Which are the few big projects using Mypy?

Dropbox has around 700K lines of Python code, and Zulip project has
around 100K of Python with type information. They also have an excellent
blog post at [3].

[1] https://www.python.org/dev/peps/pep-0484/
[2] http://mypy.readthedocs.io/en/latest/
[3] http://blog.zulip.org/2016/10/13/static-types-in-python-oh-mypy/

Kushal
-- 
Fedora Cloud Engineer
CPython Core Developer
https://kushaldas.in
https://dgplug.org
_______________________________________________
infrastructure mailing list -- infrastructure@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to infrastructure-leave@xxxxxxxxxxxxxxxxxxxxxxx




[Index of Archives]     [Fedora Development]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [KDE Users]

  Powered by Linux