From e069891053944bef5816caa2a9ca395c3b403724 Mon Sep 17 00:00:00 2001 From: Matt Metcalf Date: Fri, 10 May 2024 15:01:41 -0700 Subject: [PATCH 1/7] Removing non-beta.1 features --- README.md | 158 ------------ featuremanagement/__init__.py | 3 +- featuremanagement/_featuremanager.py | 112 +------- featuremanagement/_models/_allocation.py | 193 -------------- featuremanagement/_models/_constants.py | 16 -- featuremanagement/_models/_feature_flag.py | 32 --- featuremanagement/_models/_variant.py | 36 --- .../_models/_variant_reference.py | 79 ------ featuremanagement/aio/_featuremanager.py | 112 +------- samples/feature_variant_sample.py | 25 -- tests/test_feature_variants.py | 243 ----------------- tests/test_feature_variants_async.py | 244 ------------------ 12 files changed, 3 insertions(+), 1250 deletions(-) delete mode 100644 featuremanagement/_models/_allocation.py delete mode 100644 featuremanagement/_models/_variant.py delete mode 100644 featuremanagement/_models/_variant_reference.py delete mode 100644 samples/feature_variant_sample.py delete mode 100644 tests/test_feature_variants.py delete mode 100644 tests/test_feature_variants_async.py diff --git a/README.md b/README.md index 3d7d818..cce7779 100644 --- a/README.md +++ b/README.md @@ -268,164 +268,6 @@ class MyCustomFilter(FeatureFilter): ... ``` -### Feature Variants - -When new features are added to an application, there may come a time when a feature has multiple different proposed design options. A common solution for deciding on a design is some form of A/B testing, which involves providing a different version of the feature to different segments of the user base and choosing a version based on user interaction. In this library, this functionality is enabled by representing different configurations of a feature with variants. - -Variants enable a feature flag to become more than a simple on/off flag. A variant represents a value of a feature flag that can be a string, a number, a boolean, or even a configuration object. A feature flag that declares variants should define under what circumstances each variant should be used, which is covered in greater detail in the [Allocating Variants](#allocating-variants) section. - -```python -class Variant(): - - @property - def name(self): - - @property - def configuration(self): -``` - -#### Getting Variants - -For each feature, a variant can be retrieved using `FeatureManager`'s `get_variant` method. The method returns a `Variant` object that contains the name and configuration of the variant. Once a variant is retrieved, the configuration of a variant can be used directly as JSON from the variant's `configuration` property. - -```python -feature_manager = FeatureManager(feature_flags) - -variant = feature_manager.get_variant("FeatureU") - -my_configuration = variant.configuration - -variant = feature_manager.get_variant("FeatureV") - -sub_configuration = variant.configuration["json_key"] -``` - -#### Defining Variants - -Each variant has two properties: a name and a configuration. The name is used to refer to a specific variant, and the configuration is the value of that variant. The configuration can be set using either the `configuration_reference` or `configuration_value` properties. `configuration_reference` is a string that references a configuration, this configuration is a key inside of the configuration object passed into `FeatureManager`. `configuration_value` is an inline configuration that can be a string, number, boolean, or json object. If both are specified, `configuration_value` is used. If neither are specified, the returned variant's `configuration` property will be `None`. - -A list of all possible variants is defined for each feature under the Variants property. - -```json -{ - "feature_management": { - "feature_flags": [ - { - "id": "FeatureU", - "variants": [ - { - "name": "VariantA", - "configuration_reference": "config1" - }, - { - "name": "VariantB", - "configuration_value": { - "name": "value" - } - } - ] - } - ] - } -} -``` - -#### Allocating Variants - -The process of allocating a feature's variants is determined by the `allocation` property of the feature. - -```json -"allocation": { - "default_when_enabled": "Small", - "default_when_disabled": "Small", - "user": [ - { - "variant": "Big", - "users": [ - "Marsha" - ] - } - ], - "group": [ - { - "variant": "Big", - "groups": [ - "Ring1" - ] - } - ], - "percentile": [ - { - "variant": "Big", - "from": 0, - "to": 10 - } - ], - "seed": "13973240" -}, -"variants": [ - { - "name": "Big", - "configuration_reference": "ShoppingCart:Big" - }, - { - "name": "Small", - "configuration_value": "300px" - } -] -``` - -The `allocation` setting of a feature flag has the following properties: - -| Property | Description | -| ---------------- | ---------------- | -| `default_when_disabled` | Specifies which variant should be used when a variant is requested while the feature is considered disabled. | -| `default_when_enabled` | Specifies which variant should be used when a variant is requested while the feature is considered enabled and no other variant was assigned to the user. | -| `user` | Specifies a variant and a list of users to whom that variant should be assigned. | -| `group` | Specifies a variant and a list of groups the current user has to be in for that variant to be assigned. | -| `percentile` | Specifies a variant and a percentage range the user's calculated percentage has to fit into for that variant to be assigned. | -| `seed` | The value which percentage calculations for `percentile` are based on. The percentage calculation for a specific user will be the same across all features if the same `seed` value is used. If no `seed` is specified, then a default seed is created based on the feature name. | - -In the above example, if the feature is not enabled, the feature manager will assign the variant marked as `default_when_disabled` to the current user, which is `Small` in this case. - -If the feature is enabled, the feature manager will check the `user`, `group`, and `percentile` allocations in that order to assign a variant. For this particular example, if the user being evaluated is named `Marsha`, in the group named `Ring1`, or the user happens to fall between the 0 and 10th percentile, then the specified variant is assigned to the user. In this case, all of these would return the `Big` variant. If none of these allocations match, the user is assigned the `default_when_enabled` variant, which is `Small`. - -Allocation logic is similar to the [Microsoft.Targeting](#microsoft-targeting) feature filter, but there are some parameters that are present in targeting that aren't in allocation, and vice versa. The outcomes of targeting and allocation are not related. - -### Overriding Enabled State with a Variant - -You can use variants to override the enabled state of a feature flag. This gives variants an opportunity to extend the evaluation of a feature flag. If a caller is checking whether a flag that has variants is enabled, the feature manager will check if the variant assigned to the current user is set up to override the result. This is done using the optional variant property `status_override`. By default, this property is set to `None`, which means the variant doesn't affect whether the flag is considered enabled or disabled. Setting `status_override` to `Enabled` allows the variant, when chosen, to override a flag to be enabled. Setting `status_override` to `Disabled` provides the opposite functionality, therefore disabling the flag when the variant is chosen. - -If you are using a feature flag with binary variants, the `status_override` property can be very helpful. It allows you to continue using `is_enabled` in your application, all while benefiting from the new features that come with variants, such as percentile allocation and seed. - -```json -"allocation": { - "percentile": [{ - "variant": "On", - "from": 10, - "to": 20 - }], - "default_when_enabled": "Off", - "seed": "Enhanced-Feature-Group" -}, -"variants": [ - { - "name": "On" - }, - { - "name": "Off", - "status_override": "Disabled" - } -], -"enabled_for": [ - { - "name": "AlwaysOn" - } -] -``` - -In the above example, the feature is enabled by the `AlwaysOn` filter. If the current user is in the calculated percentile range of 10 to 20, then the `On` variant is returned. Otherwise, the `Off` variant is returned and because `status_override` is equal to `Disabled`, the feature will now be considered disabled. - ## Contributing This project welcomes contributions and suggestions. Most contributions require you to agree to a diff --git a/featuremanagement/__init__.py b/featuremanagement/__init__.py index 31dafd3..ec055b5 100644 --- a/featuremanagement/__init__.py +++ b/featuremanagement/__init__.py @@ -6,9 +6,8 @@ from ._featuremanager import FeatureManager from ._featurefilters import FeatureFilter from ._defaultfilters import TimeWindowFilter, TargetingFilter -from ._models._variant import Variant from ._version import VERSION __version__ = VERSION -__all__ = ["FeatureManager", "TimeWindowFilter", "TargetingFilter", "FeatureFilter", "Variant"] +__all__ = ["FeatureManager", "TimeWindowFilter", "TargetingFilter", "FeatureFilter"] diff --git a/featuremanagement/_featuremanager.py b/featuremanagement/_featuremanager.py index 2f82f98..d8561d6 100644 --- a/featuremanagement/_featuremanager.py +++ b/featuremanagement/_featuremanager.py @@ -4,12 +4,10 @@ # license information. # ------------------------------------------------------------------------- import logging -import hashlib from collections.abc import Mapping from ._defaultfilters import TimeWindowFilter, TargetingFilter from ._featurefilters import FeatureFilter from ._models._feature_flag import FeatureFlag -from ._models._variant import Variant FEATURE_MANAGEMENT_KEY = "feature_management" @@ -80,80 +78,6 @@ def __init__(self, configuration, **kwargs): raise ValueError("Custom filter must be a subclass of FeatureFilter") self._filters[feature_filter.name] = feature_filter - @staticmethod - def _check_default_disabled_variant(feature_flag): - if not feature_flag.allocation: - return False - return FeatureManager._check_variant_override( - feature_flag.variants, feature_flag.allocation.default_when_disabled, False - ) - - @staticmethod - def _check_default_enabled_variant(feature_flag): - if not feature_flag.allocation: - return True - return FeatureManager._check_variant_override( - feature_flag.variants, feature_flag.allocation.default_when_enabled, True - ) - - @staticmethod - def _check_variant_override(variants, default_variant_name, status): - if not variants or not default_variant_name: - return status - for variant in variants: - if variant.name == default_variant_name: - if variant.status_override == "Enabled": - return True - if variant.status_override == "Disabled": - return False - return status - - @staticmethod - def _is_targeted(context_id): - """Determine if the user is targeted for the given context""" - hashed_context_id = hashlib.sha256(context_id.encode()).digest() - context_marker = int.from_bytes(hashed_context_id[:4], byteorder="little", signed=False) - - return (context_marker / (2**32 - 1)) * 100 - - def _assign_variant(self, feature_flag, **kwargs): - if not feature_flag.variants or not feature_flag.allocation: - return None - if feature_flag.allocation.user: - user = kwargs.get("user") - if user: - for user_allocation in feature_flag.allocation.user: - if user in user_allocation.users: - return user_allocation.variant - if feature_flag.allocation.group: - groups = kwargs.get("groups") - if groups: - for group_allocation in feature_flag.allocation.group: - for group in groups: - if group in group_allocation.groups: - return group_allocation.variant - if feature_flag.allocation.percentile: - user = kwargs.get("user", "") - context_id = user + "\n" + feature_flag.allocation.seed - box = self._is_targeted(context_id) - for percentile_allocation in feature_flag.allocation.percentile: - if box == 100 and percentile_allocation.percentile_to == 100: - return percentile_allocation.variant - if percentile_allocation.percentile_from <= box < percentile_allocation.percentile_to: - return percentile_allocation.variant - return None - - def _variant_name_to_variant(self, feature_flag, variant_name): - if not feature_flag.variants: - return None - for variant_reference in feature_flag.variants: - if variant_reference.name == variant_name: - configuration = variant_reference.configuration_value - if not configuration: - configuration = self._configuration.get(variant_reference.configuration_reference) - return Variant(variant_reference.name, configuration) - return None - def is_enabled(self, feature_flag_id, **kwargs): """ Determine if the feature flag is enabled for the given context @@ -165,17 +89,6 @@ def is_enabled(self, feature_flag_id, **kwargs): """ return self._check_feature(feature_flag_id, **kwargs)["enabled"] - def get_variant(self, feature_flag_id, **kwargs): - """ - Determine the variant for the given context - - :param str feature_flag_id: Name of the feature flag - :paramtype feature_flag_id: str - :return: Name of the variant - :rtype: str - """ - return self._check_feature(feature_flag_id, **kwargs)["variant"] - def _check_feature(self, feature_flag_id, **kwargs): """ Determine if the feature flag is enabled for the given context @@ -203,11 +116,7 @@ def _check_feature(self, feature_flag_id, **kwargs): if not feature_flag.enabled: # Feature flags that are disabled are always disabled - result["enabled"] = FeatureManager._check_default_disabled_variant(feature_flag) - if feature_flag.allocation: - variant_name = feature_flag.allocation.default_when_disabled - result["variant"] = self._variant_name_to_variant(feature_flag, variant_name) - return result + return False feature_conditions = feature_flag.conditions feature_filters = feature_conditions.client_filters @@ -232,25 +141,6 @@ def _check_feature(self, feature_flag_id, **kwargs): result["enabled"] = True break - if feature_flag.allocation and feature_flag.variants: - variant_name = self._assign_variant(feature_flag, **kwargs) - if variant_name: - result["enabled"] = FeatureManager._check_variant_override( - feature_flag.variants, variant_name, result["enabled"] - ) - result["variant"] = self._variant_name_to_variant(feature_flag, variant_name) - return result - - variant_name = None - if result["enabled"]: - result["enabled"] = FeatureManager._check_default_enabled_variant(feature_flag) - if feature_flag.allocation: - variant_name = feature_flag.allocation.default_when_enabled - else: - result["enabled"] = FeatureManager._check_default_disabled_variant(feature_flag) - if feature_flag.allocation: - variant_name = feature_flag.allocation.default_when_disabled - result["variant"] = self._variant_name_to_variant(feature_flag, variant_name) return result def list_feature_flag_names(self): diff --git a/featuremanagement/_models/_allocation.py b/featuremanagement/_models/_allocation.py deleted file mode 100644 index b46889c..0000000 --- a/featuremanagement/_models/_allocation.py +++ /dev/null @@ -1,193 +0,0 @@ -# ------------------------------------------------------------------------ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# ------------------------------------------------------------------------- -from dataclasses import dataclass -from ._constants import DEFAULT_WHEN_ENABLED, DEFAULT_WHEN_DISABLED, USER, GROUP, PERCENTILE, SEED - - -class Allocation: - """ - Represents an allocation - """ - - def __init__(self, feature_name): - self._default_when_enabled = None - self._default_when_disabled = None - self._user = [] - self._group = [] - self._percentile = [] - self._seed = "allocation\n" + feature_name - - @classmethod - def convert_from_json(cls, json, feature_name): - """ - Convert a JSON object to Allocation - - :param json: JSON object - :type json: dict - :return: Allocation - :rtype: Allocation - """ - if not json: - return None - allocation = cls(feature_name) - allocation._default_when_enabled = json.get(DEFAULT_WHEN_ENABLED) - allocation._default_when_disabled = json.get(DEFAULT_WHEN_DISABLED) - allocation._user = [] - allocation._group = [] - allocation._percentile = [] - if USER in json: - allocations = json.get(USER) - for user_allocation in allocations: - allocation._user.append(UserAllocation(**user_allocation)) - if GROUP in json: - allocations = json.get(GROUP) - for group_allocation in allocations: - allocation._group.append(GroupAllocation(**group_allocation)) - if PERCENTILE in json: - allocations = json.get(PERCENTILE) - for percentile_allocation in allocations: - allocation._percentile.append(PercentileAllocation.convert_from_json(percentile_allocation)) - allocation._seed = json.get(SEED, allocation._seed) - return allocation - - @property - def default_when_enabled(self): - """ - Get the default variant when the feature flag is enabled - - :return: Default variant when the feature flag is enabled - :rtype: str - """ - return self._default_when_enabled - - @property - def default_when_disabled(self): - """ - Get the default variant when the feature flag is disabled - - :return: Default variant when the feature flag is disabled - :rtype: str - """ - return self._default_when_disabled - - @property - def user(self): - """ - Get the user allocations - - :return: User allocations - :rtype: list[UserAllocation] - """ - return self._user - - @property - def group(self): - """ - Get the group allocations - - :return: Group allocations - :rtype: list[GroupAllocation] - """ - return self._group - - @property - def percentile(self): - """ - Get the percentile allocations - - :return: Percentile allocations - :rtype: list[PercentileAllocation] - """ - return self._percentile - - @property - def seed(self): - """ - Get the seed for the allocation - - :return: Seed for the allocation - :rtype: str - """ - return self._seed - - -@dataclass -class UserAllocation: - """ - Represents a user allocation - """ - - variant: str - users: list - - -@dataclass -class GroupAllocation: - """ - Represents a group allocation - """ - - variant: str - groups: list - - -class PercentileAllocation: - """ - Represents a percentile allocation - """ - - def __init__(self): - self._variant = None - self._percentile_from = None - self._percentile_to = None - - @classmethod - def convert_from_json(cls, json): - """ - Convert a JSON object to PercentileAllocation - - :param json: JSON object - :type json: dict - :return: PercentileAllocation - :rtype: PercentileAllocation - """ - if not json: - return None - user_allocation = cls() - user_allocation._variant = json.get("variant") - user_allocation._percentile_from = json.get("from") - user_allocation._percentile_to = json.get("to") - return user_allocation - - @property - def variant(self): - """ - Get the variant for the allocation - - :return: Variant for the allocation - :rtype: str - """ - return self._variant - - @property - def percentile_from(self): - """ - Get the starting percentile for the allocation - - :return: Starting percentile for the allocation - :rtype: int - """ - return self._percentile_from - - @property - def percentile_to(self): - """ - Get the ending percentile for the allocation - - :return: Ending percentile for the allocation - :rtype: int - """ - return self._percentile_to diff --git a/featuremanagement/_models/_constants.py b/featuremanagement/_models/_constants.py index 3f6ca41..b6ad52a 100644 --- a/featuremanagement/_models/_constants.py +++ b/featuremanagement/_models/_constants.py @@ -8,8 +8,6 @@ FEATURE_FLAG_ID = "id" FEATURE_FLAG_ENABLED = "enabled" FEATURE_FLAG_CONDITIONS = "conditions" -FEATURE_FLAG_ALLOCATION = "allocation" -FEATURE_FLAG_VARIANTS = "variants" # Conditions @@ -18,17 +16,3 @@ REQUIREMENT_TYPE_ANY = "Any" FEATURE_FLAG_CLIENT_FILTERS = "client_filters" FEATURE_FILTER_NAME = "name" - -# Allocation -DEFAULT_WHEN_ENABLED = "default_when_enabled" -DEFAULT_WHEN_DISABLED = "default_when_disabled" -USER = "user" -GROUP = "group" -PERCENTILE = "percentile" -SEED = "seed" - -# Variant Reference -VARIANT_REFERENCE_NAME = "name" -CONFIGURATION_VALUE = "configuration_value" -CONFIGURATION_REFERENCE = "configuration_reference" -STATUS_OVERRIDE = "status_override" diff --git a/featuremanagement/_models/_feature_flag.py b/featuremanagement/_models/_feature_flag.py index 229ebeb..b4648dc 100644 --- a/featuremanagement/_models/_feature_flag.py +++ b/featuremanagement/_models/_feature_flag.py @@ -4,7 +4,6 @@ # license information. # ------------------------------------------------------------------------- from ._feature_conditions import FeatureConditions -from ._allocation import Allocation from ._variant_reference import VariantReference from ._constants import ( FEATURE_FLAG_ID, @@ -24,8 +23,6 @@ def __init__(self): self._id = None self._enabled = False self._conditions = FeatureConditions() - self._allocation = None - self._variants = None @classmethod def convert_from_json(cls, json_value): @@ -51,15 +48,6 @@ def convert_from_json(cls, json_value): ) else: feature_flag._conditions = FeatureConditions() - feature_flag._allocation = Allocation.convert_from_json( - json_value.get(FEATURE_FLAG_ALLOCATION, None), feature_flag._id - ) - feature_flag._variants = None - if FEATURE_FLAG_VARIANTS in json_value: - variants = json_value.get(FEATURE_FLAG_VARIANTS) - feature_flag._variants = [] - for variant in variants: - feature_flag._variants.append(VariantReference.convert_from_json(variant)) feature_flag._validate() return feature_flag @@ -93,26 +81,6 @@ def conditions(self): """ return self._conditions - @property - def allocation(self): - """ - Get the allocation for the feature flag - - :return: Allocation for the feature flag - :rtype: Allocation - """ - return self._allocation - - @property - def variants(self): - """ - Get the variants for the feature flag - - :return: Variants for the feature flag - :rtype: list[VariantReference] - """ - return self._variants - def _validate(self): if not isinstance(self._id, str): raise ValueError(f"Invalid setting 'id' with value '{self._id}' for feature '{self._id}'.") diff --git a/featuremanagement/_models/_variant.py b/featuremanagement/_models/_variant.py deleted file mode 100644 index 25f692c..0000000 --- a/featuremanagement/_models/_variant.py +++ /dev/null @@ -1,36 +0,0 @@ -# ------------------------------------------------------------------------ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# ------------------------------------------------------------------------- - - -class Variant: - """ - A class representing a variant configuration assigned by a feature flag. - - :param name: The name of the variant - :type name: str - :param configuration: The configuration of the variant - :type configuration: dict - """ - - def __init__(self, name, configuration): - self._name = name - self._configuration = configuration - - @property - def name(self): - """ - The name of the variant - :rtype: str - """ - return self._name - - @property - def configuration(self): - """ - The configuration of the variant - :rtype: dict - """ - return self._configuration diff --git a/featuremanagement/_models/_variant_reference.py b/featuremanagement/_models/_variant_reference.py deleted file mode 100644 index 6c74786..0000000 --- a/featuremanagement/_models/_variant_reference.py +++ /dev/null @@ -1,79 +0,0 @@ -# ------------------------------------------------------------------------ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# ------------------------------------------------------------------------- -from dataclasses import dataclass -from ._constants import VARIANT_REFERENCE_NAME, CONFIGURATION_VALUE, CONFIGURATION_REFERENCE, STATUS_OVERRIDE - - -@dataclass -class VariantReference: - """ - Represents a variant reference - """ - - def __init__(self): - self._name = None - self._configuration_value = None - self._configuration_reference = None - self._status_override = None - - @classmethod - def convert_from_json(cls, json): - """ - Convert a JSON object to VariantReference - - :param json: JSON object - :type json: dict - :return: VariantReference - :rtype: VariantReference - """ - if not json: - return None - variant_reference = cls() - variant_reference._name = json.get(VARIANT_REFERENCE_NAME) - variant_reference._configuration_value = json.get(CONFIGURATION_VALUE) - variant_reference._configuration_reference = json.get(CONFIGURATION_REFERENCE) - variant_reference._status_override = json.get(STATUS_OVERRIDE, None) - return variant_reference - - @property - def name(self): - """ - Get the name of the variant - - :return: Name of the variant - :rtype: str - """ - return self._name - - @property - def configuration_value(self): - """ - Get the configuration value for the variant - - :return: Configuration value for the variant - :rtype: str - """ - return self._configuration_value - - @property - def configuration_reference(self): - """ - Get the configuration reference for the variant - - :return: Configuration reference for the variant - :rtype: str - """ - return self._configuration_reference - - @property - def status_override(self): - """ - Get the status override for the variant - - :return: Status override for the variant - :rtype: str - """ - return self._status_override diff --git a/featuremanagement/aio/_featuremanager.py b/featuremanagement/aio/_featuremanager.py index d67932d..27f1f78 100644 --- a/featuremanagement/aio/_featuremanager.py +++ b/featuremanagement/aio/_featuremanager.py @@ -5,7 +5,6 @@ # ------------------------------------------------------------------------- from collections.abc import Mapping import logging -import hashlib from ._defaultfilters import TimeWindowFilter, TargetingFilter from ._featurefilters import FeatureFilter from .._featuremanager import ( @@ -16,7 +15,6 @@ _get_feature_flag, _list_feature_flag_names, ) -from .._models._variant import Variant class FeatureManager: @@ -44,80 +42,6 @@ def __init__(self, configuration, **kwargs): raise ValueError("Custom filter must be a subclass of FeatureFilter") self._filters[feature_filter.name] = feature_filter - @staticmethod - def _check_default_disabled_variant(feature_flag): - if not feature_flag.allocation: - return False - return FeatureManager._check_variant_override( - feature_flag.variants, feature_flag.allocation.default_when_disabled, False - ) - - @staticmethod - def _check_default_enabled_variant(feature_flag): - if not feature_flag.allocation: - return True - return FeatureManager._check_variant_override( - feature_flag.variants, feature_flag.allocation.default_when_enabled, True - ) - - @staticmethod - def _check_variant_override(variants, default_variant_name, status): - if not variants or not default_variant_name: - return status - for variant in variants: - if variant.name == default_variant_name: - if variant.status_override == "Enabled": - return True - if variant.status_override == "Disabled": - return False - return status - - @staticmethod - def _is_targeted(context_id): - """Determine if the user is targeted for the given context""" - hashed_context_id = hashlib.sha256(context_id.encode()).digest() - context_marker = int.from_bytes(hashed_context_id[:4], byteorder="little", signed=False) - - return (context_marker / (2**32 - 1)) * 100 - - def _assign_variant(self, feature_flag, **kwargs): - if not feature_flag.variants or not feature_flag.allocation: - return None - if feature_flag.allocation.user: - user = kwargs.get("user") - if user: - for user_allocation in feature_flag.allocation.user: - if user in user_allocation.users: - return user_allocation.variant - if feature_flag.allocation.group: - groups = kwargs.get("groups") - if groups: - for group_allocation in feature_flag.allocation.group: - for group in groups: - if group in group_allocation.groups: - return group_allocation.variant - if feature_flag.allocation.percentile: - user = kwargs.get("user", "") - context_id = user + "\n" + feature_flag.allocation.seed - box = self._is_targeted(context_id) - for percentile_allocation in feature_flag.allocation.percentile: - if box == 100 and percentile_allocation.percentile_to == 100: - return percentile_allocation.variant - if percentile_allocation.percentile_from <= box < percentile_allocation.percentile_to: - return percentile_allocation.variant - return None - - def _variant_name_to_variant(self, feature_flag, variant_name): - if not feature_flag.variants: - return None - for variant_reference in feature_flag.variants: - if variant_reference.name == variant_name: - configuration = variant_reference.configuration_value - if not configuration: - configuration = self._configuration.get(variant_reference.configuration_reference) - return Variant(variant_reference.name, configuration) - return None - async def is_enabled(self, feature_flag_id, **kwargs): """ Determine if the feature flag is enabled for the given context @@ -129,17 +53,6 @@ async def is_enabled(self, feature_flag_id, **kwargs): """ return (await self._check_feature(feature_flag_id, **kwargs))["enabled"] - async def get_variant(self, feature_flag_id, **kwargs): - """ - Determine the variant for the given context - - :param str feature_flag_id: Name of the feature flag - :paramtype feature_flag_id: str - :return: Name of the variant - :rtype: str - """ - return (await self._check_feature(feature_flag_id, **kwargs))["variant"] - async def _check_feature(self, feature_flag_id, **kwargs): """ Determine if the feature flag is enabled for the given context @@ -167,11 +80,7 @@ async def _check_feature(self, feature_flag_id, **kwargs): if not feature_flag.enabled: # Feature flags that are disabled are always disabled - result["enabled"] = FeatureManager._check_default_disabled_variant(feature_flag) - if feature_flag.allocation: - variant_name = feature_flag.allocation.default_when_disabled - result["variant"] = self._variant_name_to_variant(feature_flag, variant_name) - return result + return False feature_conditions = feature_flag.conditions feature_filters = feature_conditions.client_filters @@ -197,25 +106,6 @@ async def _check_feature(self, feature_flag_id, **kwargs): result["enabled"] = True break - if feature_flag.allocation and feature_flag.variants: - variant_name = self._assign_variant(feature_flag, **kwargs) - if variant_name: - result["enabled"] = FeatureManager._check_variant_override( - feature_flag.variants, variant_name, result["enabled"] - ) - result["variant"] = self._variant_name_to_variant(feature_flag, variant_name) - return result - - variant_name = None - if result["enabled"]: - result["enabled"] = FeatureManager._check_default_enabled_variant(feature_flag) - if feature_flag.allocation: - variant_name = feature_flag.allocation.default_when_enabled - else: - result["enabled"] = FeatureManager._check_default_disabled_variant(feature_flag) - if feature_flag.allocation: - variant_name = feature_flag.allocation.default_when_disabled - result["variant"] = self._variant_name_to_variant(feature_flag, variant_name) return result def list_feature_flag_names(self): diff --git a/samples/feature_variant_sample.py b/samples/feature_variant_sample.py deleted file mode 100644 index d68e119..0000000 --- a/samples/feature_variant_sample.py +++ /dev/null @@ -1,25 +0,0 @@ -# ------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# -------------------------------------------------------------------------- - -import json -import os -import sys -from random_filter import RandomFilter -from featuremanagement import FeatureManager - - -script_directory = os.path.dirname(os.path.abspath(sys.argv[0])) - -with open(script_directory + "/formatted_feature_flags.json", "r", encoding="utf-8") as f: - feature_flags = json.load(f) - -feature_manager = FeatureManager(feature_flags, feature_filters=[RandomFilter()]) - -print(feature_manager.is_enabled("TestVariants", user="Adam")) -print(feature_manager.get_variant("TestVariants", user="Adam").configuration) - -print(feature_manager.is_enabled("TestVariants", user="Cass")) -print(feature_manager.get_variant("TestVariants", user="Cass").configuration) diff --git a/tests/test_feature_variants.py b/tests/test_feature_variants.py deleted file mode 100644 index c49a0ea..0000000 --- a/tests/test_feature_variants.py +++ /dev/null @@ -1,243 +0,0 @@ -# ------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# -------------------------------------------------------------------------- -from featuremanagement import FeatureManager, FeatureFilter - - -class TestFeatureVariants: - # method: is_enabled - def test_basic_feature_variant_override_enabled(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": True, - "variants": [ - {"name": "On", "status_override": "Disabled"}, - ], - "allocation": { - "default_when_enabled": "On", - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags) - assert not feature_manager.is_enabled("Alpha") - assert feature_manager.get_variant("Alpha").name == "On" - - # method: is_enabled - def test_basic_feature_variant_override_disabled(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": False, - "variants": [ - {"name": "Off", "status_override": "Enabled"}, - ], - "allocation": { - "default_when_disabled": "Off", - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags) - assert feature_manager.is_enabled("Alpha") - assert feature_manager.get_variant("Alpha").name == "Off" - - # method: is_enabled - def test_basic_feature_variant_no_override(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": False, - "variants": [ - {"name": "Off"}, - ], - "allocation": { - "default_when_disabled": "Off", - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags) - assert not feature_manager.is_enabled("Alpha") - assert feature_manager.get_variant("Alpha").name == "Off" - - # method: is_enabled - def test_basic_feature_variant_allocation_users(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": True, - "variants": [ - {"name": "Off", "status_override": "Enabled"}, - {"name": "On", "status_override": "Disabled"}, - ], - "allocation": { - "user": [{"variant": "On", "users": ["Adam"]}, {"variant": "Off", "users": ["Brittney"]}], - }, - "conditions": { - "client_filters": [ - { - "name": "AlwaysOnFilter", - "parameters": {}, - } - ] - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags, feature_filters=[AlwaysOnFilter()]) - assert feature_manager.is_enabled("Alpha") - assert feature_manager.get_variant("Alpha") is None - assert not feature_manager.is_enabled("Alpha", user="Adam") - assert feature_manager.get_variant("Alpha", user="Adam").name == "On" - assert feature_manager.is_enabled("Alpha", user="Brittney") - assert feature_manager.get_variant("Alpha", user="Brittney").name == "Off" - assert feature_manager.is_enabled("Alpha", user="Charlie") - assert feature_manager.get_variant("Alpha", user="Charlie") is None - - # method: is_enabled - def test_basic_feature_variant_allocation_groups(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": True, - "variants": [ - {"name": "Off", "status_override": "Enabled"}, - {"name": "On", "status_override": "Disabled"}, - ], - "allocation": { - "group": [ - {"variant": "On", "groups": ["Group1"]}, - {"variant": "Off", "groups": ["Group2"]}, - ], - }, - "conditions": { - "client_filters": [ - { - "name": "AlwaysOnFilter", - "parameters": {}, - } - ] - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags, feature_filters=[AlwaysOnFilter()]) - assert feature_manager.is_enabled("Alpha") - assert feature_manager.get_variant("Alpha") is None - assert not feature_manager.is_enabled("Alpha", user="Adam", groups=["Group1"]) - assert feature_manager.get_variant("Alpha", user="Adam", groups=["Group1"]).name == "On" - assert feature_manager.is_enabled("Alpha", user="Brittney", groups=["Group2"]) - assert feature_manager.get_variant("Alpha", user="Brittney", groups=["Group2"]).name == "Off" - assert feature_manager.is_enabled("Alpha", user="Charlie", groups=["Group3"]) - assert feature_manager.get_variant("Alpha", user="Charlie", groups=["Group3"]) is None - - # method: is_enabled - def test_basic_feature_variant_allocation_percentile(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": True, - "variants": [ - {"name": "Off", "status_override": "Enabled"}, - {"name": "On", "status_override": "Disabled"}, - ], - "allocation": { - "percentile": [ - {"variant": "On", "from": 0, "to": 50}, - {"variant": "Off", "from": 50, "to": 100}, - ], - }, - "conditions": { - "client_filters": [ - { - "name": "AlwaysOnFilter", - "parameters": {}, - } - ] - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags, feature_filters=[AlwaysOnFilter()]) - assert feature_manager.is_enabled("Alpha") - assert feature_manager.get_variant("Alpha").name == "Off" - assert feature_manager.is_enabled("Alpha", user="Adam") - assert feature_manager.get_variant("Alpha", user="Adam").name == "Off" - assert not feature_manager.is_enabled("Alpha", user="Brittney") - assert feature_manager.get_variant("Alpha", user="Brittney").name == "On" - assert not feature_manager.is_enabled("Alpha", user="Brittney", groups=["Group1"]) - assert feature_manager.get_variant("Alpha", user="Brittney", groups=["Group1"]).name == "On" - assert feature_manager.is_enabled("Alpha", user="Cassidy") - assert feature_manager.get_variant("Alpha", user="Cassidy").name == "Off" - - # method: is_enabled - def test_basic_feature_variant_allocation_percentile_seeded(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": True, - "variants": [ - {"name": "Off", "status_override": "Enabled"}, - {"name": "On", "status_override": "Disabled"}, - ], - "allocation": { - "percentile": [ - {"variant": "On", "from": 0, "to": 50}, - {"variant": "Off", "from": 50, "to": 100}, - ], - "seed": "test-seed2", - }, - "conditions": { - "client_filters": [ - { - "name": "AlwaysOnFilter", - "parameters": {}, - } - ] - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags, feature_filters=[AlwaysOnFilter()]) - assert feature_manager.is_enabled("Alpha") - assert feature_manager.get_variant("Alpha").name == "Off" - assert not feature_manager.is_enabled("Alpha", user="Allison") - assert feature_manager.get_variant("Alpha", user="Allison").name == "On" - assert feature_manager.is_enabled("Alpha", user="Bubbles") - assert feature_manager.get_variant("Alpha", user="Bubbles").name == "Off" - assert feature_manager.is_enabled("Alpha", user="Bubbles", groups=["Group1"]) - assert feature_manager.get_variant("Alpha", user="Bubbles", groups=["Group1"]).name == "Off" - assert feature_manager.is_enabled("Alpha", user="Cassidy") - assert feature_manager.get_variant("Alpha", user="Cassidy").name == "Off" - assert not feature_manager.is_enabled("Alpha", user="Dan") - assert feature_manager.get_variant("Alpha", user="Dan").name == "On" - - -class AlwaysOnFilter(FeatureFilter): - def evaluate(self, context, **kwargs): - return True diff --git a/tests/test_feature_variants_async.py b/tests/test_feature_variants_async.py deleted file mode 100644 index 3387c8f..0000000 --- a/tests/test_feature_variants_async.py +++ /dev/null @@ -1,244 +0,0 @@ -# ------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# -------------------------------------------------------------------------- -from unittest import IsolatedAsyncioTestCase -from featuremanagement.aio import FeatureManager, FeatureFilter - - -class TestFeatureVariantsAsync(IsolatedAsyncioTestCase): - # method: is_enabled - async def test_basic_feature_variant_override_enabled(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": True, - "variants": [ - {"name": "On", "status_override": "Disabled"}, - ], - "allocation": { - "default_when_enabled": "On", - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags) - assert not await feature_manager.is_enabled("Alpha") - assert (await feature_manager.get_variant("Alpha")).name == "On" - - # method: is_enabled - async def test_basic_feature_variant_override_disabled(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": False, - "variants": [ - {"name": "Off", "status_override": "Enabled"}, - ], - "allocation": { - "default_when_disabled": "Off", - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags) - assert await feature_manager.is_enabled("Alpha") - assert (await feature_manager.get_variant("Alpha")).name == "Off" - - # method: is_enabled - async def test_basic_feature_variant_no_override(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": False, - "variants": [ - {"name": "Off"}, - ], - "allocation": { - "default_when_disabled": "Off", - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags) - assert not await feature_manager.is_enabled("Alpha") - assert (await feature_manager.get_variant("Alpha")).name == "Off" - - # method: is_enabled - async def test_basic_feature_variant_allocation_users(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": True, - "variants": [ - {"name": "Off", "status_override": "Enabled"}, - {"name": "On", "status_override": "Disabled"}, - ], - "allocation": { - "user": [{"variant": "On", "users": ["Adam"]}, {"variant": "Off", "users": ["Brittney"]}], - }, - "conditions": { - "client_filters": [ - { - "name": "AlwaysOnFilter", - "parameters": {}, - } - ] - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags, feature_filters=[AlwaysOnFilter()]) - assert await feature_manager.is_enabled("Alpha") - assert (await feature_manager.get_variant("Alpha")) is None - assert not await feature_manager.is_enabled("Alpha", user="Adam") - assert (await feature_manager.get_variant("Alpha", user="Adam")).name == "On" - assert await feature_manager.is_enabled("Alpha", user="Brittney") - assert (await feature_manager.get_variant("Alpha", user="Brittney")).name == "Off" - assert await feature_manager.is_enabled("Alpha", user="Charlie") - assert (await feature_manager.get_variant("Alpha", user="Charlie")) is None - - # method: is_enabled - async def test_basic_feature_variant_allocation_groups(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": True, - "variants": [ - {"name": "Off", "status_override": "Enabled"}, - {"name": "On", "status_override": "Disabled"}, - ], - "allocation": { - "group": [ - {"variant": "On", "groups": ["Group1"]}, - {"variant": "Off", "groups": ["Group2"]}, - ], - }, - "conditions": { - "client_filters": [ - { - "name": "AlwaysOnFilter", - "parameters": {}, - } - ] - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags, feature_filters=[AlwaysOnFilter()]) - assert await feature_manager.is_enabled("Alpha") - assert (await feature_manager.get_variant("Alpha")) is None - assert not await feature_manager.is_enabled("Alpha", user="Adam", groups=["Group1"]) - assert (await feature_manager.get_variant("Alpha", user="Adam", groups=["Group1"])).name == "On" - assert await feature_manager.is_enabled("Alpha", user="Brittney", groups=["Group2"]) - assert (await feature_manager.get_variant("Alpha", user="Brittney", groups=["Group2"])).name == "Off" - assert await feature_manager.is_enabled("Alpha", user="Charlie", groups=["Group3"]) - assert (await feature_manager.get_variant("Alpha", user="Charlie", groups=["Group3"])) is None - - # method: is_enabled - async def test_basic_feature_variant_allocation_percentile(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": True, - "variants": [ - {"name": "Off", "status_override": "Enabled"}, - {"name": "On", "status_override": "Disabled"}, - ], - "allocation": { - "percentile": [ - {"variant": "On", "from": 0, "to": 50}, - {"variant": "Off", "from": 50, "to": 100}, - ], - }, - "conditions": { - "client_filters": [ - { - "name": "AlwaysOnFilter", - "parameters": {}, - } - ] - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags, feature_filters=[AlwaysOnFilter()]) - assert await feature_manager.is_enabled("Alpha") - assert (await feature_manager.get_variant("Alpha")).name == "Off" - assert await feature_manager.is_enabled("Alpha", user="Adam") - assert (await feature_manager.get_variant("Alpha", user="Adam")).name == "Off" - assert not await feature_manager.is_enabled("Alpha", user="Brittney") - assert (await feature_manager.get_variant("Alpha", user="Brittney")).name == "On" - assert not await feature_manager.is_enabled("Alpha", user="Brittney", groups=["Group1"]) - assert (await feature_manager.get_variant("Alpha", user="Brittney", groups=["Group1"])).name == "On" - assert await feature_manager.is_enabled("Alpha", user="Cassidy") - assert (await feature_manager.get_variant("Alpha", user="Cassidy")).name == "Off" - - # method: is_enabled - async def test_basic_feature_variant_allocation_percentile_seeded(self): - feature_flags = { - "feature_management": { - "feature_flags": [ - { - "id": "Alpha", - "enabled": True, - "variants": [ - {"name": "Off", "status_override": "Enabled"}, - {"name": "On", "status_override": "Disabled"}, - ], - "allocation": { - "percentile": [ - {"variant": "On", "from": 0, "to": 50}, - {"variant": "Off", "from": 50, "to": 100}, - ], - "seed": "test-seed2", - }, - "conditions": { - "client_filters": [ - { - "name": "AlwaysOnFilter", - "parameters": {}, - } - ] - }, - } - ] - } - } - feature_manager = FeatureManager(feature_flags, feature_filters=[AlwaysOnFilter()]) - assert await feature_manager.is_enabled("Alpha") - assert (await feature_manager.get_variant("Alpha")).name == "Off" - assert not await feature_manager.is_enabled("Alpha", user="Allison") - assert (await feature_manager.get_variant("Alpha", user="Allison")).name == "On" - assert await feature_manager.is_enabled("Alpha", user="Bubbles") - assert (await feature_manager.get_variant("Alpha", user="Bubbles")).name == "Off" - assert await feature_manager.is_enabled("Alpha", user="Bubbles", groups=["Group1"]) - assert (await feature_manager.get_variant("Alpha", user="Bubbles", groups=["Group1"])).name == "Off" - assert await feature_manager.is_enabled("Alpha", user="Cassidy") - assert (await feature_manager.get_variant("Alpha", user="Cassidy")).name == "Off" - assert not await feature_manager.is_enabled("Alpha", user="Dan") - assert (await feature_manager.get_variant("Alpha", user="Dan")).name == "On" - - -class AlwaysOnFilter(FeatureFilter): - async def evaluate(self, context, **kwargs): - return True From 6555f76446c7471f7895f8b8d5278eaa72194b2b Mon Sep 17 00:00:00 2001 From: Matt Metcalf Date: Fri, 10 May 2024 15:04:53 -0700 Subject: [PATCH 2/7] Update _feature_flag.py --- featuremanagement/_models/_feature_flag.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/featuremanagement/_models/_feature_flag.py b/featuremanagement/_models/_feature_flag.py index b4648dc..22e76ad 100644 --- a/featuremanagement/_models/_feature_flag.py +++ b/featuremanagement/_models/_feature_flag.py @@ -4,13 +4,10 @@ # license information. # ------------------------------------------------------------------------- from ._feature_conditions import FeatureConditions -from ._variant_reference import VariantReference from ._constants import ( FEATURE_FLAG_ID, FEATURE_FLAG_ENABLED, FEATURE_FLAG_CONDITIONS, - FEATURE_FLAG_ALLOCATION, - FEATURE_FLAG_VARIANTS, ) From a83c198497abdfb30d8a8075cc768504a9f67c25 Mon Sep 17 00:00:00 2001 From: Matt Metcalf Date: Fri, 10 May 2024 15:08:56 -0700 Subject: [PATCH 3/7] fixing tests --- featuremanagement/_featuremanager.py | 4 ++-- featuremanagement/aio/_featuremanager.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/featuremanagement/_featuremanager.py b/featuremanagement/_featuremanager.py index d8561d6..4c0fd98 100644 --- a/featuremanagement/_featuremanager.py +++ b/featuremanagement/_featuremanager.py @@ -98,7 +98,7 @@ def _check_feature(self, feature_flag_id, **kwargs): :return: True if the feature flag is enabled for the given context :rtype: bool """ - result = {"enabled": None, "variant": None} + result = {"enabled": None} if self._copy is not self._configuration.get(FEATURE_MANAGEMENT_KEY): self._cache = {} self._copy = self._configuration.get(FEATURE_MANAGEMENT_KEY) @@ -116,7 +116,7 @@ def _check_feature(self, feature_flag_id, **kwargs): if not feature_flag.enabled: # Feature flags that are disabled are always disabled - return False + return {"enabled": False,} feature_conditions = feature_flag.conditions feature_filters = feature_conditions.client_filters diff --git a/featuremanagement/aio/_featuremanager.py b/featuremanagement/aio/_featuremanager.py index 27f1f78..c815030 100644 --- a/featuremanagement/aio/_featuremanager.py +++ b/featuremanagement/aio/_featuremanager.py @@ -62,7 +62,7 @@ async def _check_feature(self, feature_flag_id, **kwargs): :return: True if the feature flag is enabled for the given context :rtype: bool """ - result = {"enabled": None, "variant": None} + result = {"enabled": None} if self._copy is not self._configuration.get(FEATURE_MANAGEMENT_KEY): self._cache = {} self._copy = self._configuration.get(FEATURE_MANAGEMENT_KEY) @@ -80,7 +80,7 @@ async def _check_feature(self, feature_flag_id, **kwargs): if not feature_flag.enabled: # Feature flags that are disabled are always disabled - return False + return {"enabled": False} feature_conditions = feature_flag.conditions feature_filters = feature_conditions.client_filters From cef1cee6b4845a4839e2bb6e1cecf209723a7ab5 Mon Sep 17 00:00:00 2001 From: Matt Metcalf Date: Fri, 10 May 2024 15:11:28 -0700 Subject: [PATCH 4/7] Update _featuremanager.py --- featuremanagement/_featuremanager.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/featuremanagement/_featuremanager.py b/featuremanagement/_featuremanager.py index 4c0fd98..3626bd6 100644 --- a/featuremanagement/_featuremanager.py +++ b/featuremanagement/_featuremanager.py @@ -116,7 +116,9 @@ def _check_feature(self, feature_flag_id, **kwargs): if not feature_flag.enabled: # Feature flags that are disabled are always disabled - return {"enabled": False,} + return { + "enabled": False, + } feature_conditions = feature_flag.conditions feature_filters = feature_conditions.client_filters From 4aadc0f3e7fbee988f602857aa75148f3aef7aca Mon Sep 17 00:00:00 2001 From: Matt Metcalf Date: Tue, 21 May 2024 13:45:17 -0700 Subject: [PATCH 5/7] Update Merge of Targeting Update --- featuremanagement/_models/__init__.py | 3 +- featuremanagement/_models/_telemetry.py | 16 ---- featuremanagement/_models/_variant.py | 35 -------- .../_models/_variant_reference.py | 79 ------------------- 4 files changed, 1 insertion(+), 132 deletions(-) delete mode 100644 featuremanagement/_models/_telemetry.py delete mode 100644 featuremanagement/_models/_variant.py delete mode 100644 featuremanagement/_models/_variant_reference.py diff --git a/featuremanagement/_models/__init__.py b/featuremanagement/_models/__init__.py index 5aba53f..f63470c 100644 --- a/featuremanagement/_models/__init__.py +++ b/featuremanagement/_models/__init__.py @@ -4,10 +4,9 @@ # license information. # ------------------------------------------------------------------------- from ._feature_flag import FeatureFlag -from ._variant import Variant from ._evaluation_event import EvaluationEvent from ._targeting_context import TargetingContext __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore -__all__ = ["FeatureFlag", "Variant", "EvaluationEvent", "TargetingContext"] +__all__ = ["FeatureFlag", "EvaluationEvent", "TargetingContext"] diff --git a/featuremanagement/_models/_telemetry.py b/featuremanagement/_models/_telemetry.py deleted file mode 100644 index 55e3917..0000000 --- a/featuremanagement/_models/_telemetry.py +++ /dev/null @@ -1,16 +0,0 @@ -# ------------------------------------------------------------------------ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# ------------------------------------------------------------------------- -from dataclasses import dataclass, field - - -@dataclass -class Telemetry: - """ - Represents the telemetry configuration for a feature flag. - """ - - enabled: bool = False - metadata: dict = field(default_factory=dict) diff --git a/featuremanagement/_models/_variant.py b/featuremanagement/_models/_variant.py deleted file mode 100644 index 3dc4548..0000000 --- a/featuremanagement/_models/_variant.py +++ /dev/null @@ -1,35 +0,0 @@ -# ------------------------------------------------------------------------ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# ------------------------------------------------------------------------- - - -class Variant: - """ - A class representing a variant configuration assigned by a feature flag. - - :param str name: The name of the variant - :param dict configuration: The configuration of the variant. - """ - - def __init__(self, name, configuration): - self._name = name - self._configuration = configuration - - @property - def name(self): - """ - The name of the variant. - :rtype: str - """ - return self._name - - @property - def configuration(self): - """ - The configuration of the variant. - :rtype: dict - """ - return self._configuration - diff --git a/featuremanagement/_models/_variant_reference.py b/featuremanagement/_models/_variant_reference.py deleted file mode 100644 index 47cb3cc..0000000 --- a/featuremanagement/_models/_variant_reference.py +++ /dev/null @@ -1,79 +0,0 @@ -# ------------------------------------------------------------------------ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# ------------------------------------------------------------------------- -from dataclasses import dataclass -from ._constants import VARIANT_REFERENCE_NAME, CONFIGURATION_VALUE, CONFIGURATION_REFERENCE, STATUS_OVERRIDE - - -@dataclass -class VariantReference: - """ - Represents a variant reference. - """ - - def __init__(self): - self._name = None - self._configuration_value = None - self._configuration_reference = None - self._status_override = None - - @classmethod - def convert_from_json(cls, json): - """ - Convert a JSON object to VariantReference. - - :param dict json: JSON object - :return: VariantReference - :rtype: VariantReference - """ - if not json: - return None - variant_reference = cls() - variant_reference._name = json.get(VARIANT_REFERENCE_NAME) - variant_reference._configuration_value = json.get(CONFIGURATION_VALUE) - variant_reference._configuration_reference = json.get(CONFIGURATION_REFERENCE) - variant_reference._status_override = json.get(STATUS_OVERRIDE, None) - return variant_reference - - @property - def name(self): - """ - Get the name of the variant. - - :return: Name of the variant - :rtype: str - """ - return self._name - - @property - def configuration_value(self): - """ - Get the configuration value for the variant. - - :return: Configuration value for the variant. - :rtype: str - """ - return self._configuration_value - - @property - def configuration_reference(self): - """ - Get the configuration reference for the variant. - - :return: Configuration reference for the variant. - :rtype: str - """ - return self._configuration_reference - - @property - def status_override(self): - """ - Get the status override for the variant. - - :return: Status override for the variant. - :rtype: str - """ - return self._status_override - From b11651bc0591bd49f56831e72036c72898dc7274 Mon Sep 17 00:00:00 2001 From: Matt Metcalf Date: Tue, 21 May 2024 13:48:42 -0700 Subject: [PATCH 6/7] Update _featuremanager.py --- featuremanagement/aio/_featuremanager.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/featuremanagement/aio/_featuremanager.py b/featuremanagement/aio/_featuremanager.py index 7f83d80..6d192b5 100644 --- a/featuremanagement/aio/_featuremanager.py +++ b/featuremanagement/aio/_featuremanager.py @@ -128,6 +128,10 @@ async def _check_feature(self, feature_flag_id, targeting_context, **kwargs): logging.warning("Feature flag %s not found", feature_flag_id) # Unknown feature flags are disabled by default return EvaluationEvent(enabled=False) + + if not feature_flag.enabled: + # Feature flags that are disabled are always disabled + return EvaluationEvent(enabled=False) return await self._check_feature_filters(feature_flag, targeting_context, **kwargs) From c37be47c293d717b46d2ad942bb1b3b723d86d63 Mon Sep 17 00:00:00 2001 From: Matt Metcalf Date: Tue, 21 May 2024 13:50:31 -0700 Subject: [PATCH 7/7] Update _featuremanager.py --- featuremanagement/aio/_featuremanager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/featuremanagement/aio/_featuremanager.py b/featuremanagement/aio/_featuremanager.py index 6d192b5..3a2f915 100644 --- a/featuremanagement/aio/_featuremanager.py +++ b/featuremanagement/aio/_featuremanager.py @@ -128,7 +128,7 @@ async def _check_feature(self, feature_flag_id, targeting_context, **kwargs): logging.warning("Feature flag %s not found", feature_flag_id) # Unknown feature flags are disabled by default return EvaluationEvent(enabled=False) - + if not feature_flag.enabled: # Feature flags that are disabled are always disabled return EvaluationEvent(enabled=False)