|
4 | 4 | # license information. |
5 | 5 | # ------------------------------------------------------------------------- |
6 | 6 | import inspect |
7 | | -import logging |
8 | | -from typing import cast, overload, Mapping, Dict, Any, Optional, List |
| 7 | +from typing import cast, overload, Any, Optional, Dict, Mapping, List |
9 | 8 | from ._defaultfilters import TimeWindowFilter, TargetingFilter |
10 | 9 | from ._featurefilters import FeatureFilter |
11 | | -from .._models import EvaluationEvent, TargetingContext, Variant |
| 10 | +from .._models import EvaluationEvent, Variant, TargetingContext |
12 | 11 | from .._featuremanagerbase import ( |
13 | | - _get_feature_flag, |
14 | 12 | FeatureManagerBase, |
15 | 13 | PROVIDED_FEATURE_FILTERS, |
16 | | - FEATURE_MANAGEMENT_KEY, |
17 | 14 | REQUIREMENT_TYPE_ALL, |
18 | 15 | FEATURE_FILTER_NAME, |
19 | 16 | ) |
@@ -63,7 +60,12 @@ async def is_enabled(self, feature_flag_id: str, *args: Any, **kwargs: Any) -> b |
63 | 60 | targeting_context = self._build_targeting_context(args) |
64 | 61 |
|
65 | 62 | result = await self._check_feature(feature_flag_id, targeting_context, **kwargs) |
66 | | - if self._on_feature_evaluated and result.feature and result.feature.telemetry.enabled: |
| 63 | + if ( |
| 64 | + self._on_feature_evaluated |
| 65 | + and result.feature |
| 66 | + and result.feature.telemetry.enabled |
| 67 | + and callable(self._on_feature_evaluated) |
| 68 | + ): |
67 | 69 | result.user = targeting_context.user_id |
68 | 70 | if inspect.iscoroutinefunction(self._on_feature_evaluated): |
69 | 71 | await self._on_feature_evaluated(result) |
@@ -94,7 +96,12 @@ async def get_variant(self, feature_flag_id: str, *args: Any, **kwargs: Any) -> |
94 | 96 | targeting_context = self._build_targeting_context(args) |
95 | 97 |
|
96 | 98 | result = await self._check_feature(feature_flag_id, targeting_context, **kwargs) |
97 | | - if self._on_feature_evaluated and result.feature and result.feature.telemetry.enabled: |
| 99 | + if ( |
| 100 | + self._on_feature_evaluated |
| 101 | + and result.feature |
| 102 | + and result.feature.telemetry.enabled |
| 103 | + and callable(self._on_feature_evaluated) |
| 104 | + ): |
98 | 105 | result.user = targeting_context.user_id |
99 | 106 | if inspect.iscoroutinefunction(self._on_feature_evaluated): |
100 | 107 | await self._on_feature_evaluated(result) |
@@ -141,35 +148,12 @@ async def _check_feature( |
141 | 148 |
|
142 | 149 | :param str feature_flag_id: Name of the feature flag. |
143 | 150 | :param TargetingContext targeting_context: Targeting context. |
144 | | - :return: True if the feature flag is enabled for the given context. |
145 | | - :rtype: bool |
| 151 | + :return: EvaluationEvent for the given context. |
| 152 | + :rtype: EvaluationEvent |
146 | 153 | """ |
147 | | - if self._copy is not self._configuration.get(FEATURE_MANAGEMENT_KEY): |
148 | | - self._cache = {} |
149 | | - self._copy = self._configuration.get(FEATURE_MANAGEMENT_KEY) |
150 | | - |
151 | | - if not self._cache.get(feature_flag_id): |
152 | | - feature_flag = _get_feature_flag(self._configuration, feature_flag_id) |
153 | | - self._cache[feature_flag_id] = feature_flag |
154 | | - else: |
155 | | - feature_flag = self._cache.get(feature_flag_id) |
156 | | - |
157 | | - evaluation_event = EvaluationEvent(feature_flag) |
158 | | - if not feature_flag: |
159 | | - logging.warning("Feature flag %s not found", feature_flag_id) |
160 | | - # Unknown feature flags are disabled by default |
161 | | - return evaluation_event |
162 | | - |
163 | | - if not feature_flag.enabled: |
164 | | - # Feature flags that are disabled are always disabled |
165 | | - FeatureManager._check_default_disabled_variant(evaluation_event) |
166 | | - if feature_flag.allocation: |
167 | | - variant_name = feature_flag.allocation.default_when_disabled |
168 | | - evaluation_event.variant = self._variant_name_to_variant(feature_flag, variant_name) |
169 | | - evaluation_event.feature = feature_flag |
| 154 | + evaluation_event, done = FeatureManager._check_feature_base(self, feature_flag_id) |
170 | 155 |
|
171 | | - # If a feature flag is disabled and override can't enable it |
172 | | - evaluation_event.enabled = False |
| 156 | + if done: |
173 | 157 | return evaluation_event |
174 | 158 |
|
175 | 159 | await self._check_feature_filters(evaluation_event, targeting_context, **kwargs) |
|
0 commit comments