Skip to content

Commit

Permalink
py38 compatibility, ruff adjustment, better error messages tested
Browse files Browse the repository at this point in the history
  • Loading branch information
niccokunzmann committed Oct 11, 2024
1 parent c7d2335 commit 35b15be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ ignore = [
"PERF401", # Use a list comprehension to create a transformed list
"ARG002", # Unused method argument: ...
"ARG001", # Unused function argument: ...
"UP007", # Optional -> X | None remove when migrated to py39+
]
extend-safe-fixes = [
"PT006", # Wrong type passed to first argument of @pytest.mark.parametrize; expected {expected_string}
Expand Down
15 changes: 7 additions & 8 deletions src/icalendar/cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import os
from datetime import date, datetime, timedelta
from typing import List, Tuple
from typing import List, Optional, Tuple

import dateutil.rrule
import dateutil.tz
Expand Down Expand Up @@ -511,7 +511,6 @@ def p_get(self : Component):
if not isinstance(value, value_type):
raise InvalidCalendar(f"{prop} must be either a date or a datetime, not {value}.")
return value


def p_set(self:Component, value) -> None:
if value is None:
Expand All @@ -524,13 +523,13 @@ def p_set(self:Component, value) -> None:
for other_prop in self.exclusive:
if other_prop != prop:
self.pop(other_prop, None)
p_set.__annotations__["value"] = p_get.__annotations__["return"] = type_def | None
p_set.__annotations__["value"] = p_get.__annotations__["return"] = Optional[type_def]

def p_del(self:Component):
self.pop(prop)

p_doc = f"""The {prop} property.
{doc}
Accepted values: {', '.join(t.__name__ for t in value_type)}.
Expand Down Expand Up @@ -595,7 +594,7 @@ def _get_start_end_duration(self):
return start, end, duration

@property
def DURATION(self) -> timedelta | None: # noqa: N802
def DURATION(self) -> Optional[timedelta]: # noqa: N802
"""The DURATION of the component.
The "DTSTART" property for a "VEVENT" specifies the inclusive start of the event.
Expand All @@ -617,7 +616,7 @@ def DURATION(self) -> timedelta | None: # noqa: N802
return None

@DURATION.setter
def DURATION(self, value: timedelta | None): # noqa: N802
def DURATION(self, value: Optional[timedelta]): # noqa: N802
if value is None:
self.pop("duration", None)
return
Expand Down Expand Up @@ -663,7 +662,7 @@ def start(self) -> date | datetime:
return start

@start.setter
def start(self, start: date | datetime| None):
def start(self, start: Optional[date | datetime]):
"""Set the start."""
self.DTSTART = start

Expand Down
12 changes: 8 additions & 4 deletions src/icalendar/tests/test_issue_662_component_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from datetime import date, datetime, timedelta

import pytest
from zoneinfo import ZoneInfo

try:
from zoneinfo import ZoneInfo
except ImportError:
from backports.zoneinfo import ZoneInfo # type: ignore

from icalendar import (
Event,
Expand Down Expand Up @@ -201,9 +205,9 @@ def test_start_and_duration(event, dtstart, duration):
@pytest.mark.parametrize(
("invalid_event", "message"),
[
(invalid_event_end_1, "DTSTART and DTEND must have the same type."),
(invalid_event_end_2, "DTSTART and DTEND must have the same type."),
(invalid_event_end_3, "DTEND and DURATION cannot be there at the same time."),
(invalid_event_end_1, "DTSTART and DTEND must be of the same type, either date or datetime."),
(invalid_event_end_2, "DTSTART and DTEND must be of the same type, either date or datetime."),
(invalid_event_end_3, "Only one of DTEND and DURATION may be in a VEVENT, not both."),
(invalid_event_end_4, "When DTSTART is a date, DURATION must be of days or weeks."),
]
)
Expand Down

0 comments on commit 35b15be

Please sign in to comment.