From 32de730b5adb3bcf519588810be51e35afc63c85 Mon Sep 17 00:00:00 2001 From: Shanjidul Hasan Rohan <125924303+shrohan2003@users.noreply.github.com> Date: Thu, 12 Nov 2026 23:53:17 +0600 Subject: [PATCH 1/3] fix: read the resource UID when a nested alarm has its own UID Codex reproduced issue #788, implemented the fix and regression tests, and ran: python -m pytest -q tests/test_caldav_unit.py tests/test_async_davclient.py Assisted-By: OpenAI Codex (GPT-6) --- caldav/datastate.py | 15 ++++++++++++++- changes/788.fixed.md | 1 + tests/test_caldav_unit.py | 12 ++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 changes/788.fixed.md diff --git a/caldav/datastate.py b/caldav/datastate.py index 625afb96..eb973476 100644 --- a/caldav/datastate.py +++ b/caldav/datastate.py @@ -146,7 +146,20 @@ def get_uid(self) -> str | None: if match: uid = re.sub(r"\r?\n[ \t]", "", match.group(1)).strip() # leave backslash escapes to the full parse - if "\\" not in uid: + # Only take the shortcut when this is the first UID and we are + # directly inside the resource component. A VALARM can own a UID too. + headers = re.findall( + r"^(?:BEGIN|END|UID)[:;][^\r\n]*", + self._data[: match.start()], + re.MULTILINE | re.IGNORECASE, + ) + if ( + "\\" not in uid + and len(headers) == 2 + and headers[0].upper() == "BEGIN:VCALENDAR" + and headers[1].upper() + in ("BEGIN:VEVENT", "BEGIN:VTODO", "BEGIN:VJOURNAL", "BEGIN:VFREEBUSY") + ): return uid # Fall back to parsing if regex fails (e.g., UID with parameters) return super().get_uid() diff --git a/changes/788.fixed.md b/changes/788.fixed.md new file mode 100644 index 00000000..b5fe644f --- /dev/null +++ b/changes/788.fixed.md @@ -0,0 +1 @@ +Use the resource component's UID when a nested alarm also has a UID. diff --git a/tests/test_caldav_unit.py b/tests/test_caldav_unit.py index 62f5956b..730a1176 100755 --- a/tests/test_caldav_unit.py +++ b/tests/test_caldav_unit.py @@ -7030,6 +7030,18 @@ def test_cheap_accessor_unfolds(self, fold) -> None: assert RawDataState(ical).get_uid() == self.UID assert Event(data=ical).id == self.UID + @pytest.mark.parametrize("parent_uid", ["UID:event-1", ""]) + def test_nested_alarm_uid_is_not_the_event_id(self, parent_uid) -> None: + ical = ( + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\n" + "DTSTART:20260101T000000Z\r\nBEGIN:VALARM\r\nUID:alarm-1\r\n" + "ACTION:DISPLAY\r\nTRIGGER:-PT5M\r\nDESCRIPTION:Reminder\r\nEND:VALARM\r\n" + + (parent_uid + "\r\n" if parent_uid else "") + + "END:VEVENT\r\nEND:VCALENDAR\r\n" + ) + event = Event(data=ical) + assert event.id == ("event-1" if parent_uid else None) + def test_folded_uid_needs_no_full_parse(self) -> None: from caldav.datastate import RawDataState From 71878ef0e0c3e6afd07b4586e0d1076a7ba5dc55 Mon Sep 17 00:00:00 2001 From: Shanjidul Hasan Rohan <125924303+shrohan2003@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:15:06 +0600 Subject: [PATCH 2/3] fix: keep the raw UID shortcut after a timezone component Track open components before the UID, with parsed fallback for ambiguous structure. Add a VTIMEZONE-first test that disables the full parser. Assisted-By: OpenAI Codex (GPT-6) --- caldav/datastate.py | 40 +++++++++++++++++++++++---------------- tests/test_caldav_unit.py | 12 ++++++++++++ 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/caldav/datastate.py b/caldav/datastate.py index eb973476..7da9ff6d 100644 --- a/caldav/datastate.py +++ b/caldav/datastate.py @@ -145,22 +145,30 @@ def get_uid(self) -> str | None: match = re.search(r"^UID:([^\r\n]*(?:\r?\n[ \t][^\r\n]*)*)", self._data, re.MULTILINE) if match: uid = re.sub(r"\r?\n[ \t]", "", match.group(1)).strip() - # leave backslash escapes to the full parse - # Only take the shortcut when this is the first UID and we are - # directly inside the resource component. A VALARM can own a UID too. - headers = re.findall( - r"^(?:BEGIN|END|UID)[:;][^\r\n]*", - self._data[: match.start()], - re.MULTILINE | re.IGNORECASE, - ) - if ( - "\\" not in uid - and len(headers) == 2 - and headers[0].upper() == "BEGIN:VCALENDAR" - and headers[1].upper() - in ("BEGIN:VEVENT", "BEGIN:VTODO", "BEGIN:VJOURNAL", "BEGIN:VFREEBUSY") - ): - return uid + if "\\" not in uid: + # A preceding VTIMEZONE is common. Only the components still + # open at the UID determine whether it belongs to the resource. + stack = [] + for header in re.finditer( + r"^(BEGIN|END|UID)[:;]([^\r\n]*)", + self._data[: match.start()], + re.IGNORECASE | re.MULTILINE, + ): + token, name = header.group(1).upper(), header.group(2).upper() + if token == "BEGIN": + stack.append(name) + elif token == "END" and stack and stack[-1] == name: + stack.pop() + else: + # Earlier UID with parameters/case differences, or malformed nesting. + break + else: + if ( + len(stack) == 2 + and stack[0] == "VCALENDAR" + and stack[1] in ("VEVENT", "VTODO", "VJOURNAL", "VFREEBUSY") + ): + return uid # Fall back to parsing if regex fails (e.g., UID with parameters) return super().get_uid() diff --git a/tests/test_caldav_unit.py b/tests/test_caldav_unit.py index 730a1176..7246a6d8 100755 --- a/tests/test_caldav_unit.py +++ b/tests/test_caldav_unit.py @@ -7042,6 +7042,18 @@ def test_nested_alarm_uid_is_not_the_event_id(self, parent_uid) -> None: event = Event(data=ical) assert event.id == ("event-1" if parent_uid else None) + def test_timezone_before_resource_uid_needs_no_full_parse(self) -> None: + from caldav.datastate import RawDataState + + zone = ( + "BEGIN:VTIMEZONE\r\nTZID:Example\r\nBEGIN:STANDARD\r\n" + "DTSTART:19700101T000000\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0100\r\n" + "END:STANDARD\r\nEND:VTIMEZONE\r\n" + ) + data = self._ical("\r\n ").replace("BEGIN:VEVENT", zone + "BEGIN:VEVENT") + with mock.patch("caldav.datastate.parse_ical", side_effect=AssertionError): + assert RawDataState(data).get_uid() == self.UID + def test_folded_uid_needs_no_full_parse(self) -> None: from caldav.datastate import RawDataState From 18fe23ddfcea6cb945add65e0edc7dde3cd1c6ea Mon Sep 17 00:00:00 2001 From: Shanjidul Hasan Rohan <125924303+shrohan2003@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:21:25 +0600 Subject: [PATCH 3/3] Preserve IDs for unwrapped resources and cover task reminders Address the follow-up review on #858. Verified both unwrapped event cases failed before the change and all 775 unit tests pass after it. Assisted-By: Codex --- caldav/datastate.py | 6 ++++-- changes/788.fixed.md | 2 +- tests/test_caldav_unit.py | 17 +++++++++++------ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/caldav/datastate.py b/caldav/datastate.py index 7da9ff6d..c5fd5d72 100644 --- a/caldav/datastate.py +++ b/caldav/datastate.py @@ -65,6 +65,8 @@ def get_uid(self) -> str | None: Default implementation parses the data, but subclasses can optimize. """ cal = self.get_icalendar_copy() + if cal.name in ("VEVENT", "VTODO", "VJOURNAL", "VFREEBUSY"): + return str(cal["UID"]) if "UID" in cal else None for comp in cal.subcomponents: if comp.name in ("VEVENT", "VTODO", "VJOURNAL", "VFREEBUSY") and "UID" in comp: return str(comp["UID"]) @@ -148,7 +150,7 @@ def get_uid(self) -> str | None: if "\\" not in uid: # A preceding VTIMEZONE is common. Only the components still # open at the UID determine whether it belongs to the resource. - stack = [] + stack: list[str] = [] for header in re.finditer( r"^(BEGIN|END|UID)[:;]([^\r\n]*)", self._data[: match.start()], @@ -169,7 +171,7 @@ def get_uid(self) -> str | None: and stack[1] in ("VEVENT", "VTODO", "VJOURNAL", "VFREEBUSY") ): return uid - # Fall back to parsing if regex fails (e.g., UID with parameters) + # Parse parameterized or escaped UIDs and resolve ambiguous component nesting. return super().get_uid() def get_component_type(self) -> str | None: diff --git a/changes/788.fixed.md b/changes/788.fixed.md index b5fe644f..70fb0893 100644 --- a/changes/788.fixed.md +++ b/changes/788.fixed.md @@ -1 +1 @@ -Use the resource component's UID when a nested alarm also has a UID. +Events and tasks with reminders now report their own ID even when a reminder has a separate ID. diff --git a/tests/test_caldav_unit.py b/tests/test_caldav_unit.py index 7246a6d8..cd10fcfa 100755 --- a/tests/test_caldav_unit.py +++ b/tests/test_caldav_unit.py @@ -7030,17 +7030,22 @@ def test_cheap_accessor_unfolds(self, fold) -> None: assert RawDataState(ical).get_uid() == self.UID assert Event(data=ical).id == self.UID - @pytest.mark.parametrize("parent_uid", ["UID:event-1", ""]) - def test_nested_alarm_uid_is_not_the_event_id(self, parent_uid) -> None: + @pytest.mark.parametrize(("kind", "resource"), [("VEVENT", Event), ("VTODO", Todo)]) + @pytest.mark.parametrize("parent_uid", ["UID:parent-1", ""]) + def test_nested_alarm_uid_is_not_the_resource_id(self, kind, resource, parent_uid) -> None: ical = ( - "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\n" + f"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:{kind}\r\n" "DTSTART:20260101T000000Z\r\nBEGIN:VALARM\r\nUID:alarm-1\r\n" "ACTION:DISPLAY\r\nTRIGGER:-PT5M\r\nDESCRIPTION:Reminder\r\nEND:VALARM\r\n" + (parent_uid + "\r\n" if parent_uid else "") - + "END:VEVENT\r\nEND:VCALENDAR\r\n" + + f"END:{kind}\r\nEND:VCALENDAR\r\n" ) - event = Event(data=ical) - assert event.id == ("event-1" if parent_uid else None) + assert resource(data=ical).id == ("parent-1" if parent_uid else None) + + @pytest.mark.parametrize("alarm", ["", "BEGIN:VALARM\r\nUID:alarm-1\r\nEND:VALARM\r\n"]) + def test_unwrapped_event_uid(self, alarm) -> None: + data = f"BEGIN:VEVENT\r\n{alarm}UID:event-1\r\nEND:VEVENT\r\n" + assert Event(data=data).id == "event-1" def test_timezone_before_resource_uid_needs_no_full_parse(self) -> None: from caldav.datastate import RawDataState