DEV Community

Cover image for Python Type Hints and .pyi Files: Enhancing Code Readability and Tooling
AissaGeek
AissaGeek

Posted on • Edited on

Python Type Hints and .pyi Files: Enhancing Code Readability and Tooling

As software development evolves, code readability and understanding become ever more critical. Modern programming languages now often include features that enable explicit typing, aiding in the understanding of functions and variables. Python, known for its simplicity and readability, has introduced a concept called "type hints" in recent years to assist developers in creating more readable and safer code.

Understanding Python Type Hints

Python is a dynamically typed language, which means the type (like integer, string, list, etc.) of a variable is checked during runtime. You don't need to specify the type of a variable when you declare it, making Python flexible and easy-to-use. But this flexibility can sometimes lead to bugs that are hard to spot.

Python 3.5 introduced "type hints" as a new feature that allows developers to indicate the type of a variable or the return type of a function. Here's a quick example:

def greet(name: str) -> str:
    return 'Hello, ' + name
Enter fullscreen mode Exit fullscreen mode

In this example, name: str indicates that the greet function expects a string as an argument. The -> str part indicates that the greet function will return a string.

Type hints do not affect the actual runtime of the program; they are merely "hints". However, they can significantly enhance code readability, and tools like Mypy, Pyright, or PyCharm can use them to catch potential bugs before runtime.

Introducing .pyi Files: Stub Files for Type Annotations

Python 3.5's type hint system works well but can fall short in certain situations. For example, what if you want to add type hints to a third-party library, or an existing codebase, without touching the original code?

This is where Python's stub files, or .pyi files, come into play. Python Interface files, as they're known, allow you to provide type information separately from your Python source files.

Here's a simple example. Let's say we have a Python file math_utils.py:

# math_utils.py
def add(x, y):
    return x + y
Enter fullscreen mode Exit fullscreen mode

We can create a corresponding .pyi file to provide type information:

# math_utils.pyi
def add(x: int, y: int) -> int: ...
Enter fullscreen mode Exit fullscreen mode

The ... is used in Python stub files to indicate that the body of the function is omitted.

Now, any type checker like Mypy, Pyright, or PyCharm, which is configured correctly, will consider add as a function that takes two integers and returns an integer. It will then flag any misuse in your code.

# main.py
import math_utils

print(math_utils.add(5, 7))  # This is fine
print(math_utils.add("hello", "world"))  # This will be flagged by mypy
Enter fullscreen mode Exit fullscreen mode

When you run mypy main.py, it would return an error for the line print(math_utils.add("hello", "world")), catching a potential bug before the code even runs.

Conclusion

Python's type hinting, along with .pyi files, offers a valuable way to make your Python code more understandable and safer. While these are optional tools, they can be instrumental in catching bugs early in large codebases and making your code easier to understand for other developers. As Python continues to grow in popularity, these features will undoubtedly become even more valuable.

Follow up for more, your support makes me high :D

Top comments (0)