Type annotations

Starting with version 3.6, Python introduced type annotations – these hint at what data types the function expects to get, and what data type it will return.

Let's look at the following example. Here is the negative power function we declared already. This time, however, we added the type annotations both for the function arguments and the outcome:

def negative_power(v:int, p:int)-> int: 
'''Return negative value v in power p'''
return -1 * (v**p)

First of all, those additional characters do not affect code execution in any direct way. Those are type annotations – merely hints at the function's expected data types for arguments and return value, stored within the function – in the same way docstrings are.

While there is no effect on the code itself, those annotations can be used for testing purposes and raise an error, if the function is fed with the wrong data. The most popular tool for that testing is called mypy, developed at Dropbox. Once installed, mypy can run through your code and test whether any of the operations with data type annotations are used inappropriately. 

There are other use cases as well. For example, a FastAPI web framework that we'll use in Chapter 18Serving Models with a RESTful API, of this book, uses type annotations to validate inbound API calls, while Cython and Numba, which we'll discuss in Chapter 20, Best Practices and Python Performancecan use type annotations to efficiently compile Python into optimized C code.

Type annotations are not bound to basic variable types either. You can describe more complex requirements using the standard typing package; for example, you can explicitly describe any type using the Any class, or state that you expect a tuple, filled with string variables. Nevertheless, type annotations are a very recent phenomenon, not widely adopted, and have limited support throughout the libraries. In this book, we will use them only in Chapter 18, Serving Models with a RESTful API.

This concludes the main body of information that you'll need in order to write effective functions. Given all that, let's revisit our budgeting example!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset