8 months, 3 weeks

Type-hinting Generics




 

Type-hinting annotations allow you to annotate variable, argument, and function return types with type definitions. These type annotations serve documentational purposes, but can also be used to validate your code using external tools. 

There are also static type checkers, such as mypy or pyright, that can be used to scan through the whole code base and report all typing errors of code units that use annotations.

In older versions of Python, built-in collection types could not be annotated so easily with types of their content. The typing module provides special types that can be used for that purpose.

These types include:
• typing.Dict for dictionaries
• typing.List for lists
• typing.Tuple for tuples
• typing.Set for sets
• typing.FrozenSet for frozen sets


These types are still useful if you need to provide functionality for a wide spectrum of Python versions, but if you're writing code for Python 3.9 and newer releases only, you should use the built-in generics instead. Importing those types from typing modules is deprecated and they will be removed from Python in the future.


Responses(0)







Related