🔎Unit 1: Python Type Hint
Type hints in Python are a way to explicitly specify the type of a variable, which can be very helpful in making your code more readable and maintainable. Type hints are annotations you add to your Python code to specify what type of data each variable should hold. They were introduced in Python 3.5 and have become a standard practice for many Python developers.
Type Hints with Editor's Autocompletion
Consider the following basic Python program that receives two variables: fullname
and lastname
. It returns a concatenated string of these two variables, forming a complete name. Additionally, the program capitalizes the first letter of each word in the resulting full name.
It's advisable to manually enter the code provided, rather than relying on copy-and-paste. This approach highlights the advantages of utilizing Type Hints in your coding practice.
In modern Integrated Development Environments (IDEs) like PyCharm and Microsoft Visual Studio Code, manual code entry is enhanced by the editor's autocompletion feature. However, as illustrated in Fig. 1, this autocomplete feature proves ineffective in certain scenarios. This shortcoming arises because the IDEs are unable to discern the types associated with specific variables, such as fullname
and lastname
.
To address this limitation, we have explicitly specified the data type for each parameter, as illustrated in Fig. 2.
Basic Syntax
The syntax is straightforward. You use a colon :
after the variable name, followed by the type. For instance:
Benefits of Using Type Hints
Improved Code Readability: Others can understand what type of data your functions expect and return.
Early Bug Detection: Tools like MyPy can use type hints to detect type-related errors in your code without having to run it.
Better IDE Support: Modern Integrated Development Environments (IDEs) use type hints for features like auto-completion and type checking.
Function Annotations
You can also add type hints to function parameters and return types. For example:
Data Types
You can use all primitive data types available in Python for Type Hints, for example:
int
float
bool
bytes
Generic Types with Type Parameters
Certain data structures in Python, such as dictionaries (dict
), lists
, sets
, and tuples
, are capable of holding values of various types. Importantly, the elements contained within these structures can themselves be of distinct types.
Such data structures are referred to as generic
types, a term denoting their ability to hold elements of multiple types. Python allows for explicit declaration of these generic types along with the types of their internal elements.
For facilitating this, Python includes a specialized module named typing
. This module is specifically designed to provide support for these type hints, enabling more precise and clear code annotations regarding the types of elements within these generic structures.
For Python 3.8 and earlier versions, you need to import and use the typing module to declare the type annotations, as follows:
include from typing import List
Tuple and Set
In the program above:
The variable
items_t
is a tuple with 3 items, two integer values and a string.The variable
items_s
is a set, and each of its items is of type bytes.
Dict
To define a dict
as Type Hints, you pass 2 type parameters, separated by commas.
The first type parameter is for the keys of the dict;
The second type parameter is for the values of the dict.
Classes as Types
You can also declare a class as the type of a variable. Let's take a look in the example below.
Type Hints with Metadata Annotations
Python also has a feature that allows putting additional metadata in these type hints using Annotated
. Let's take a look:
You can use Annotated
to provide FastAPI with additional metadata about how you want your application to behave.
Last updated