Open In App

FastAPI – Type Hints

Last Updated : 11 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The readability and maintainability of your codebase can be greatly improved by comprehending and utilizing type hints. Developers can identify potential bugs early in the development process and produce more dependable and effective applications by clearly defining the expected data types.

In this article, we will delve deeper into learning about Type Hints which is supported by FastAPI. A fundamental component of modern Python programming, type hints offer an organized method for defining the kinds of variables and function parameters.

What are Python Type Hints?

“Python supports type hints” (also known as “type annotations”). A special syntax known as “type hints” or annotations makes it possible to declare a variable’s type. You can get better support from editors and tools if you declare types for your variables.

This is just a quick tutorial/refresher about type hints. It covers only the minimum necessary to use them with FastAPI. Type hints form the foundation of FastAPI and provide numerous advantages and benefits.

Type Hints in FastAPI Example

Code Without Type Hints

Let’s start with a simple example, Assume we are writing a function that takes the “name” of a person of the person, converts the first letter to upper case, forms sentences, and returns it.

Python3




def get_name_and_age(name):
    name_with_age = "My name is: " + name.title()
    return name_with_age
print(get_name_and_age('geeks for geeks'))


Output

Geeks For Geeks

This is a very simple program. Imagine, though, that you were writing it from the beginning. You would have eventually begun defining the function and prepared the parameters. The “that method that converts the first letter to upper case” must then be called. Was it upper? Was it uppercase? first_uppercase? capitalize?

Then you try editor autocompletion. To cause the completion, type the function’s first parameter, name, followed by a dot (. ), and then press Ctrl+Space. Sadly, though, you receive nothing helpful:

Screenshot-(179)

Editor autocompletions without type hints

Code with Type Hints

Let’s change just one line from the earlier version. We will exactly alter this section, the function’s parameters, from:

name

to,

name : str

That’s it. This is the “type hints”. The final code would be:

Python3




def get_name_and_age(name:str):
    name_with_age = "My name is: " + name.title()
    return name_with_age
print(get_name_and_age('geeks for geeks'))


Also, adding type hints typically has no effect on the outcome from what would occur without them. Now, however, picture yourself once more in the midst of developing that function, but this time with type hints. At the same point, you try to activate the autocomplete by pressing Ctrl+Space and observe.

Screenshot-(180)

Editor autocompletion with type hints.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads