Typing Stubs for vobject

0.9.9.20260408 · active · verified Fri Apr 17

The `types-vobject` library provides type annotations (stubs) for the `vobject` library, enabling static type checkers like MyPy or Pyright to analyze code that uses `vobject` for potential type-related errors. It does not provide any runtime functionality itself. As part of the `typeshed` project, it is frequently updated to reflect changes in `vobject` and improvements in typing quality. The current version is 0.9.9.20260408.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `vobject` library with type hints. It shows creating a simple VCALENDAR with an event, adding properties, and serializing it. With `types-vobject` installed, a type checker will provide autocompletion and error checking for `vobject` objects and methods.

import vobject
from vobject.base import Component, VEvent
from typing import cast, List

# Install 'vobject' and 'types-vobject' first:
# pip install vobject types-vobject

def create_simple_calendar() -> Component:
    """Creates a VCALENDAR with a single VEVENT using type hints."""
    # 'calendar' is inferred as vobject.base.Component
    calendar = vobject.newFromBehavior('vcalendar')

    # Add an event component
    event: Component = calendar.add('vevent')

    # Accessing the event via vevent_list is type-safe due to stubs
    event_list: List[VEvent] = calendar.vevent_list
    first_event = event_list[0] # first_event is inferred as VEvent

    first_event.add('dtstart').value = '20231027T100000'
    first_event.add('dtend').value = '20231027T110000'
    first_event.add('summary').value = 'Meeting with client'
    first_event.add('description').value = 'Discuss project roadmap and next steps.'

    return calendar

if __name__ == '__main__':
    my_calendar = create_simple_calendar()
    print(my_calendar.serialize())

    # Accessing properties with type safety
    first_event: VEvent = my_calendar.vevent_list[0]
    print(f"\nEvent Summary: {first_event.summary.value}")
    print(f"Event Description: {first_event.description.value}")
    print(f"Event Start: {first_event.dtstart.value}")

view raw JSON →