diff --git a/news/2118.bugfix b/news/2118.bugfix new file mode 100644 index 000000000..12c14625c --- /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. I used AI to assist me with this change. @Mubashir78 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")