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/2118.bugfix
Original file line number Diff line number Diff line change
@@ -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
27 changes: 26 additions & 1 deletion src/icalendar/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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`.
Expand Down
63 changes: 63 additions & 0 deletions src/icalendar/tests/test_issue_1840.py
Original file line number Diff line number Diff line change
@@ -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")
Loading