from datetime import datetime
import re


def parse_iso_8601_timestamp(timestamp: str) -> datetime:
    """Parse a timestamp in ISO 8601 format to a datetime object."""

    def shorten_to_milliseconds(match: re.Match) -> str:
        return f"{match.group(1)}{match.group(2)[:6]}{match.group(3)}"

    return datetime.strptime(
        re.sub(r"([.])(\d*)([Z+-].*)$", shorten_to_milliseconds, timestamp),
        "%Y-%m-%dT%H:%M:%S.%f%z",
    )