Skip to main content

A minimalistic feature flag implementation with type hints.

Project description

Fiicha is minimalistic mypy-friendly feature flag implementation with no external dependencies.

Installation

pip install fiicha

For Development

Clone the repo, change current working dir to the root of the repo and execute:

python -m venv env
. env/bin/activate
pip install -e .[test,lint]

Usage

Basic

Basic usage is simple:

  1. Sublcass from FeatureFlags

  2. Add FeatureFlag attributes representing individual feature flags.

from fiicha import FeatureFlag, FeatureFlags

class MyProjectFeatureFlags(FeatureFlags):
    release_x = FeatureFlag("Enable features from upcoming release X")
    use_new_algorithm = FeatureFlag("Use new algorithm for ...")

ff = MyProjectFeatureFlags({"release_x": True})

print(ff.release_x)  # True
print(ff.use_new_algorithm)  # False

In addition, you kan specify a key defining default state of the feature flags:

ff = MyProjectFeatureFlags({"all": True, "release_x": False}, default_key="all")

print(ff.release_x)  # False
print(ff.use_new_algorithm)  # True

Classes subclassed from FeatureFlags has full type hint support, so when you are accessing removed or otherwise non-existent feature flags, you can detect it before running tests:

ff.xxx  # will trigger mypy's attr-defined error

Utils

The package comes with few util functions to avoid building your own framework for simple use cases.

Single-line Configuration

from fiicha import parse_feature_flags_string

flags_string = "release_x !use_new_algorithm"  # stored in config
ff = MyProjectFeatureFlags(parse_feature_flags_string(flags_string))

print(ff.release_x)  # True
print(ff.use_new_algorithm)  # False

Load Flags From Environment Variables

import os
from fiicha import feature_flags_from_environ

os.environ["MYPROJ_FEATURE_RELEASE_X"] = "True"  # can also be 1, t, yes, on
os.environ["MYPROJ_FEATURE_USE_NEW_ALGORITHM"] = "False"  # can also be 0, f, no, off or empty string

ff = MyProjectFeatureFlags(feature_flags_from_environ("MYPROJ_FEATURE_"))

print(ff.release_x)  # True
print(ff.use_new_algorithm)  # False

Automatically Document Your Feature Flags

from fiicha import make_napoleon_doc, make_sphinx_doc

class GoogleStyleDocFeatureFlags(FeatureFlags, make_doc=make_napoleon_doc):
    a = FeatureFlag("Enable feature A")
    b = FeatureFlag("Enable feature B")

class SphinxStyleDocFeatureFlags(FeatureFlags, make_doc=make_sphinx_doc):
    x = FeatureFlag("Enable feature X")
    y = FeatureFlag("Enable feature Y")

print(GoogleStyleDocFeatureFlags.__doc__)
print(SphinxStyleDocFeatureFlags.__doc__)

Context Variable Support

You might want to do A/B testing or otherwise toggle feature flags for individual users without affecting the rest of the system. For such cases, there is FeatureFlagsContext - a wrapper around ContextVar. It copies current feature flags and sets them as new context variable on enter and resets them back on exit. This way you will be able to achieve global feature flags protection from changes made within context of a request or a task.

from contextvars import ContextVar
from fiicha import FeatureFlag, FeatureFlags, FeatureFlagsContext

class MyFeatureFlags(FeatureFlags):
    a = FeatureFlag("Enable feature A")
    b = FeatureFlag("Enable feature B")

root = MyFeatureFlags()
var: ContextVar[MyFeatureFlags] = ContextVar("test", default=root)
ff_ctx = FeatureFlagsContext(var)

with ff_ctx:
    ff = var.get()
    ff.a = True

    assert ff is not root  # is a copy

assert not root.a  # not changed

Advanced

See examples subfolder within the repo.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fiicha-0.1.0.tar.gz (8.6 kB view hashes)

Uploaded Source

Built Distribution

fiicha-0.1.0-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page