Blog>>Software development>>Backend>>Give Python more speed with type annotations

Give Python more speed with type annotations

Have you ever wondered if there is a way to speed up a Python project? I did, and I found such a way by using type annotations, but it is not what you think. Python type annotations do not make the code execution any faster. Python interpreter does not have such powers yet. The true speed comes from the development team. By using type annotations, programmers have a better understanding of the code base, make fewer silly mistakes, and thus, iterate quicker with new features.

The "Aha!" moment

I still remember the day I reluctantly decided to add type annotations to my Python project. As a seasoned developer, I had always viewed them as unnecessary overhead – something that only added complexity without providing any tangible benefits.

But then came the fateful night when our application went down in production due to a silly mistake related to types. It was 2 AM, and I received an urgent call from the project owner asking why our API was returning unexpected results. After scrambling through the code, I discovered that a simple typo had caused the issue – a variable meant to hold integers was being assigned a numeric string value.

The frustration was palpable as I frantically searched for the root cause of the problem. But in hindsight, it's clear that type annotations would have caught this error early on and prevented the entire debacle from occurring.

Fast-forward to the next day, I decided to take matters into my own hands. I sat down with a fresh cup of coffee and began annotating the codebase. At first, it felt like an uphill battle – tedious and unnecessary. But as I worked through the process, something clicked. The annotations started to make sense; they became a natural extension of my coding workflow.

The "Aha!" moment came when I realized that type annotations weren't just about adding extra syntax or making the code look prettier. They were actually helping me write better code – more robust and reliable code that would withstand the rigors of production.

From that day forward, I became a convert to the Church of Type Annotations. Our team saw significant improvements in development speed and quality as we continued to refine our annotated codebase. And when new developers joined our project, they were immediately impressed by the clarity and maintainability of the code – thanks in large part to those humble type annotations.

Later on we decided to migrate to FastAPI that utilizes Pydantic for API schema generation and validation. The Pydantic core functionality leverages Python types annotations. The migration was as easy as it can be, given how well-structured our annotated codebase already was. With the power of Pydantic behind us, we were able to generate robust APIs with minimal effort – a true testament to the value of type annotations.

As I look back on that journey, I'm reminded of the importance of embracing best practices and investing in tools that make development more efficient and enjoyable. Type annotations may seem like a minor detail at first, but their impact on code quality is undeniable. By adopting this practice early on, we were able to avoid costly mistakes down the line and focus on building innovative solutions rather than patching up errors.

A different layer of improvement

When it comes to speeding up Python projects, many developers focus on optimizing their code or using faster libraries. However, everybody seems to ignore the speedup of time to market, which is very often more important than optimizing a code base to the point where it becomes unreadable.

You already know from my “Aha!” moment that there is a subtle yet powerful tool that can have a significant impact on development speed and reliability. Type annotations are not just about making your code look prettier or run faster; they also provide numerous benefits for both the developer and the business.

For developers, type annotations offer several advantages. Firstly, it helps catch errors early in the development process, reducing the time spent debugging and fixing issues later on. This means that you can deliver features to users more quickly, which is essential in today’s fast-paced software development. Secondly, type annotations make your code easier to read and understand, allowing other developers to pick up where you left off without getting lost in a sea of technical debt.

But the benefits don’t stop there. For businesses, using type annotations can have significant long-term implications. By making it easier for new developers to join projects and contribute to their growth, companies can scale up their teams quickly and improve overall team productivity. Additionally, well-annotated codebases are more attractive to potential investors or acquirers, as they demonstrate a commitment to quality and maintainability - essential qualities in today’s competitive tech industry.

Type annotations may seem like an unnecessary nuisance at first glance, but they can have a huge impact on both the development process and the business. By embracing this best practice early on, you can deliver higher quality software faster and more reliably while your business can reap the rewards of increased productivity and improved investor appeal.

Understanding Python type annotations

One of the most significant benefits of using type annotations is improved code clarity. When your code includes explicit type declarations for variables, function parameters, and return types, it becomes much easier to understand what each part of the code does. This reduced cognitive load allows developers to focus on writing new features rather than trying to decipher complex logic.

In addition to improving code readability, type annotations can also help reduce bugs in your Python projects. By explicitly stating the expected data types for variables and function parameters, you're less likely to accidentally assign a value of the wrong type or pass an incorrect argument to a function. This reduction in errors means fewer debugging sessions and more time spent on actual development.

