📝Python's Type Annotations 📝- Why You Always Should Use It
Tags: python, webdev, datascience, computerscience
Published: 2020-10-31 10:48:20

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pn1vymphuj2v6rb5ma9h.png)

**Python** is a dynamically typed language and allows us to operate fairly freely on variables of different types. However, when writing code, we somehow assume which types of variables will be used (this may be caused by a limitation of the algorithm or business logic). And for the program to work correctly, it is important for us to find errors associated with transferring data of the wrong type as early as possible.

Keeping the idea of ​​dynamic duck typing in modern versions of **Python (3.6+)** supports annotations of variable types, class fields, arguments, and return values ​​of functions:

+ [PEP 3107 - Function Annotations](https://medium.com/r/?url=https%3A%2F%2Fwww.python.org%2Fdev%2Fpeps%2Fpep-3107%2F)
+ [PEP 484 - Type Hints](https://medium.com/r/?url=https%3A%2F%2Fwww.python.org%2Fdev%2Fpeps%2Fpep-0484%2F)
+ [PEP 526 - Syntax for Variable Annotations](https://medium.com/r/?url=https%3A%2F%2Fwww.python.org%2Fdev%2Fpeps%2Fpep-0526%2F)
+ `typing` package

Type annotations are read by the Python interpreter and are not processed in any way but are available for use from third-party code and are primarily designed for static analyzers.

In this article, I want to explain the basics and give examples of using type annotations and eventually show why it made my life as a Python developer much easier 🙂.

First, let's understand what type annotations are

---

# ▶ Type annotations - The basics

The types themselves are used to indicate the basic types of variables:
+ `str`
+ `int`
+ `float`
+ `bool`
+ `complex`
+ `bytes`
+ etc.

Unlike older versions of Python, type annotations are not written in comments or docstrings but directly in the code. On the one hand, this breaks backward compatibility. On the other, it clearly means that this is part of the code and can be processed accordingly.

In the simplest case, the annotation contains the directly expected type. More complex cases will be discussed below. If a base class is specified as annotation, it can pass instances of its descendants as values. However, you can use only those capabilities that are implemented in the base class.

Variable annotations are written with a colon after the identifier. This can be followed by value initialization. For instance:

```python
price: int = 5
title: str
```

Function parameters are annotated in the same way as variables, and the return value is specified after the arrow `->` and before the trailing colon. Let me give an example of using type annotations in a python function:

```python
def func(a: int, b: float) -> str:
    a: str = f"{a}, {b}"
    return a
```

For class fields, annotations must be specified explicitly when the class is defined. However, analyzers can automatically infer them based on the `__init__` method, but in this case, they will not be available at runtime:

```python
class Book:
    title: str
    author: str

    def __init__(self, title: str, author: str) -> None:
        self.title = title
        self.author = author

b: Book = Book(title="Fahrenheit 451", author="Bradbury")
```

---

# ▶ Type annotations - Built-in types

Although you can use standard types as annotations, there is a lot of useful stuff hidden in the module `typing`. Let's take a look at its' sub-modules.

# 1️⃣ Optional

If you mark a variable with a type `int` and try to assign to it None, there will be an error:

> Incompatible types in assignment (expression has type "None", variable has type "int")

Exactly for such cases, the typing module provides an annotation `Optional` indicating a specific type. Please note that the type of an optional variable is indicated in **square brackets**:

```python
from typing import Optional

amount: int
amount: None # Gives "Incompatible types" error

price: Optional[int]
price: None # Will work!
```

# 2️⃣ Any

Sometimes you don't want to restrict the possible types of a variable. For example, if it really doesn't matter, or if you plan on doing different types of handling yourself. In this case, annotation can be used `Any`. It will not swear at the following code:

```python
some_item: Any = 1
print(some_item)
print(some_item.startswith("hello"))
print(some_item // 0)
```

The question may arise, why not use `object`? However, in this case, it is assumed that although any object can be passed, it can only be treated as an instance `object`:

```python
some_object: object
print(some_object)
print(some_object.startswith("hello)) # ERROR: "object" has no attribute "startswith"

print(some_object // 0) # ERROR: Unsupported operand types for // ("object" and "int")
```

# 3️⃣ Union

For cases when it is necessary to allow the use of not all types, but only some, you can use the annotation `typing.Union` indicating the list of types in square brackets.

```python
def hundreds(x: Union[int, float]) -> int:
    return (int(x) // 100) % 100

hundreds(100.0)
hundreds(100)
hundreds("100")
# ERROR: Argument 1 to "hundreds" has incompatible type "str"; expected "Union[int, float]"
```

By the way, the annotation `Optional[T]` is equivalent to` Union[T, None]`, although such a notation is not recommended.

# 4️⃣ Collections

The mechanism of type annotations supports the mechanism of generics ([PEP484 - Generics](https://medium.com/r/?url=https%3A%2F%2Fwww.python.org%2Fdev%2Fpeps%2Fpep-0484%2F%23generics), for more details in the second part of the article), which allows specifying the types of elements stored in them for containers.

# 5️⃣ Lists

To indicate that a variable contains a list, you can use the list type as an annotation. However, if you want to specify which elements the list contains, such an annotation will no longer work. For this, there is `typing.List`. Similar to the way we specified the type of an optional variable, we specify the type of the list items in square brackets.

```python
titles: List[str] = ["hello", "world"]
titles.append(100500)
# ERROR: Argument 1 to "hundreds" has incompatible type "str"; expected "Union[int, float]"

titles = ["hello", 1]
# ERROR: List item 1 has incompatible type "int"; expected "str"

items: List = ["hello", 1]
# Everything is good!
```

The list is assumed to contain an indefinite number of similar items. But at the same time, there are no restrictions on annotation elements: You can use the `Any`, `Optional`, `List`, and others. If no element type is specified, it is assumed to be `Any`.

In addition to the list, there are similar annotations for sets: `typing.Set` and `typing.FrozenSet`.

# 6️⃣ Tuples

Tuples, unlike lists, are often used for different types of elements. The syntax is similar with one difference: the type of each element of the tuple is indicated in square brackets separately.

If you plan to use a tuple similarly to a list: store an unknown number of elements of the same type, you can use the ellipsis (`...`).

Annotation `Tuple` without specifying element types works the same way as `Tuple[Any, ...]`

```python
price_container: Tuple[int] = (1,)
price_container: ("hello")
# ERROR: Incompatible types in assignment (expression has type "str", variable has type "Tuple[int]")

price_container = (1, 2)
# ERROR: Incompatible types in assignment (expression has type "Tuple[int, int]", variable has type "Tuple[int]")

price_with_title: Tuple[int, str] = (1, "hello")
# Everything is good!

prices: Tuple[int, ...] = (1, 2)
prices: (1,)
prices: (1, "str")
# ERROR: Incompatible types in assignment (expression has type "Tuple[int, str]", variable has type "Tuple[int]")

something: Tuple = (1, 2, "hello")
# Everything is good!
```

# 7️⃣ Dictionaries

Used for dictionaries `typing.Dict`. Key type and value type are annotated separately:

```python
book_authors: Dict[str, str] = {"Fahrenheit 451": "Bradbury"}
book_authors["1984"] = 0
# ERROR: Incompatible types in assignment (expression has type "int", target has type "str")

book_authors[1984] = "Orwell"
# ERROR: Invalid index type "int" for "Dict[str, str]"; expected type "str"
```

Similarly used `typing.DefaultDict` and `typing.OrderedDict`

# 8️⃣ Function execution results

Any type annotations can be used to indicate the type of function result. But there are a few special cases.

If the function returns nothing (like how `print`), its result is always equal `None`. We also use for annotation `None`.

The correct options for completing such a function are: explicit return `None`, return without specifying a value, and termination without a call `return`:

```python
def nothing(a: int) -> None:
    if a == 1:
        return
    elif a == 2:
        return
    elif a == 3:
        return "" # No return value expected
    else:
        pass

```

If the function **never** returns control (for example, how `sys.exit`), use the annotation `NoReturn`:

```python
def forever() -> NoReturn:
    while True:
        pass
```

If this is a generator function, that is, its body contains an operator yield, you can use the annotation for the returned one `Iterable[T]`, either `Generator[YT, ST, RT]`:

```python
def generate_two() -> Iterable[int]:
    yield 1
    yield "2"
    # ERROR: Incompatible types in "yield" (actual type "str", expected type "int")
```

---

# Instead of a conclusion

For many situations, the typing module has suitable types, but I will not cover everything, since the behavior is similar to those described. For example, there `Iterator` is a generic version for `collections.abc.Iterator`, `typing.SupportsInt` to indicate that an object supports a method `__int__` or `Callable` for functions and objects that support a method `__call__`

The standard also defines the format of annotations in the form of comments and stub-files, which contain information only for static analyzers.

# Read More

*If you found this article helpful, click the💚 or 👏 button below or share the article on Facebook so your friends can benefit from it too.*

https://subscribe.to/raevskymichail