Typeguard

4.5.1 · active · verified Sat Mar 28

Typeguard is a Python library that provides run-time type checking for functions and methods defined with PEP 484 type annotations, as well as for arbitrary objects. It acts as an additional layer of type safety alongside static type checkers like MyPy, catching type violations that can only be detected at run time. Currently at version 4.5.1, the library is actively maintained with regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates using the `@typechecked` decorator on both functions and methods. It shows successful type checking and how `typeguard` raises a `TypeError` when type annotations are violated at runtime.

from typeguard import typechecked
from typing import List

@typechecked
def process_data(data: List[int], multiplier: float) -> List[float]:
    """Processes a list of integers, multiplies each by a float, and returns a list of floats."""
    return [item * multiplier for item in data]

@typechecked
class MyCalculator:
    def __init__(self, offset: int):
        self.offset = offset

    def add(self, a: int, b: int) -> int:
        return a + b + self.offset


# --- Successful usage ---
print(f"Processed data: {process_data([1, 2, 3], 2.5)}") # Expected: [2.5, 5.0, 7.5]
calculator = MyCalculator(offset=10)
print(f"Calculator sum: {calculator.add(5, 7)}") # Expected: 22

# --- Intentional type errors (will raise TypeError) ---
try:
    process_data([1, '2', 3], 2.5) # data contains a string instead of int
except TypeError as e:
    print(f"Caught expected error (list item type): {e}")

try:
    process_data([1, 2, 3], '2.5') # multiplier is a string instead of float
except TypeError as e:
    print(f"Caught expected error (argument type): {e}")

try:
    calculator.add(5, 'seven') # b is a string instead of int
except TypeError as e:
    print(f"Caught expected error (method argument type): {e}")

view raw JSON →