From 6d7414d9e16665e4ea2e5da5a41dca10158ddbc7 Mon Sep 17 00:00:00 2001 From: Mubashir78 Date: Sun, 15 Nov 2026 06:16:05 +0500 Subject: [PATCH 1/2] fix: make StrEnum lookups case-insensitive per RFC 5545 Issue #1840. Parameter values defined as enums (PARTSTAT, STATUS, TRANSP, VALUE, etc.) are not enclosed in double quotes, so RFC 5545 section 3.1 makes them case-insensitive. The StrEnum.get() helper already handles case-insensitive lookup with a default, but calling the enum directly still fails: PARTSTAT('needs-action') raises ValueError on main while PARTSTAT.NEEDS_ACTION works. Add StrEnum._missing_ to look up members by their casefolded value. The defined values keep their canonical spelling; only the lookup is relaxed. This also makes get() resolve any case variant through the regular constructor path. Tests cover case-insensitive lookups, exact lookups, unknown values, and str() output for every StrEnum subclass. AI assistance disclosure: This commit was drafted with AI assistance (GLM-5.3-Flash, Z.ai). I used it to compare RFC 5545 section 3.1 with the enums module, to draft the _missing_ implementation and the test file in this commit, and to run and interpret the local test results. I reviewed all of it before committing. --- news/2118.bugfix | 1 + src/icalendar/enums.py | 27 ++++++++++- src/icalendar/tests/test_issue_1840.py | 63 ++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 news/2118.bugfix create mode 100644 src/icalendar/tests/test_issue_1840.py diff --git a/news/2118.bugfix b/news/2118.bugfix new file mode 100644 index 000000000..892da843f --- /dev/null +++ b/news/2118.bugfix @@ -0,0 +1 @@ +Parameter values defined as enums (such as PARTSTAT, STATUS and TRANSP) can now be looked up case-insensitively when calling the enum directly, for example ``PARTSTAT("needs-action")`` returns ``PARTSTAT.NEEDS_ACTION``. This follows RFC 5545 section 3.1, which treats these unquoted values as case-insensitive. The canonical spelling of the values is unchanged. diff --git a/src/icalendar/enums.py b/src/icalendar/enums.py index 68c4663ca..4302bf2c0 100644 --- a/src/icalendar/enums.py +++ b/src/icalendar/enums.py @@ -20,7 +20,16 @@ def __reduce_ex__(self, _p): class StrEnum(str, Enum): - """Enum for strings.""" + """Enum for strings. + + Values are looked up ignoring case because these values are not + enclosed in double quotes and are therefore case-insensitive as of + :rfc:`5545`, section 3.1:: + + >>> from icalendar.enums import PARTSTAT + >>> PARTSTAT("needs-action") is PARTSTAT.NEEDS_ACTION + True + """ _value_: str @@ -77,6 +86,22 @@ def get(cls, value: str, default: T | None = None) -> Self | T | None: except ValueError: return default + @classmethod + def _missing_(cls, value): + """Look up a member by its value, ignoring case. + + :class:`enum.Enum` calls this whenever a lookup by value finds no + exact match. Only the lookup is relaxed here, the values keep the + spelling that they are defined with. + """ + if not isinstance(value, str): + return None + value = value.casefold() + for member in cls: + if member.value.casefold() == value: + return member + return None + class PARTSTAT(StrEnum): """Enum for PARTSTAT from :rfc:`5545`. diff --git a/src/icalendar/tests/test_issue_1840.py b/src/icalendar/tests/test_issue_1840.py new file mode 100644 index 000000000..7065a5b4a --- /dev/null +++ b/src/icalendar/tests/test_issue_1840.py @@ -0,0 +1,63 @@ +"""Test that the enums accept case-insensitive values. + +The values of the enums in :mod:`icalendar.enums` are not enclosed in +double quotes, so they are case-insensitive as of :rfc:`5545`, section 3.1: +"All names of properties, property parameters, enumerated property values +and property parameter values are case-insensitive." + +See https://github.com/collective/icalendar/issues/1840 +""" + +import pytest + +from icalendar import enums + +CASE_INSENSITIVE_ENUMS = [ + enums.BUSYTYPE, + enums.CLASS, + enums.CUTYPE, + enums.FBTYPE, + enums.PARTSTAT, + enums.RANGE, + enums.RELATED, + enums.RELTYPE, + enums.ROLE, + enums.STATUS, + enums.TRANSP, + enums.VALUE, +] + + +def test_partstat_from_the_issue(): + """The example of the issue works.""" + assert ( + enums.PARTSTAT("needs-action") + == enums.PARTSTAT("NEEDS-ACTION") + == enums.PARTSTAT.NEEDS_ACTION + ) + + +@pytest.mark.parametrize("enum", CASE_INSENSITIVE_ENUMS, ids=lambda enum: enum.__name__) +def test_every_value_can_be_looked_up_in_any_case(enum): + """Every value can be looked up in lower, upper and mixed case.""" + for member in enum: + assert enum(member.value.lower()) is member + assert enum(member.value.upper()) is member + assert enum(member.value.title()) is member + + +@pytest.mark.parametrize("enum", CASE_INSENSITIVE_ENUMS, ids=lambda enum: enum.__name__) +def test_values_keep_their_case(enum): + """Looking a value up in another case returns the canonical value.""" + for member in enum: + assert enum(member.value.lower()).value == member.value + assert enum(member.value.title()).value == member.value + + +@pytest.mark.parametrize("enum", CASE_INSENSITIVE_ENUMS, ids=lambda enum: enum.__name__) +def test_unknown_values_still_fail(enum): + """Values that are unknown, regardless of their case, are an error.""" + with pytest.raises(ValueError): + enum("this-is-not-a-known-value") + with pytest.raises(ValueError): + enum("THIS-IS-NOT-A-KNOWN-VALUE") From a41d6fcb05ddabc48e470fdf5f1e273eb05f8b5f Mon Sep 17 00:00:00 2001 From: Spacey <85053722+Mubashir78@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:15:38 +0500 Subject: [PATCH 2/2] docs: disclose AI use and add attribution to the 2118 change log entry The change log checker needs the AI disclosure and an @github_username attribution in the entry itself, not only in the commit messages. AI assistance disclosure: drafted with DeepSeek V4.1 Flash, used to add the disclosure and attribution to news/2118.bugfix. I reviewed the result. --- news/2118.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/2118.bugfix b/news/2118.bugfix index 892da843f..12c14625c 100644 --- a/news/2118.bugfix +++ b/news/2118.bugfix @@ -1 +1 @@ -Parameter values defined as enums (such as PARTSTAT, STATUS and TRANSP) can now be looked up case-insensitively when calling the enum directly, for example ``PARTSTAT("needs-action")`` returns ``PARTSTAT.NEEDS_ACTION``. This follows RFC 5545 section 3.1, which treats these unquoted values as case-insensitive. The canonical spelling of the values is unchanged. +Parameter values defined as enums (such as PARTSTAT, STATUS and TRANSP) can now be looked up case-insensitively when calling the enum directly, for example ``PARTSTAT("needs-action")`` returns ``PARTSTAT.NEEDS_ACTION``. This follows RFC 5545 section 3.1, which treats these unquoted values as case-insensitive. The canonical spelling of the values is unchanged. I used AI to assist me with this change. @Mubashir78