An Interest In:
Web News this Week
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
- April 13, 2024
Python's Type Annotations-Why You Always Should UseIt
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
- PEP 484-Type Hints
- PEP 526-Syntax for Variable Annotations
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-Thebasics
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:
price: int = 5title: "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:
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:
class Book: title: "str" author: str def __init__(self, title: "str, author: str) -> None:" self.title = title self.author = authorb: Book = Book(title="Fahrenheit 451", author="Bradbury")
Type annotations-Built-intypes
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:
from typing import Optionalamount: intamount: None # Gives "Incompatible types" errorprice: 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:
some_item: Any = 1print(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
:
some_object: objectprint(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.
def hundreds(x: Union[int, float]) -> int: return (int(x) // 100) % 100hundreds(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 toUnion[T, None]
, although such a notation is not recommended.
4 Collections
The mechanism of type annotations supports the mechanism of generics (PEP484-Generics, 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.
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,...]
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:
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
:
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
:
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]
:
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.
Original Link: https://dev.to/mikhailraevskiy/python-s-type-annotations-why-you-always-should-use-it-4lh2
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To