Mypy improves static type-checking for big Python apps

Mypy's performance improvements are aimed at maintaining large-scale apps and speeding up the parsing process

Mypy improves static type-checking for big Python apps

Mypy, a code-linting project created to allow static type checking in Python, has been improved to benefit larger, more complex projects and speed up parsing.

One of Python's big attractions is its flexible type system, which allows developers to produce useful software in less time than with statically typed languages. But dynamic typing's big pitfall has always been inaccuracy. It's too easy to submit the wrong kind of variable to a function, never check to make sure it's what's expected, then get bitten by the consequences later in production.

Mypy was proposed to help avoid these problems. Developers can take a conventional Python application and add annotations in a special type-hinting syntax. When the Python app is converted to bytecode by the interpreter, Mypy performs its type-checking magic and generates error reports if it finds anything amiss.

The latest version of Mypy, 0.4, has several key improvements that make it more useful to maintainers of big applications. If you pass the --incremental flag to Mypy, the results of file checks are cached and can be reused between runs. Large projects with many files benefit from this, since changes to such projects tend to affect only a few files at a time, and it makes little sense to recheck files that haven't been touched.

Another change to MyPy is an experimental parser that uses a modified version of Python's standard library module ast to perform application parsing. The modified parser speeds up the parsing process, although it has to be installed from a separately maintained project (typed-ast) before it can be used.

One of Mypy's biggest conveniences is that syntax can be added gradually to an application, on a function-by-function or module-by-module basis. That boon is in line with other enhancements to the language Cython, a system for compiling Python modules to C for the sake of speed that can compile Python code as-is for a modest improvement in performance. Developers can gradually layer in type annotations for more speed across the course of the development cycle, rather than stopping and rewriting everything at once.

Mypy's approach to static typing in Python has already influenced the language. Type-hinting syntax was officially enshrined as an extension to Python 3.5, although it's also possible to use Mypy on Python 2 applications.

Python creator Guido van Rossum has made it clear that adding support for Mypy syntax to Python 3.5 doesn't mean the language will become statically typed. The MyPy syntax is intended only to be used for the sake of linting-type applications, not as a prelude to making Python a statically typed language.

It's entirely possible that someone could develop a fork of Python with such features baked in, or use Microsoft's proposed Python compiler extensions as an add-on for the stock CPython runtime. But the core of Python will likely always remain a dynamic language.

Copyright © 2016 IDG Communications, Inc.

How to choose a low-code development platform