-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path_featurefilters.py
More file actions
53 lines (41 loc) · 1.51 KB
/
_featurefilters.py
File metadata and controls
53 lines (41 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# ------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# -------------------------------------------------------------------------
"""Base class for async feature filters."""
from abc import ABC, abstractmethod
from typing import Mapping, Callable, Any, Optional
class FeatureFilter(ABC):
"""
Parent class for all async feature filters.
"""
_alias: Optional[str] = None
@abstractmethod
async def evaluate(self, context: Mapping[Any, Any], **kwargs: Any) -> bool:
"""
Determine if the feature flag is enabled for the given context.
:param Mapping context: Context for the feature flag.
"""
@property
def name(self) -> str:
"""
Get the name of the filter.
:return: Name of the filter, or alias if it exists.
:rtype: str
"""
if hasattr(self, "_alias") and self._alias:
return self._alias
return self.__class__.__name__
@staticmethod
def alias(alias: str) -> Callable[..., Any]:
"""
Decorator to set the alias for the filter.
:param str alias: Alias for the filter.
:return: Decorator
:rtype: Callable
"""
def wrapper(cls: "FeatureFilter") -> Any:
cls._alias = alias # pylint: disable=protected-access
return cls
return wrapper