IDEs like PyCharm or VSCode also benefit from type annotations by providing better code completion suggestions, syntax highlighting, and error detection. Annotations provide additional context for AI and generative code assistance algorithms, allowing them to generate more accurate and relevant suggestions. For instance, when you’re writing a function that takes an obscure type from a third-party library as an input, the IDE can suggest possible values or methods based on the annotated type. The contextual code assistance can provide spot-on assistance for undocumented features. It saves me a lot of time, especially when dealing with packages that are just Python wrappers for C/C++ libraries. This not only saves time but also reduces errors by providing developers with intelligent recommendations.

The implications of this synergy are profound. With AI-powered code assistance tools like Kite, Copilot, or JetBrains AI Assistant, type annotations become even more valuable assets for developers. These tools use machine learning algorithms to analyze the structure and semantics of your codebase, making predictions about what you might want to do next. By incorporating type annotations into their analysis, these tools can provide more accurate suggestions that take into account not only the syntax but also the intended meaning of your code.

Now that we've discussed the benefits of using type annotations in Python, let's dive deeper into understanding how they work.

Python type annotations are a way to add explicit type information to your code. This is done by adding type hints after variable names or function parameters PEP484      link-icon. For example:

def greet(name: str) -> None:
    print(f"Hello, {name}!")

n this example, the greet function takes a single argument name, which is expected to be a string (str). The return type of the function is also annotated as None.

Type annotations are optional and don't affect how Python runs your code. They're purely for static analysis tools built in modern IDEs or standalone tools like mypy. No matter which one you prefer, the end result is the same. Much better visibility of what is happening in the source code.

One important thing to note about type annotations in Python is that they're not enforced at runtime. This means you can still assign a value of the unmatched type to a variable, even if it's annotated with a specific type. However, this doesn't mean type annotations are useless; instead, they serve as a contract between developers and IDEs.

Another benefit of understanding Python's type system is that it allows for more advanced features like generics (not yet fully supported in Python). It also allows you to run static typing analysis to better understand the code and find issues before runtime.

In the next chapter, we'll explore how type annotations contribute to faster development time by providing specific examples or case studies where type annotations have made a noticeable difference.

How type annotations contribute to faster development time

In the previous sections of this article, we've discussed how type annotations can improve code clarity and reduce bugs in Python projects. Now, let's explore a specific example of how type annotations can contribute to faster development time.

Suppose you're working on a project that involves processing large datasets. You need to write a function that takes a list of integers as input and returns the sum of all numbers in the list that are over a threshold value. Without type annotations, your code might look like this:

def sum_over_threshold(numbers, threshold):
    return sum(int(n) for n in numbers if n > threshold)

While this code works correctly, you need to read the body of the function to get an idea of what it will actually do. With type annotations, you can make the function more explicit and easier to understand.

def sum_over_threshold(numbers: list[str], threshold: int) -> int:
    return sum(int(n) for n in numbers if int(n) > threshold)

By adding type annotations, you’ve made it clear that the function takes a list of numbers in the form of a string (numeric string), and it will return an integer as a result. This makes the code easier to understand. 

The above example was a one-line function, already quite easy to comprehend. Now imagine a multiline, complex function that depends on the output of other complex functions. Without type annotations, it quickly gets out of control.

If, at some point, someone tries to put a list of integers as an argument, your linter of choice will catch that and raise a linting error before the change goes to production.

Here are some specific benefits of using type annotations for faster development:

1. Reduced error rate

One of the most significant benefits of using type annotations is that it can significantly reduce error rate. By specifying the expected types for variables, function parameters, and return values, you're less likely to introduce errors into your code in the first place.

For example:

def greet(name: str) -> None:
    print(f"Hello {name}!")

greet(123)  # Error! Expected a string.

In this case, if we hadn't used type annotations, we might not have caught that error until runtime. But with the str annotation on the name parameter, Python can catch the error during linting, either locally or in the CI environment, and prevent it from happening in the first place.

2. Improved code completion

Another way that type annotations contribute to faster development time is by improving code completion. When you're writing code, being able to see a list of available methods or properties for an object can be incredibly helpful.

For example:

import math

def calculate_area(shape: dict[str, float]) -> float:
    # ...

