Typing Stubs for icalendar
The `types-icalendar` package provides type annotations (stubs) for the `icalendar` library, which is an RFC 5545 compliant Python parser and generator for iCalendar files. This package is part of the `typeshed` project and specifically targets `icalendar` versions prior to 7.0.0. From `icalendar` version 7.0.0 onwards, type annotations are included directly within the `icalendar` package, making `types-icalendar` redundant for newer versions. It currently requires Python >=3.10 and is updated regularly based on typeshed contributions.
Warnings
- breaking If using `icalendar` version 7.0.0 or newer, `types-icalendar` should be uninstalled. `icalendar` now includes its own type annotations, which can cause conflicts or redundancy with `types-icalendar`.
- breaking The `icalendar` runtime library, for which `types-icalendar` provides stubs, dropped support for Python 3.8 and 3.9 in `icalendar` version 7.0.3. The `types-icalendar` package itself requires Python >=3.10, aligning with the current `icalendar` branch it supports (e.g., `icalendar==6.3.2`).
- gotcha In `icalendar` versions prior to 7.0.3, the `Component.decoded()` method for text properties returned bytes. From version 7.0.3 onwards, it returns a string. Code relying on the byte return type will break if `icalendar` is updated.
- gotcha According to RFC 5545, `Calendar` objects in `icalendar` require `PRODID` and `VERSION` properties. Forgetting to add these can lead to invalid iCalendar files, even if the code type-checks. Newer `icalendar` versions (e.g., 7.0.0+) may provide enhanced methods for automatic `PRODID` generation.
- gotcha Incorrect handling of timezones with `datetime` objects can lead to silently corrupted or invalid iCalendar data. `icalendar` supports multiple timezone implementations (e.g., `zoneinfo`, `dateutil.tz`, `pytz`). Using naive `datetime` objects for `dtstart` or `dtend` without proper timezone information is a common mistake.
Install
-
pip install types-icalendar -
pip install icalendar==6.3.2
Imports
- Calendar
from icalendar import Calendar
- Event
from icalendar import Event
- vDatetime
from icalendar import vDatetime
Quickstart
from icalendar import Calendar, Event, vText
from datetime import datetime
import zoneinfo
# Create a new Calendar object
cal = Calendar()
cal.add('prodid', '-//My Company//My Calendar App//EN')
cal.add('version', '2.0')
cal.add('summary', 'My Awesome Calendar')
# Create an Event
event = Event()
event.add('summary', 'Meeting with Client X')
event.add('dtstart', datetime(2026, 4, 15, 10, 0, 0, tzinfo=zoneinfo.ZoneInfo('America/New_York')))
event.add('dtend', datetime(2026, 4, 15, 11, 0, 0, tzinfo=zoneinfo.ZoneInfo('America/New_York')))
event.add('description', 'Discuss Q2 strategy and project roadmap.')
event.add('location', vText('Conference Room A'))
event['uid'] = '20260415T100000-abcd-1234@example.com'
# Add the event to the calendar
cal.add_component(event)
# Serialize the calendar to iCalendar format
ical_string = cal.to_ical().decode('utf-8')
print(ical_string)
# Example of parsing an iCalendar string
# parsed_cal = Calendar.from_ical(ical_string)
# for component in parsed_cal.walk():
# if component.name == 'VEVENT':
# print(component.get('summary'))