From 56c5a2f2044a3c2d0cff59820ad77d6dd52cb406 Mon Sep 17 00:00:00 2001 From: Lukas Reining Date: Sat, 16 Nov 2024 02:07:59 +0100 Subject: [PATCH 1/7] feat: implement transaction context Signed-off-by: Lukas Reining --- README.md | 101 +++++++++- openfeature/api.py | 26 +++ openfeature/client.py | 3 +- openfeature/transaction_context/__init__.py | 15 ++ ...text_var_transaction_context_propagator.py | 18 ++ .../no_op_transaction_context_propagator.py | 12 ++ .../transaction_context_propagator.py | 16 ++ pyproject.toml | 1 + tests/test_transaction_context.py | 172 ++++++++++++++++++ 9 files changed, 353 insertions(+), 11 deletions(-) create mode 100644 openfeature/transaction_context/__init__.py create mode 100644 openfeature/transaction_context/context_var_transaction_context_propagator.py create mode 100644 openfeature/transaction_context/no_op_transaction_context_propagator.py create mode 100644 openfeature/transaction_context/transaction_context_propagator.py create mode 100644 tests/test_transaction_context.py diff --git a/README.md b/README.md index 909ee20e..6a491aad 100644 --- a/README.md +++ b/README.md @@ -99,16 +99,17 @@ print("Value: " + str(flag_value)) ## 🌟 Features -| Status | Features | Description | -| ------ | ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | -| βœ… | [Providers](#providers) | Integrate with a commercial, open source, or in-house feature management tool. | -| βœ… | [Targeting](#targeting) | Contextually-aware flag evaluation using [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context). | -| βœ… | [Hooks](#hooks) | Add functionality to various stages of the flag evaluation life-cycle. | -| βœ… | [Logging](#logging) | Integrate with popular logging packages. | -| βœ… | [Domains](#domains) | Logically bind clients with providers. | -| βœ… | [Eventing](#eventing) | React to state changes in the provider or flag management system. | -| βœ… | [Shutdown](#shutdown) | Gracefully clean up a provider during application shutdown. | -| βœ… | [Extending](#extending) | Extend OpenFeature with custom providers and hooks. | +| Status | Features | Description | +|--------|---------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------| +| βœ… | [Providers](#providers) | Integrate with a commercial, open source, or in-house feature management tool. | +| βœ… | [Targeting](#targeting) | Contextually-aware flag evaluation using [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context). | +| βœ… | [Hooks](#hooks) | Add functionality to various stages of the flag evaluation life-cycle. | +| βœ… | [Logging](#logging) | Integrate with popular logging packages. | +| βœ… | [Domains](#domains) | Logically bind clients with providers. | +| βœ… | [Eventing](#eventing) | React to state changes in the provider or flag management system. | +| βœ… | [Shutdown](#shutdown) | Gracefully clean up a provider during application shutdown. | +| βœ… | [Transaction Context Propagation](#transaction-context-propagation) | Set a specific [evaluation context](/docs/reference/concepts/evaluation-context) for a transaction (e.g. an HTTP request or a thread) | +| βœ… | [Extending](#extending) | Extend OpenFeature with custom providers and hooks. | Implemented: βœ… | In-progress: ⚠️ | Not implemented yet: ❌ @@ -235,6 +236,86 @@ def on_provider_ready(event_details: EventDetails): client.add_handler(ProviderEvent.PROVIDER_READY, on_provider_ready) ``` +### Transaction Context Propagation + +Transaction context is a container for transaction-specific evaluation context (e.g. user id, user agent, IP). +Transaction context can be set where specific data is available (e.g. an auth service or request handler) and by using the transaction context propagator it will automatically be applied to all flag evaluations within a transaction (e.g. a request or thread). + +You can implement a different transaction context propagator by implementing the `TransactionContextPropagator` class exported by the OpenFeature SDK. +In most cases you can use `ContextVarsTransactionContextPropagator` as it works for `threads` and `asyncio` using [Context Variables](https://peps.python.org/pep-0567/). + +The following example shows a **multithreaded** Flask application using transaction context propagation to propagate the request ip and user id into request scoped transaction context. + +```python +from flask import Flask, request +from openfeature import api +from openfeature.evaluation_context import EvaluationContext +from openfeature.transaction_context import ContextVarsTransactionContextPropagator + +# Initialize the Flask app +app = Flask(__name__) + +# Set the transaction context propagator +api.set_transaction_context_propagator(ContextVarsTransactionContextPropagator()) + +# Middleware to set the transaction context +# You can call api.set_transaction_context anywhere you have information, +# you want to have available in the code-paths below the current one. +@app.before_request +def set_request_transaction_context(): + ip = request.headers.get("X-Forwarded-For", request.remote_addr) + user_id = request.headers.get("User-Id") # Assuming we're getting the user ID from a header + evaluation_context = EvaluationContext(targeting_key=user_id, attributes={"ipAddress": ip}) + api.set_transaction_context(evaluation_context) + +def create_response() -> str: + # This method can be anywhere in our code. + # The feature flag evaluation will automatically contain the transaction context merged with other context + new_response = api.get_client().get_string_value("response-message", "Hello User!") + return f"Message from server: {new_response}" + +# Example route where we use the transaction context +@app.route('/greeting') +def some_endpoint(): + return create_response() +``` + +This also works for asyncio based implementations e.g. FastApi as seen in the following example: + +```python +from fastapi import FastAPI, Request +from openfeature import api +from openfeature.evaluation_context import EvaluationContext +from openfeature.transaction_context import ContextVarsTransactionContextPropagator + +# Initialize the FastAPI app +app = FastAPI() + +# Set the transaction context propagator +api.set_transaction_context_propagator(ContextVarsTransactionContextPropagator()) + +# Middleware to set the transaction context +@app.middleware("http") +async def set_request_transaction_context(request: Request, call_next): + ip = request.headers.get("X-Forwarded-For", request.client.host) + user_id = request.headers.get("User-Id") # Assuming we're getting the user ID from a header + evaluation_context = EvaluationContext(targeting_key=user_id, attributes={"ipAddress": ip}) + api.set_transaction_context(evaluation_context) + response = await call_next(request) + return response + +def create_response() -> str: + # This method can be located anywhere in our code. + # The feature flag evaluation will automatically include the transaction context merged with other context. + new_response = api.get_client().get_string_value("response-message", "Hello User!") + return f"Message from server: {new_response}" + +# Example route where we use the transaction context +@app.get('/greeting') +async def some_endpoint(): + return create_response() +``` + ### Shutdown The OpenFeature API provides a shutdown function to perform a cleanup of all registered providers. This should only be called when your application is in the process of shutting down. diff --git a/openfeature/api.py b/openfeature/api.py index c95d10ac..c7830204 100644 --- a/openfeature/api.py +++ b/openfeature/api.py @@ -12,6 +12,10 @@ from openfeature.provider import FeatureProvider from openfeature.provider._registry import provider_registry from openfeature.provider.metadata import Metadata +from openfeature.transaction_context import ( + NoOpTransactionContextPropagator, + TransactionContextPropagator, +) __all__ = [ "get_client", @@ -20,6 +24,9 @@ "get_provider_metadata", "get_evaluation_context", "set_evaluation_context", + "set_transaction_context_propagator", + "get_transaction_context", + "set_transaction_context", "add_hooks", "clear_hooks", "get_hooks", @@ -29,6 +36,7 @@ ] _evaluation_context = EvaluationContext() +_evaluation_transaction_context_propagator = NoOpTransactionContextPropagator() _hooks: typing.List[Hook] = [] @@ -68,6 +76,24 @@ def set_evaluation_context(evaluation_context: EvaluationContext) -> None: _evaluation_context = evaluation_context +def set_transaction_context_propagator( + transaction_context_propagator: TransactionContextPropagator, +) -> None: + global _evaluation_transaction_context_propagator + _evaluation_transaction_context_propagator = transaction_context_propagator + + +def get_transaction_context() -> EvaluationContext: + return _evaluation_transaction_context_propagator.get_transaction_context() + + +def set_transaction_context(evaluation_context: EvaluationContext) -> None: + global _evaluation_transaction_context_propagator + _evaluation_transaction_context_propagator.set_transaction_context( + evaluation_context + ) + + def add_hooks(hooks: typing.List[Hook]) -> None: global _hooks _hooks = _hooks + hooks diff --git a/openfeature/client.py b/openfeature/client.py index 9e4518ec..1edfca63 100644 --- a/openfeature/client.py +++ b/openfeature/client.py @@ -335,9 +335,10 @@ def evaluate_flag_details( # noqa: PLR0915 ) invocation_context = invocation_context.merge(ctx2=evaluation_context) - # Requirement 3.2.2 merge: API.context->client.context->invocation.context + # Requirement 3.2.2 merge: API.context->transaction.context->client.context->invocation.context merged_context = ( api.get_evaluation_context() + .merge(api.get_transaction_context()) .merge(self.context) .merge(invocation_context) ) diff --git a/openfeature/transaction_context/__init__.py b/openfeature/transaction_context/__init__.py new file mode 100644 index 00000000..e9659cb2 --- /dev/null +++ b/openfeature/transaction_context/__init__.py @@ -0,0 +1,15 @@ +from openfeature.transaction_context.context_var_transaction_context_propagator import ( + ContextVarsTransactionContextPropagator, +) +from openfeature.transaction_context.no_op_transaction_context_propagator import ( + NoOpTransactionContextPropagator, +) +from openfeature.transaction_context.transaction_context_propagator import ( + TransactionContextPropagator, +) + +__all__ = [ + "TransactionContextPropagator", + "NoOpTransactionContextPropagator", + "ContextVarsTransactionContextPropagator", +] diff --git a/openfeature/transaction_context/context_var_transaction_context_propagator.py b/openfeature/transaction_context/context_var_transaction_context_propagator.py new file mode 100644 index 00000000..e57687cb --- /dev/null +++ b/openfeature/transaction_context/context_var_transaction_context_propagator.py @@ -0,0 +1,18 @@ +from contextvars import ContextVar + +from openfeature.evaluation_context import EvaluationContext +from openfeature.transaction_context.transaction_context_propagator import ( + TransactionContextPropagator, +) + +_transaction_context_var: ContextVar[EvaluationContext] = ContextVar( + "transaction_context", default=EvaluationContext() +) + + +class ContextVarsTransactionContextPropagator(TransactionContextPropagator): + def get_transaction_context(self) -> EvaluationContext: + return _transaction_context_var.get() + + def set_transaction_context(self, transaction_context: EvaluationContext) -> None: + _transaction_context_var.set(transaction_context) diff --git a/openfeature/transaction_context/no_op_transaction_context_propagator.py b/openfeature/transaction_context/no_op_transaction_context_propagator.py new file mode 100644 index 00000000..22a5a3f1 --- /dev/null +++ b/openfeature/transaction_context/no_op_transaction_context_propagator.py @@ -0,0 +1,12 @@ +from openfeature.evaluation_context import EvaluationContext +from openfeature.transaction_context.transaction_context_propagator import ( + TransactionContextPropagator, +) + + +class NoOpTransactionContextPropagator(TransactionContextPropagator): + def get_transaction_context(self) -> EvaluationContext: + return EvaluationContext() + + def set_transaction_context(self, transaction_context: EvaluationContext) -> None: + pass diff --git a/openfeature/transaction_context/transaction_context_propagator.py b/openfeature/transaction_context/transaction_context_propagator.py new file mode 100644 index 00000000..d651ab0c --- /dev/null +++ b/openfeature/transaction_context/transaction_context_propagator.py @@ -0,0 +1,16 @@ +from abc import ABC, abstractmethod +from typing import TypeVar + +from openfeature.evaluation_context import EvaluationContext + +T = TypeVar("T", bound="TransactionContextPropagator") + + +class TransactionContextPropagator(ABC): + @abstractmethod + def get_transaction_context(self) -> EvaluationContext: + pass + + @abstractmethod + def set_transaction_context(self, transaction_context: EvaluationContext) -> None: + pass diff --git a/pyproject.toml b/pyproject.toml index 40887d9d..6d37bd88 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,7 @@ dependencies = [ "behave", "coverage[toml]>=6.5", "pytest", + "pytest-asyncio" ] [tool.hatch.envs.default.scripts] diff --git a/tests/test_transaction_context.py b/tests/test_transaction_context.py new file mode 100644 index 00000000..bef2825f --- /dev/null +++ b/tests/test_transaction_context.py @@ -0,0 +1,172 @@ +import asyncio +import threading +from unittest.mock import MagicMock + +import pytest + +from openfeature.api import ( + get_transaction_context, + set_transaction_context, + set_transaction_context_propagator, +) +from openfeature.evaluation_context import EvaluationContext +from openfeature.transaction_context import ( + ContextVarsTransactionContextPropagator, + NoOpTransactionContextPropagator, + TransactionContextPropagator, +) +from openfeature.transaction_context.context_var_transaction_context_propagator import ( + _transaction_context_var, +) + + +# Test cases +def test_should_return_default_evaluation_context_with_noop_propagator(): + # Given + set_transaction_context_propagator(NoOpTransactionContextPropagator()) + + # When + context = get_transaction_context() + + # Then + assert isinstance(context, EvaluationContext) + assert context.attributes == {} + + +def test_should_set_and_get_custom_transaction_context(): + # Given + set_transaction_context_propagator(ContextVarsTransactionContextPropagator()) + evaluation_context = EvaluationContext("custom_key", {"attr1": "val1"}) + + # When + set_transaction_context(evaluation_context) + + # Then + context = get_transaction_context() + assert context.targeting_key == "custom_key" + assert context.attributes == {"attr1": "val1"} + + +def test_should_override_propagator_and_reset_context(): + # Given + custom_propagator = MagicMock(spec=TransactionContextPropagator) + default_context = EvaluationContext() + + set_transaction_context_propagator(custom_propagator) + + # When + set_transaction_context_propagator(NoOpTransactionContextPropagator()) + + # Then + assert get_transaction_context() == default_context + + +def test_should_call_set_transaction_context_on_propagator(): + # Given + custom_propagator = MagicMock(spec=TransactionContextPropagator) + evaluation_context = EvaluationContext("custom_key", {"attr1": "val1"}) + set_transaction_context_propagator(custom_propagator) + + # When + set_transaction_context(evaluation_context) + + # Then + custom_propagator.set_transaction_context.assert_called_with(evaluation_context) + + +def test_should_return_default_context_with_noop_propagator_set(): + # Given + noop_propagator = NoOpTransactionContextPropagator() + + set_transaction_context_propagator(noop_propagator) + + # When + context = get_transaction_context() + + # Then + assert context == EvaluationContext() + + +def test_should_propagate_event_when_context_set(): + # Given + custom_propagator = ContextVarsTransactionContextPropagator() + set_transaction_context_propagator(custom_propagator) + evaluation_context = EvaluationContext("custom_key", {"attr1": "val1"}) + + # When + set_transaction_context(evaluation_context) + + # Then + assert _transaction_context_var.get().targeting_key == "custom_key" + assert _transaction_context_var.get().attributes == {"attr1": "val1"} + + +def test_context_vars_transaction_context_propagator_multiple_threads(): + # Given + context_var_propagator = ContextVarsTransactionContextPropagator() + set_transaction_context_propagator(context_var_propagator) + + number_of_threads = 3 + barrier = threading.Barrier(number_of_threads) + + def thread_func(context_value, result_list, index): + context = EvaluationContext( + f"context_{context_value}", {"thread": context_value} + ) + set_transaction_context(context) + barrier.wait() + result_list[index] = get_transaction_context() + + results = [None] * number_of_threads + threads = [] + + # When + for i in range(3): + thread = threading.Thread(target=thread_func, args=(i, results, i)) + threads.append(thread) + thread.start() + + for thread in threads: + thread.join() + + # Then + for i in range(3): + assert results[i].targeting_key == f"context_{i}" + assert results[i].attributes == {"thread": i} + + +@pytest.mark.asyncio +async def test_context_vars_transaction_context_propagator_asyncio(): + # Given + context_var_propagator = ContextVarsTransactionContextPropagator() + set_transaction_context_propagator(context_var_propagator) + + number_of_tasks = 3 + event = asyncio.Event() + ready_count = 0 + + async def async_func(context_value, results, index): + nonlocal ready_count + context = EvaluationContext( + f"context_{context_value}", {"async": context_value} + ) + set_transaction_context(context) + + ready_count += 1 # Increment the ready count + if ready_count == number_of_tasks: + event.set() # Set the event when all tasks are ready + + await event.wait() # Wait for the event to be set + results[index] = get_transaction_context() + + # Placeholder for results + results = [None] * number_of_tasks + + # When + tasks = [async_func(i, results, i) for i in range(number_of_tasks)] + await asyncio.gather(*tasks) + + # Then + for i in range(number_of_tasks): + assert results[i].targeting_key == f"context_{i}" + assert results[i].attributes == {"async": i} From 2a21b9d9a501075b8106b697c7536790ed0d00b7 Mon Sep 17 00:00:00 2001 From: Lukas Reining Date: Sat, 16 Nov 2024 15:46:04 +0100 Subject: [PATCH 2/7] fix: lint issues Signed-off-by: Lukas Reining --- README.md | 4 ++-- .../transaction_context_propagator.py | 14 ++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6a491aad..7653bc0a 100644 --- a/README.md +++ b/README.md @@ -259,7 +259,7 @@ app = Flask(__name__) api.set_transaction_context_propagator(ContextVarsTransactionContextPropagator()) # Middleware to set the transaction context -# You can call api.set_transaction_context anywhere you have information, +# You can call api.set_transaction_context anywhere you have information, # you want to have available in the code-paths below the current one. @app.before_request def set_request_transaction_context(): @@ -269,7 +269,7 @@ def set_request_transaction_context(): api.set_transaction_context(evaluation_context) def create_response() -> str: - # This method can be anywhere in our code. + # This method can be anywhere in our code. # The feature flag evaluation will automatically contain the transaction context merged with other context new_response = api.get_client().get_string_value("response-message", "Hello User!") return f"Message from server: {new_response}" diff --git a/openfeature/transaction_context/transaction_context_propagator.py b/openfeature/transaction_context/transaction_context_propagator.py index d651ab0c..07c87a77 100644 --- a/openfeature/transaction_context/transaction_context_propagator.py +++ b/openfeature/transaction_context/transaction_context_propagator.py @@ -1,4 +1,4 @@ -from abc import ABC, abstractmethod +import typing from typing import TypeVar from openfeature.evaluation_context import EvaluationContext @@ -6,11 +6,9 @@ T = TypeVar("T", bound="TransactionContextPropagator") -class TransactionContextPropagator(ABC): - @abstractmethod - def get_transaction_context(self) -> EvaluationContext: - pass +class TransactionContextPropagator(typing.Protocol): + def get_transaction_context(self) -> EvaluationContext: ... - @abstractmethod - def set_transaction_context(self, transaction_context: EvaluationContext) -> None: - pass + def set_transaction_context( + self, transaction_context: EvaluationContext + ) -> None: ... From 67e43680fc687d0c17224c33ef24a1de1b65fe3d Mon Sep 17 00:00:00 2001 From: Lukas Reining Date: Sat, 16 Nov 2024 16:15:00 +0100 Subject: [PATCH 3/7] feat: add tests for context merging Signed-off-by: Lukas Reining --- openfeature/api.py | 4 +- ...text_var_transaction_context_propagator.py | 12 ++--- tests/test_client.py | 49 ++++++++++++++++++- tests/test_transaction_context.py | 11 +++-- 4 files changed, 63 insertions(+), 13 deletions(-) diff --git a/openfeature/api.py b/openfeature/api.py index c7830204..847f7daa 100644 --- a/openfeature/api.py +++ b/openfeature/api.py @@ -36,7 +36,9 @@ ] _evaluation_context = EvaluationContext() -_evaluation_transaction_context_propagator = NoOpTransactionContextPropagator() +_evaluation_transaction_context_propagator: TransactionContextPropagator = ( + NoOpTransactionContextPropagator() +) _hooks: typing.List[Hook] = [] diff --git a/openfeature/transaction_context/context_var_transaction_context_propagator.py b/openfeature/transaction_context/context_var_transaction_context_propagator.py index e57687cb..1abc04fa 100644 --- a/openfeature/transaction_context/context_var_transaction_context_propagator.py +++ b/openfeature/transaction_context/context_var_transaction_context_propagator.py @@ -5,14 +5,14 @@ TransactionContextPropagator, ) -_transaction_context_var: ContextVar[EvaluationContext] = ContextVar( - "transaction_context", default=EvaluationContext() -) - class ContextVarsTransactionContextPropagator(TransactionContextPropagator): + _transaction_context_var: ContextVar[EvaluationContext] = ContextVar( + "transaction_context", default=EvaluationContext() + ) + def get_transaction_context(self) -> EvaluationContext: - return _transaction_context_var.get() + return self._transaction_context_var.get() def set_transaction_context(self, transaction_context: EvaluationContext) -> None: - _transaction_context_var.set(transaction_context) + self._transaction_context_var.set(transaction_context) diff --git a/tests/test_client.py b/tests/test_client.py index b51c460c..caab2d36 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,12 +1,14 @@ import time import uuid from concurrent.futures import ThreadPoolExecutor -from unittest.mock import MagicMock +from unittest.mock import MagicMock, Mock import pytest +from openfeature import api from openfeature.api import add_hooks, clear_hooks, get_client, set_provider from openfeature.client import OpenFeatureClient +from openfeature.evaluation_context import EvaluationContext from openfeature.event import EventDetails, ProviderEvent, ProviderEventDetails from openfeature.exception import ErrorCode, OpenFeatureError from openfeature.flag_evaluation import FlagResolutionDetails, Reason @@ -14,6 +16,7 @@ from openfeature.provider import FeatureProvider, ProviderStatus from openfeature.provider.in_memory_provider import InMemoryFlag, InMemoryProvider from openfeature.provider.no_op_provider import NoOpProvider +from openfeature.transaction_context import ContextVarsTransactionContextPropagator @pytest.mark.parametrize( @@ -384,3 +387,47 @@ def emit_events_task(): f2 = executor.submit(emit_events_task) f1.result() f2.result() + + +def test_client_should_merge_contexts(): + api.clear_hooks() + api.set_transaction_context_propagator(ContextVarsTransactionContextPropagator()) + + provider = NoOpProvider() + provider.resolve_boolean_details = MagicMock(wraps=provider.resolve_boolean_details) + api.set_provider(provider) + + # Global evaluation context + global_context = EvaluationContext( + targeting_key="global", attributes={"global_attr": "global_value"} + ) + api.set_evaluation_context(global_context) + + # Transaction context + transaction_context = EvaluationContext( + targeting_key="transaction", + attributes={"transaction_attr": "transaction_value"}, + ) + api.set_transaction_context(transaction_context) + + # Client-specific context + client_context = EvaluationContext( + targeting_key="client", attributes={"client_attr": "client_value"} + ) + client = OpenFeatureClient(domain=None, version=None, context=client_context) + + # Invocation-specific context + invocation_context = EvaluationContext( + targeting_key="invocation", attributes={"invocation_attr": "invocation_value"} + ) + client.get_boolean_details("flag", False, invocation_context) + + # Retrieve the call arguments + args, kwargs = provider.resolve_boolean_details.call_args + flag_key, default_value, context = args + + assert context.targeting_key == "invocation" # Last one in the merge chain + assert context.attributes["global_attr"] == "global_value" + assert context.attributes["transaction_attr"] == "transaction_value" + assert context.attributes["client_attr"] == "client_value" + assert context.attributes["invocation_attr"] == "invocation_value" diff --git a/tests/test_transaction_context.py b/tests/test_transaction_context.py index bef2825f..84d1d7b0 100644 --- a/tests/test_transaction_context.py +++ b/tests/test_transaction_context.py @@ -15,9 +15,6 @@ NoOpTransactionContextPropagator, TransactionContextPropagator, ) -from openfeature.transaction_context.context_var_transaction_context_propagator import ( - _transaction_context_var, -) # Test cases @@ -97,8 +94,12 @@ def test_should_propagate_event_when_context_set(): set_transaction_context(evaluation_context) # Then - assert _transaction_context_var.get().targeting_key == "custom_key" - assert _transaction_context_var.get().attributes == {"attr1": "val1"} + assert ( + custom_propagator._transaction_context_var.get().targeting_key == "custom_key" + ) + assert custom_propagator._transaction_context_var.get().attributes == { + "attr1": "val1" + } def test_context_vars_transaction_context_propagator_multiple_threads(): From 72860427261f49cc1e6bd7c89f09b32c8ef27f0d Mon Sep 17 00:00:00 2001 From: Lukas Reining Date: Sat, 16 Nov 2024 16:48:43 +0100 Subject: [PATCH 4/7] feat: fix pre-commit checks Signed-off-by: Lukas Reining --- tests/test_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_client.py b/tests/test_client.py index caab2d36..7f0ca461 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,7 +1,7 @@ import time import uuid from concurrent.futures import ThreadPoolExecutor -from unittest.mock import MagicMock, Mock +from unittest.mock import MagicMock import pytest From 7bf1cdc09d58baa279c4177681ce628660859fed Mon Sep 17 00:00:00 2001 From: Lukas Reining Date: Mon, 18 Nov 2024 13:20:34 +0100 Subject: [PATCH 5/7] feat: use elipsis instead of pass Signed-off-by: Lukas Reining --- .../no_op_transaction_context_propagator.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/openfeature/transaction_context/no_op_transaction_context_propagator.py b/openfeature/transaction_context/no_op_transaction_context_propagator.py index 22a5a3f1..3028b877 100644 --- a/openfeature/transaction_context/no_op_transaction_context_propagator.py +++ b/openfeature/transaction_context/no_op_transaction_context_propagator.py @@ -8,5 +8,6 @@ class NoOpTransactionContextPropagator(TransactionContextPropagator): def get_transaction_context(self) -> EvaluationContext: return EvaluationContext() - def set_transaction_context(self, transaction_context: EvaluationContext) -> None: - pass + def set_transaction_context( + self, transaction_context: EvaluationContext + ) -> None: ... From b9d427cd7197617162dd87ef2143c622fc82f5c2 Mon Sep 17 00:00:00 2001 From: Lukas Reining Date: Tue, 19 Nov 2024 10:26:36 +0100 Subject: [PATCH 6/7] Update openfeature/transaction_context/no_op_transaction_context_propagator.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Anton GrΓΌbel Signed-off-by: Lukas Reining --- .../no_op_transaction_context_propagator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openfeature/transaction_context/no_op_transaction_context_propagator.py b/openfeature/transaction_context/no_op_transaction_context_propagator.py index 3028b877..01069ffe 100644 --- a/openfeature/transaction_context/no_op_transaction_context_propagator.py +++ b/openfeature/transaction_context/no_op_transaction_context_propagator.py @@ -10,4 +10,5 @@ def get_transaction_context(self) -> EvaluationContext: def set_transaction_context( self, transaction_context: EvaluationContext - ) -> None: ... + ) -> None: + pass From bb0e8bc8a25574d36aa9ce42e77e6a3b41d665ae Mon Sep 17 00:00:00 2001 From: Lukas Reining Date: Tue, 19 Nov 2024 10:29:49 +0100 Subject: [PATCH 7/7] feat: pr feedback Signed-off-by: Lukas Reining --- openfeature/api.py | 4 ++-- openfeature/transaction_context/__init__.py | 4 ---- .../no_op_transaction_context_propagator.py | 6 ++---- .../transaction_context/transaction_context_propagator.py | 3 --- tests/test_transaction_context.py | 4 +++- 5 files changed, 7 insertions(+), 14 deletions(-) diff --git a/openfeature/api.py b/openfeature/api.py index 847f7daa..c7d29c48 100644 --- a/openfeature/api.py +++ b/openfeature/api.py @@ -12,9 +12,9 @@ from openfeature.provider import FeatureProvider from openfeature.provider._registry import provider_registry from openfeature.provider.metadata import Metadata -from openfeature.transaction_context import ( +from openfeature.transaction_context import TransactionContextPropagator +from openfeature.transaction_context.no_op_transaction_context_propagator import ( NoOpTransactionContextPropagator, - TransactionContextPropagator, ) __all__ = [ diff --git a/openfeature/transaction_context/__init__.py b/openfeature/transaction_context/__init__.py index e9659cb2..e97fd36f 100644 --- a/openfeature/transaction_context/__init__.py +++ b/openfeature/transaction_context/__init__.py @@ -1,15 +1,11 @@ from openfeature.transaction_context.context_var_transaction_context_propagator import ( ContextVarsTransactionContextPropagator, ) -from openfeature.transaction_context.no_op_transaction_context_propagator import ( - NoOpTransactionContextPropagator, -) from openfeature.transaction_context.transaction_context_propagator import ( TransactionContextPropagator, ) __all__ = [ "TransactionContextPropagator", - "NoOpTransactionContextPropagator", "ContextVarsTransactionContextPropagator", ] diff --git a/openfeature/transaction_context/no_op_transaction_context_propagator.py b/openfeature/transaction_context/no_op_transaction_context_propagator.py index 01069ffe..22a5a3f1 100644 --- a/openfeature/transaction_context/no_op_transaction_context_propagator.py +++ b/openfeature/transaction_context/no_op_transaction_context_propagator.py @@ -8,7 +8,5 @@ class NoOpTransactionContextPropagator(TransactionContextPropagator): def get_transaction_context(self) -> EvaluationContext: return EvaluationContext() - def set_transaction_context( - self, transaction_context: EvaluationContext - ) -> None: - pass + def set_transaction_context(self, transaction_context: EvaluationContext) -> None: + pass diff --git a/openfeature/transaction_context/transaction_context_propagator.py b/openfeature/transaction_context/transaction_context_propagator.py index 07c87a77..9a54367b 100644 --- a/openfeature/transaction_context/transaction_context_propagator.py +++ b/openfeature/transaction_context/transaction_context_propagator.py @@ -1,10 +1,7 @@ import typing -from typing import TypeVar from openfeature.evaluation_context import EvaluationContext -T = TypeVar("T", bound="TransactionContextPropagator") - class TransactionContextPropagator(typing.Protocol): def get_transaction_context(self) -> EvaluationContext: ... diff --git a/tests/test_transaction_context.py b/tests/test_transaction_context.py index 84d1d7b0..1da57c28 100644 --- a/tests/test_transaction_context.py +++ b/tests/test_transaction_context.py @@ -12,9 +12,11 @@ from openfeature.evaluation_context import EvaluationContext from openfeature.transaction_context import ( ContextVarsTransactionContextPropagator, - NoOpTransactionContextPropagator, TransactionContextPropagator, ) +from openfeature.transaction_context.no_op_transaction_context_propagator import ( + NoOpTransactionContextPropagator, +) # Test cases