Skip to content

Commit

Permalink
FeatureFlag: Add __str__ and __repr__
Browse files Browse the repository at this point in the history
Store the additional name. In retrospect, it was a bad idea to name the `__init__` kwarg `cache_key`, when that has another meaning.
  • Loading branch information
phihag committed Oct 29, 2024
1 parent 2b48c43 commit 0c73c17
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Feature flags: https://github.com/boxine/bx_django_utils/blob/master/bx_django_u

#### bx_django_utils.feature_flags.data_classes

* [`FeatureFlag()`](https://github.com/boxine/bx_django_utils/blob/master/bx_django_utils/feature_flags/data_classes.py#L18-L168) - A feature flag that persistent the state into django cache/database.
* [`FeatureFlag()`](https://github.com/boxine/bx_django_utils/blob/master/bx_django_utils/feature_flags/data_classes.py#L18-L176) - A feature flag that persistent the state into django cache/database.

#### bx_django_utils.feature_flags.test_utils

Expand Down
8 changes: 8 additions & 0 deletions bx_django_utils/feature_flags/data_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(

validate_cache_key(cache_key)
validate_cache_key(cache_key_prefix)
self.name = cache_key
self.cache_key = f'{cache_key_prefix}-{cache_key}'
validate_cache_key(self.cache_key) # Double check ;)

Expand Down Expand Up @@ -166,3 +167,10 @@ def get_by_cache_key(cls, cache_key) -> "FeatureFlag":

def __bool__(self):
return self.is_enabled

def __str__(self):
return f'<FeatureFlag {self.name}>'

def __repr__(self):
initial = self.initial_state == State.ENABLED
return f'FeatureFlag(cache_key={self.name!r}, human_name={self.human_name!r}, initial_enabled={initial!r})'
9 changes: 9 additions & 0 deletions bx_django_utils/feature_flags/tests/test_feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,12 @@ def test_reset(self):

# Reset again – should not error
flag.reset()

def test_str(self):
flag = FeatureFlag(
cache_key='be-wild',
human_name='Be wild',
initial_enabled=False,
)
self.assertEqual(str(flag), '<FeatureFlag be-wild>')
self.assertEqual(repr(flag), "FeatureFlag(cache_key='be-wild', human_name='Be wild', initial_enabled=False)")

0 comments on commit 0c73c17

Please sign in to comment.