Skip to content

Commit eabad84

Browse files
committed
fixing review items
1 parent 5339273 commit eabad84

File tree

4 files changed

+46
-18
lines changed

4 files changed

+46
-18
lines changed

featuremanagement/_defaultfilters.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323

2424
# Time Window Exceptions
2525
TIME_WINDOW_FILTER_INVALID = (
26-
"%s: The %s feature filter is not valid for feature %s. It must specify either %s, $s, or both."
26+
"{}: The {} feature filter is not valid for feature {}. It must specify either {}, {}, or both."
2727
)
2828
TIME_WINDOW_FILTER_INVALID_RECURRENCE = (
29-
"%s: The %s feature filter is not valid for feature %s. It must specify both %s and $s when Recurrence is not None."
29+
"{}: The {} feature filter is not valid for feature {}. It must specify both {} and {} when Recurrence is not None."
3030
)
3131

3232
# Targeting kwargs
@@ -106,6 +106,7 @@ def evaluate(self, context: Mapping[Any, Any], **kwargs: Any) -> bool:
106106

107107
return (start_time is None or start_time <= current_time) and (end_time is None or current_time < end_time)
108108

109+
109110
@FeatureFilter.alias("Microsoft.Targeting")
110111
class TargetingFilter(FeatureFilter):
111112
"""

featuremanagement/_time_window_filter/_models.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# license information.
55
# -------------------------------------------------------------------------
66
from enum import Enum
7+
from typing import Dict, Any
78
from datetime import datetime
8-
from typing import Self
99
from dataclasses import dataclass
1010
from email.utils import parsedate_to_datetime
1111

@@ -18,7 +18,16 @@ class RecurrencePatternType(str, Enum):
1818
DAILY = "Daily"
1919
WEEKLY = "Weekly"
2020

21-
def from_str(value: str) -> Self:
21+
@staticmethod
22+
def from_str(value: str) -> "RecurrencePatternType":
23+
"""
24+
Get the RecurrencePatternType from the string value.
25+
26+
:param value: The string value.
27+
:type value: str
28+
:return: The RecurrencePatternType.
29+
:rtype: RecurrencePatternType
30+
"""
2231
if value == "Daily":
2332
return RecurrencePatternType.DAILY
2433
if value == "Weekly":
@@ -35,7 +44,16 @@ class RecurrenceRangeType(str, Enum):
3544
END_DATE = "EndDate"
3645
NUMBERED = "Numbered"
3746

38-
def from_str(value: str) -> Self:
47+
@staticmethod
48+
def from_str(value: str) -> "RecurrenceRangeType":
49+
"""
50+
Get the RecurrenceRangeType from the string value.
51+
52+
:param value: The string value.
53+
:type value: str
54+
:return: The RecurrenceRangeType.
55+
:rtype: RecurrenceRangeType
56+
"""
3957
if value == "NoEnd":
4058
return RecurrenceRangeType.NO_END
4159
if value == "EndDate":
@@ -44,36 +62,40 @@ def from_str(value: str) -> Self:
4462
return RecurrenceRangeType.NUMBERED
4563
raise ValueError(f"Invalid value: {value}")
4664

47-
class RecurrencePattern:
65+
66+
class RecurrencePattern: # pylint: disable=too-few-public-methods
4867
"""
4968
The recurrence pattern settings.
5069
"""
5170

52-
def __init__(self, pattern_data: dict[str: any]):
71+
def __init__(self, pattern_data: Dict[str, Any]):
5372
self.type = RecurrencePatternType.from_str(pattern_data.get("Type", "Daily"))
5473
self.interval = pattern_data.get("Interval", 1)
5574
self.days_of_week = pattern_data.get("DaysOfWeek", [])
5675
self.first_day_of_week = pattern_data.get("FirstDayOfWeek", 7)
5776

58-
class RecurrenceRange:
77+
78+
class RecurrenceRange: # pylint: disable=too-few-public-methods
5979
"""
6080
The recurrence range settings.
6181
"""
6282

63-
def __init__(self, range_data: dict[str: any]):
83+
def __init__(self, range_data: Dict[str, Any]):
6484
self.type = RecurrenceRangeType.from_str(range_data.get("Type", "NoEnd"))
65-
if range_data.get("EndDate"):
66-
self.end_date = parsedate_to_datetime(range_data.get("EndDate"))
85+
if range_data.get("EndDate") and isinstance(range_data.get("EndDate"), str):
86+
end_date_str = range_data.get("EndDate", "")
87+
self.end_date = parsedate_to_datetime(end_date_str) if end_date_str else None
6788
self.num_of_occurrences = range_data.get("NumberOfOccurrences")
6889

69-
class Recurrence:
90+
91+
class Recurrence: # pylint: disable=too-few-public-methods
7092
"""
7193
The recurrence settings.
7294
"""
7395

74-
def __init__(self, recurrence_data: dict[str: any]):
75-
self.pattern = RecurrencePattern(recurrence_data.get("Pattern"))
76-
self.range = RecurrenceRange(recurrence_data.get("Range"))
96+
def __init__(self, recurrence_data: Dict[str, Any]):
97+
self.pattern = RecurrencePattern(recurrence_data.get("Pattern", {}))
98+
self.range = RecurrenceRange(recurrence_data.get("Range", {}))
7799

78100

79101
@dataclass

featuremanagement/_time_window_filter/_recurrence_evaluator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ def _get_previous_occurrence(settings: TimeWindowFilterSettings, now: datetime)
4141

4242
recurrence_range = settings.recurrence.range
4343
range_type = recurrence_range.type
44+
previous_occurrence = occurrence_info.previous_occurrence
45+
end_date = recurrence_range.end_date
4446
if (
4547
range_type == RecurrenceRangeType.END_DATE
46-
and occurrence_info.previous_occurrence
47-
and occurrence_info.previous_occurrence > recurrence_range.end_date
48+
and previous_occurrence is not None
49+
and end_date is not None
50+
and previous_occurrence > end_date
4851
):
4952
return None
5053
if (
5154
range_type == RecurrenceRangeType.NUMBERED
55+
and recurrence_range.num_of_occurrences is not None
5256
and occurrence_info.num_of_occurrences > recurrence_range.num_of_occurrences
5357
):
5458
return None

featuremanagement/_time_window_filter/_recurrence_validator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def _validate_days_of_week(settings: TimeWindowFilterSettings) -> None:
109109

110110

111111
def _validate_end_date(settings: TimeWindowFilterSettings) -> None:
112-
if settings.recurrence.range.end_date < settings.start:
112+
end_date = settings.recurrence.range.end_date
113+
if end_date and end_date < settings.start:
113114
raise ValueError("The Recurrence.Range.EndDate should be after the Start")
114115

115116

0 commit comments

Comments
 (0)