Skip to content

Commit f00b9d8

Browse files
author
Michael Brewer
committed
fix(feature-toggles): More MyPy fixes and Docstrings
1 parent 784ff0c commit f00b9d8

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

aws_lambda_powertools/utilities/feature_toggles/configuration_store.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Any, Dict, List, Optional
2+
from typing import Any, Dict, List, Optional, cast
33

44
from . import schema
55
from .exceptions import ConfigurationError
@@ -12,8 +12,10 @@ class ConfigurationStore:
1212
def __init__(self, schema_fetcher: SchemaFetcher):
1313
"""constructor
1414
15-
Args:
16-
schema_fetcher (SchemaFetcher): A schema JSON fetcher, can be AWS AppConfig, Hashicorp Consul etc.
15+
Parameters
16+
----------
17+
schema_fetcher: SchemaFetcher
18+
A schema JSON fetcher, can be AWS AppConfig, Hashicorp Consul etc.
1719
"""
1820
self._logger = logger
1921
self._schema_fetcher = schema_fetcher
@@ -39,12 +41,12 @@ def _match_by_action(self, action: str, condition_value: Any, context_value: Any
3941
def _is_rule_matched(self, feature_name: str, rule: Dict[str, Any], rules_context: Dict[str, Any]) -> bool:
4042
rule_name = rule.get(schema.RULE_NAME_KEY, "")
4143
rule_default_value = rule.get(schema.RULE_DEFAULT_VALUE)
42-
conditions: Dict[str, str] = rule.get(schema.CONDITIONS_KEY)
44+
conditions = cast(List[Dict], rule.get(schema.CONDITIONS_KEY))
4345

4446
for condition in conditions:
45-
context_value = rules_context.get(condition.get(schema.CONDITION_KEY))
47+
context_value = rules_context.get(str(condition.get(schema.CONDITION_KEY)))
4648
if not self._match_by_action(
47-
condition.get(schema.CONDITION_ACTION),
49+
condition.get(schema.CONDITION_ACTION, ""),
4850
condition.get(schema.CONDITION_VALUE),
4951
context_value,
5052
):
@@ -73,7 +75,7 @@ def _handle_rules(
7375
for rule in rules:
7476
rule_default_value = rule.get(schema.RULE_DEFAULT_VALUE)
7577
if self._is_rule_matched(feature_name, rule, rules_context):
76-
return rule_default_value
78+
return bool(rule_default_value)
7779
# no rule matched, return default value of feature
7880
logger.debug(
7981
f"no rule matched, returning default value of feature, feature_default_value={feature_default_value}, "
@@ -95,20 +97,18 @@ def get_configuration(self) -> Dict[str, Any]:
9597
Dict[str, Any]
9698
parsed JSON dictionary
9799
"""
98-
config: Dict[
99-
str, Any
100-
] = (
101-
self._schema_fetcher.get_json_configuration()
102-
) # parse result conf as JSON, keep in cache for self.max_age seconds
100+
# parse result conf as JSON, keep in cache for self.max_age seconds
101+
config = self._schema_fetcher.get_json_configuration()
103102
# validate schema
104103
self._schema_validator.validate_json_schema(config)
105104
return config
106105

107106
def get_feature_toggle(
108107
self, *, feature_name: str, rules_context: Optional[Dict[str, Any]] = None, value_if_missing: bool
109108
) -> bool:
110-
"""get a feature toggle boolean value. Value is calculated according to a set of rules and conditions.
111-
see below for explanation.
109+
"""Get a feature toggle boolean value. Value is calculated according to a set of rules and conditions.
110+
111+
See below for explanation.
112112
113113
Parameters
114114
----------
@@ -123,7 +123,7 @@ def get_feature_toggle(
123123
configuration from appconfig
124124
125125
Returns
126-
----------
126+
------
127127
bool
128128
calculated feature toggle value. several possibilities:
129129
1. if the feature doesn't appear in the schema or there has been an error fetching the
@@ -157,16 +157,16 @@ def get_feature_toggle(
157157
f"no rules found, returning feature default value, feature_name={feature_name}, "
158158
f"default_value={feature_default_value}"
159159
)
160-
return feature_default_value
160+
return bool(feature_default_value)
161161
# look for first rule match
162162
logger.debug(
163163
f"looking for rule match, feature_name={feature_name}, feature_default_value={feature_default_value}"
164164
)
165165
return self._handle_rules(
166166
feature_name=feature_name,
167167
rules_context=rules_context,
168-
feature_default_value=feature_default_value,
169-
rules=rules_list,
168+
feature_default_value=bool(feature_default_value),
169+
rules=cast(List, rules_list),
170170
)
171171

172172
def get_all_enabled_feature_toggles(self, *, rules_context: Optional[Dict[str, Any]] = None) -> List[str]:

0 commit comments

Comments
 (0)