Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/876.feature.2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added :attr:`vMonth.ical_value <icalendar.prop.recur.month.vMonth.ical_value>`, which returns the month number as an :class:`int`. For :rfc:`7529` leap months such as ``5L``, the leap flag stays available as :attr:`~icalendar.prop.recur.month.vMonth.leap`. I used Claude Opus 5.5 to help choose the return type and draft some of the tests. @jakkoyang
8 changes: 8 additions & 0 deletions src/icalendar/prop/recur/month.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ def to_ical(self) -> bytes:
"""The ical representation."""
return str(self).encode("utf-8")

@property
def ical_value(self) -> int:
"""Returns the month number as an int, for example, ``5``.

The :rfc:`7529` leap flag is not included. Use :attr:`leap` for it.
"""
return int(self)

@classmethod
def from_ical(cls, ical: str):
return cls(ical)
Expand Down
33 changes: 33 additions & 0 deletions src/icalendar/tests/prop/test_vMonth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Tests for vMonth.ical_value (issue #876)."""

import pytest

from icalendar.prop import vMonth, vRecur


@pytest.mark.parametrize("month", [1, 5, 12])
def test_ical_value_is_the_month_number(month):
"""ical_value returns the month number for a regular month."""
assert vMonth(month).ical_value == month


def test_ical_value_is_a_plain_int():
"""ical_value is a plain int, not a vMonth (isinstance would accept both)."""
assert type(vMonth(5).ical_value) is int


def test_ical_value_of_leap_month_is_the_month_number():
"""A RFC 7529 leap month like 5L returns its month number."""
assert vMonth("5L").ical_value == 5


def test_leap_flag_is_unchanged():
"""The leap flag stays available on vMonth itself."""
assert vMonth("5L").leap is True
assert vMonth(5).leap is False


def test_ical_value_inside_a_recurrence_rule():
"""BYMONTH values parsed from an RRULE expose ical_value."""
month = vRecur.from_ical("FREQ=YEARLY;BYMONTH=5")["BYMONTH"][0]
assert month.ical_value == 5
Loading