Typing Stubs for pytz

2026.1.1.20260304 · active · verified Sat Mar 28

types-pytz provides static type checking annotations (typing stubs) for the `pytz` library. It enables type checkers like MyPy and Pyright to analyze code that uses `pytz` for timezone operations, helping to catch type-related errors before runtime. As of its latest version `2026.1.1.20260304`, it aims to provide accurate annotations for `pytz==2026.1.post1` and is part of the actively maintained Typeshed project, which releases updates frequently.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic usage of `pytz` for creating and converting timezone-aware `datetime` objects. Installing `types-pytz` enables static type checkers to verify the types in such operations, enhancing code reliability.

from datetime import datetime
from pytz import timezone, utc

# Create a naive datetime object
naive_dt = datetime(2024, 7, 21, 10, 30, 0)
print(f"Naive Datetime: {naive_dt}")

# Get a timezone object
eastern = timezone('America/New_York')

# Localize the naive datetime (make it timezone-aware)
aware_dt_eastern = eastern.localize(naive_dt)
print(f"Aware Datetime (Eastern): {aware_dt_eastern}")

# Convert to UTC
aware_dt_utc = aware_dt_eastern.astimezone(utc)
print(f"Aware Datetime (UTC): {aware_dt_utc}")

# Get current time in a specific timezone
current_paris_time = datetime.now(timezone('Europe/Paris'))
print(f"Current Paris Time: {current_paris_time}")

def get_current_utc() -> datetime:
    """Returns the current UTC time with timezone awareness."""
    return datetime.now(utc)

# Demonstrate type checking with a function call
current_utc = get_current_utc()
print(f"Current UTC (from typed function): {current_utc}")

view raw JSON →