In this case, if we hadn't used type annotations on the shape parameter, our code completion tool might not have been able to provide us with a list of available methods or properties for that object. However, by specifying the expected types using type annotations, we can get more accurate and helpful code completion suggestions.

3. Better code organization

Type annotations can also help you organize your code better by providing a clear indication of what each function or method is intended to do.

For example:

def calculate_area(shape: dict[str, float]) -> float:
    # ...

def get_perimeter(shape: dict[str, float]) -> float:
    # ...

In this case, the type annotations on these functions provide a clear indication of what each function does and what types it expects as input. This can make your code easier to understand and maintain.

4. Easier code review

Finally, type annotations can make it easier for others (or yourself) to review your code by providing a clear understanding of the expected inputs and outputs for each function or method.

For example:

def calculate_area(shape: dict[str, float]) -> float:
    # ...

# Later in another file:

if __name__ == "__main__":
    shape = {"width": 10.0, "height": 5.0}
    area = calculate_area(shape)

In this case, the type annotations on the calculate_area function provide a clear indication of what types it expects as input and what type it returns. This can make it easier for someone reviewing your code to understand how it works.

By using type annotations in Python, you can reduce errors, improve code completion, better organize your code, and make it easier for others (or yourself) to review the source code. These benefits can all contribute to faster development time and more maintainable code over the long term.

Advanced type annotations and static typing libraries

In previous sections, we've discussed how type annotations can improve code clarity, reduce bugs, and contribute to faster development time in Python projects. Now, let's explore some advanced features of type annotations that can help you write more robust and maintainable code.

1. Generics

One of the most powerful features of type annotations is generics. Generics allow you to define reusable functions or classes with a specific type parameter. This means you can create generic types that work with any subtype, which makes your code more flexible and reusable.

For example, suppose you want to write a function that takes a list of values as input and returns the maximum value in the list. You could use generics to define this function like so:

from typing import TypeVar

T = TypeVar('T')

def max_value(values: list[T]) -> T:
    return max(values)

In this example, we're using a type variable T to represent any subtype that can be used as the input values. The function takes a list of values and returns the maximum value in the list. It can be a list of integers, floats, decimals, or even a custom type that complies with the built-in max() function. The annotated interface gives you a guarantee that this generic function can work with whatever data you throw at it.

2. Static typing libraries

One of the powerful libraries is already mentioned mypy. Mypy is an optional static type checker for Python that you can use to catch errors before the code is even executed.

For example, suppose you have a function that takes two integers as input and returns their sum:

def add_numbers(a: int, b: int) -> int:
    return a + b

If you try to call this function with non-integer values, mypy will catch the error before you execute the code. For instance, if you try to pass a or b as strings instead of integers, mypy will raise an error like this:

add_numbers("hello", 42)  # Error: Argument 1 has incompatible type "str"; expected "int"

This means that before even running your code, you can catch errors and prevent bugs from propagating further. Modern IDEs like PyCharm integrate mypy to show you warnings and errors while you write the code.

3. Type hints

Another feature of type annotations is type hints. Type hints are a way to provide additional information about the types used in your code without actually enforcing those types at runtime.

For example, suppose you have a function that takes an optional argument with a default value:

def greet(name: str = "World") -> None:
    print(f"Hello {name}!")

In this case, we're using type hints to indicate the expected type of the name parameter. This makes it clear what types are being used in your code without actually enforcing those types at runtime.

Type annotations can help you write more robust and maintainable Python code by providing specific benefits like improved readability, reduced bugs, faster development time, and better error handling. By using advanced features like generics and static typing libraries, you can take your type annotation game to the next level!

Best practices for using type annotations

So far, we've discussed how type annotations can improve code clarity, reduce bugs, and contribute to faster development time in Python projects. Now, let's explore some best practices for using type annotations effectively.

1. Use consistent naming conventions

When using type annotations, it's essential to use consistent naming conventions throughout your project. This makes it easier to read and understand your code.

For example, if you're using the list type annotation, make sure to consistently use List[T] or list[T] instead of mixing both formats. The first of them, capital List[T], comes from the Typing module, and it is required in Python < 3.9. But since version 3.9, it is preferable to use the list directly: PEP585      link-icon

2. Use type annotations for function parameters

When defining functions with multiple parameters, consider using type annotations for each parameter. This makes it clear what types are expected and helps catch errors before they even compile.

