Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add is_feature_enabled to variant response #285

Merged
merged 9 commits into from
Nov 7, 2023

Conversation

nunogois
Copy link
Member

@nunogois nunogois commented Nov 6, 2023

Description

Adds a is_feature_enabled property to the variant response. This can be especially useful to differentiate between:

  • Feature flag that is disabled
  • Feature flag that is enabled but has no variants

Fixes #284

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Added new tests and/or adapted existing tests to assume the new behavior.

  • Unit tests
  • Spec Tests
  • Integration tests / Manual Tests

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@coveralls
Copy link

coveralls commented Nov 6, 2023

Coverage Status

coverage: 96.892% (+0.2%) from 96.684%
when pulling 213d45b on feat-variant-add-is-feature-enabled
into 2ddb217 on main.

assert variant == {
"enabled": False,
"name": "disabled",
"is_feature_enabled": True,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we choose is_feature_enabled over feature_enabled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the name suggested in the issue, and I noticed we also have a variable with this name in the existing codebase.
If you prefer feature_enabled let me know, I can easily make the change 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know that I would have picked this up but now that we're talking, I'm leaning towards "feature_enabled" rather

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in b754eac

Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, I'd like it we can at least address the copy/dict comprehension story before we merge though

The bootstrapping is optional but nice, if it's too much work I'm okay to let that one go

UnleashClient/features/Feature.py Outdated Show resolved Hide resolved
UnleashClient/features/Feature.py Outdated Show resolved Hide resolved
@@ -427,13 +428,14 @@ def get_variant(self, feature_name: str, context: Optional[dict] = None) -> dict
"""
context = context or {}
context.update(self.unleash_static_context)
disabled_variation = copy.deepcopy(DISABLED_VARIATION)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the copy here? This is a constant so we should never be mutating this

Copy link
Member Author

@nunogois nunogois Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't. Addressed in cc102a1

Fun story: This was me being a Python noob and not realizing the del variant["feature_enabled"] on my test_client_specs was actually affecting the DISABLED_VARIANT constant that was being returned, making it so it would no longer have the feature_enabled key, which in turn caused future del variant["feature_enabled"] to fail in the following tests, since the key would no longer be present. I noticed we deepcopied the DISABLED_VARIANT in another place, so I tried that and it worked, which is why we have this copy.

Overall I think it's better to use the dict comprehension like you've shown me on the other comment, so I adapted the test. I also got rid of the other copies as I don't think they are necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooo! Nice spot! Burned by state mutations xD



@responses.activate
def test_uc_get_variant_feature_enabled_no_variants():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get away with bootstrapping rather than a mocked response? These sleeps are making the test suite insanely slow and I'm trying to be defensive about making that problem worse. There's some examples in the test_client_specs.py file where we use bootstrap over a mocked response that removes the need for sleeps and HTTP. If we can't then we can't but I gotta ask!

Copy link
Member Author

@nunogois nunogois Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 7c687f0
Might be worth doing the same for the rest of the tests, but out of scope for this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to but gosh it's a lot of work. Definitely something on my back burner for when insomnia strikes again

assert variant == {
"enabled": False,
"name": "disabled",
"is_feature_enabled": True,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know that I would have picked this up but now that we're talking, I'm leaning towards "feature_enabled" rather

Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
4 participants