Typing stubs for python-dateutil
types-python-dateutil provides static typing stubs for the popular `python-dateutil` library. It allows type checkers like Mypy or Pyright to verify code that uses `python-dateutil`, catching potential type-related errors before runtime. This package is part of the typeshed project and aims to provide accurate annotations for `python-dateutil==2.9.*`. Updates are released frequently, often daily, directly from the typeshed repository. The current version is 2.9.0.20260323.
Warnings
- gotcha The `types-python-dateutil` package provides stubs for a specific major/minor version range of `python-dateutil` (e.g., `2.9.*`). Using a significantly different version of the runtime `python-dateutil` library may lead to inaccurate type checking results or errors, as API changes in `python-dateutil` might not be reflected in the stubs for a different version range.
- gotcha Type checkers (like Mypy) might sometimes fail to pick up installed stub packages, especially in complex environments or when using tools like `pre-commit` hooks. This can result in 'Missing stubs' errors even when `types-python-dateutil` is installed.
- gotcha While typeshed aims to release stub packages frequently (up to once a day), there can be a slight delay between a new `python-dateutil` release and the corresponding update to `types-python-dateutil`. During this period, very new features or breaking changes in the latest `python-dateutil` might not yet have accurate type annotations.
- breaking Due to the nature of type stubs, any version bump of `types-python-dateutil` can introduce changes that might cause your code to fail type checking, even if the runtime behavior of `python-dateutil` has not changed. This often happens when stricter or more accurate types are introduced.
- gotcha `types-python-dateutil` requires Python >=3.10. Using it with older Python versions might lead to installation issues or type checking inconsistencies if the underlying `python-dateutil` library behaves differently or relies on features not available in older Python versions.
Install
-
pip install types-python-dateutil
Imports
- parse
from dateutil.parser import parse
- relativedelta
from dateutil.relativedelta import relativedelta
- tz
from dateutil import tz
- UTC
from dateutil.tz import UTC
Quickstart
from datetime import datetime
from dateutil.parser import parse
from dateutil.relativedelta import relativedelta, MO
# Enable type checking in your editor or via a tool like Mypy
# E.g., run `mypy your_script.py`
def process_date_string(date_str: str) -> datetime:
"""Parses a date string and returns a datetime object."""
return parse(date_str)
def calculate_future_date(start_date: datetime) -> datetime:
"""Calculates a future date using relativedelta."""
# Add 1 month and move to the 1st Monday
return start_date + relativedelta(months=1, weekday=MO(1))
current_time_str: str = "2024-03-28 10:30:00 UTC"
parsed_dt: datetime = process_date_string(current_time_str)
print(f"Parsed datetime: {parsed_dt}")
future_dt: datetime = calculate_future_date(parsed_dt)
print(f"Future datetime: {future_dt}")
# Example where type checker would catch an error (uncomment to test):
# def expects_string(s: str):
# print(s)
# expects_string(123) # Mypy would report: Argument 's' to 'expects_string' has incompatible type "int"; expected "str"