For example:

def greet(name: str, age: int) -> None:
    print(f"Hello {name}! You're {age} years old.")

3. Use type annotations for return types

When defining functions that return values, consider using type annotations to specify the expected return types.

For example:

def get_user_data() -> dict[str, int]:
    # ...
    return user_data

4. Avoid over-annotation

While it's essential to use type annotations consistently throughout your project, avoid over-annotating your code. This can make your code harder to read and understand.

Do:

def get_user_data() -> dict[str, int]:
    user_data = query_database()
    return user_data 

Don’t:

def get_user_data() -> dict[str, int]:
    user_data: dict[str, int] = query_database()
    return user_data

5. Use static typing libraries

Python interpreter does not enforce annotations (at least not yet). The IDE of your choice uses annotations for static analysis and suggestions, but to be absolutely sure, you need to lint your code. Make sure to run it before committing new changes, and, to be absolutely sure, run it again as part of the CI pipeline.

Strategies for leveraging type annotations

Start adding annotations to the core functionality first. The modules that change frequently and have the most impact on the business. With every new pull request you will have more and more annotations that help other developers. Remember that every small step counts towards improving your development process. Don't be afraid to try new things and learn from your mistakes – after all, that's what makes software development so rewarding!

Run mypy or any other capable linter in CI and keep an eye on the number of errors in already annotated parts of the code.

By integrating a linter into your CI pipeline, you can detect errors early on in the development process. This allows developers to catch and fix issues before they become major problems downstream. With a linter, you can identify syntax errors, type mismatches, and other coding mistakes that might otherwise go unnoticed until runtime.

When you integrate a linter into your CI pipeline, you can significantly reduce development time by catching errors early on. With linting tools, developers don't have to spend hours debugging issues or rewriting code because they caught an error too late in the process. This means that with every iteration of testing and refinement, developers are one step closer to delivering high-quality software faster.

Leverage frameworks and libraries that utilize annotations for internal logic. A prime example of this is FastAPI + Pydantic. Annotations are directly used by Pydantic to generate OpenAPI JSON schema. But that is not all. It also validates requests and builds request/response objects based on the annotations. You will never need to cast incoming payload to data models again. Pydantic will do it automatically based on the annotations you provided.

Benefits of using Python type annotations

As we've explored throughout this article, Python type annotations are a powerful tool for improving code quality, reducing errors, and enhancing collaboration among developers. By incorporating type annotations into your projects, you can reap numerous benefits that will make your development process more efficient, reliable, and enjoyable. Let’s wrap up what type annotations can give you.

Improved code readability

When variables and function parameters are annotated with their expected types, it becomes much easier for other developers (and even yourself) to understand the code's intent.

Reduced errors

By specifying the expected type of a variable or parameter, you can catch errors early on before they become major problems downstream by running linters and static analysis locally or in CI, or both!

Better code completion

Many Integrated Development Environments (IDEs) and text editors now support Python type annotations, providing better code completion suggestions based on your annotated types.

Improved collaboration

When multiple developers are working together on a project, using type annotations can help ensure that everyone is on the same page regarding variable and function usage.

Overcoming common objections

"I'm not sure what to annotate.": Start by annotating variables and functions with their most obvious types (e.g., int, str, etc.). As you become more comfortable, you can refine your annotations further.

"It's too much work.": Remember that type annotations are a one-time investment upfront. The benefits they provide will far outweigh the initial effort required to implement them.

More To Love

This article is just an overview and does not explore all the details about type annotations. If you want to know more about all the power annotations can give you, go to the PEPs catalog      link-icon for more information on this topic.

Zadrożny Paweł

Paweł Zadrożny

Senior Software Engineer

Paweł Zadrożny is a Software Engineer at CodiLime. His background includes broad backend experience, including development, implementation and maintenance of applications and websites and deployment of finished products. He is well versed in various programming languages, including Python, Go, JavaScript, and...Read about author >

Read also

Get your project estimate

For businesses that need support in their software or network engineering projects, please fill in the form and we’ll get back to you within one business day.

For businesses that need support in their software or network engineering projects, please fill in the form and we’ll get back to you within one business day.

We guarantee 100% privacy.

Trusted by leaders:

Cisco Systems
Palo Alto Services
Equinix
Jupiter Networks
Nutanix