Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Release History

## Unreleased

### Bugs Fixed

- Corrected sync and async `FeatureManager.is_enabled` and `FeatureManager.get_variant` overloads to accept calls
without a user identifier and calls with a positional `TargetingContext`, including custom keyword arguments.

See [Release Notes](https://github.com/Azure/AppConfiguration/blob/main/releaseNotes/PythonFeatureManagement.md) for the full release history.
27 changes: 19 additions & 8 deletions featuremanagement/_featuremanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""Synchronous feature manager implementation."""

import logging
from typing import cast, overload, Any, Optional, Dict, Mapping, List, Tuple
from typing import cast, overload, Any, Optional, Dict, Mapping, List, Tuple, Union
from ._defaultfilters import TimeWindowFilter, TargetingFilter
from ._featurefilters import FeatureFilter
from ._models import EvaluationEvent, Variant, TargetingContext
Expand Down Expand Up @@ -44,13 +44,17 @@ def __init__(self, configuration: Mapping[str, Any], **kwargs: Any):
raise ValueError("Custom filter must be a subclass of FeatureFilter")
self._filters[feature_filter.name] = feature_filter

@overload # type: ignore
def is_enabled(self, feature_flag_id: str, user_id: str, **kwargs: Any) -> bool:
@overload
def is_enabled(self, feature_flag_id: str, **kwargs: Any) -> bool: ...

@overload
def is_enabled(self, feature_flag_id: str, user_id: Union[str, TargetingContext], **kwargs: Any) -> bool:
"""
Determine if the feature flag is enabled for the given context.

:param str feature_flag_id: Name of the feature flag.
:param str user_id: User identifier.
:param user_id: User identifier or targeting context, passed positionally.
:type user_id: str or TargetingContext
:return: True if the feature flag is enabled for the given context.
:rtype: bool
"""
Expand All @@ -60,6 +64,7 @@ def is_enabled(self, feature_flag_id: str, *args: Any, **kwargs: Any) -> bool:
Determine if the feature flag is enabled for the given context.

:param str feature_flag_id: Name of the feature flag.
:param args: Optional positional user identifier or TargetingContext.
:return: True if the feature flag is enabled for the given context.
:rtype: bool
"""
Expand All @@ -76,13 +81,19 @@ def is_enabled(self, feature_flag_id: str, *args: Any, **kwargs: Any) -> bool:
self._on_feature_evaluated(result)
return result.enabled

@overload # type: ignore
def get_variant(self, feature_flag_id: str, user_id: str, **kwargs: Any) -> Optional[Variant]:
@overload
def get_variant(self, feature_flag_id: str, **kwargs: Any) -> Optional[Variant]: ...

@overload
def get_variant(
self, feature_flag_id: str, user_id: Union[str, TargetingContext], **kwargs: Any
) -> Optional[Variant]:
"""
Determine the variant for the given context.

:param str feature_flag_id: Name of the feature flag.
:param str user_id: User identifier.
:param user_id: User identifier or targeting context, passed positionally.
:type user_id: str or TargetingContext
:return: return: Variant instance.
:rtype: Variant
"""
Expand All @@ -92,7 +103,7 @@ def get_variant(self, feature_flag_id: str, *args: Any, **kwargs: Any) -> Option
Determine the variant for the given context.

:param str feature_flag_id: Name of the feature flag
:keyword TargetingContext targeting_context: Targeting context.
:param args: Optional positional user identifier or TargetingContext.
:return: Variant instance.
:rtype: Variant
"""
Expand Down
27 changes: 19 additions & 8 deletions featuremanagement/aio/_featuremanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import inspect
import logging
from typing import cast, overload, Any, Optional, Dict, Mapping, List, Tuple
from typing import cast, overload, Any, Optional, Dict, Mapping, List, Tuple, Union
from ._defaultfilters import TimeWindowFilter, TargetingFilter
from ._featurefilters import FeatureFilter
from .._models import EvaluationEvent, Variant, TargetingContext
Expand Down Expand Up @@ -45,13 +45,17 @@ def __init__(self, configuration: Mapping[str, Any], **kwargs: Any):
raise ValueError("Custom filter must be a subclass of FeatureFilter")
self._filters[feature_filter.name] = feature_filter

@overload # type: ignore
async def is_enabled(self, feature_flag_id: str, user_id: str, **kwargs: Any) -> bool:
@overload
async def is_enabled(self, feature_flag_id: str, **kwargs: Any) -> bool: ...

@overload
async def is_enabled(self, feature_flag_id: str, user_id: Union[str, TargetingContext], **kwargs: Any) -> bool:
"""
Determine if the feature flag is enabled for the given context.

:param str feature_flag_id: Name of the feature flag.
:param str user_id: User identifier.
:param user_id: User identifier or targeting context, passed positionally.
:type user_id: str or TargetingContext
:return: True if the feature flag is enabled for the given context.
:rtype: bool
"""
Expand All @@ -61,6 +65,7 @@ async def is_enabled(self, feature_flag_id: str, *args: Any, **kwargs: Any) -> b
Determine if the feature flag is enabled for the given context.

:param str feature_flag_id: Name of the feature flag.
:param args: Optional positional user identifier or TargetingContext.
:return: True if the feature flag is enabled for the given context.
:rtype: bool
"""
Expand All @@ -80,13 +85,19 @@ async def is_enabled(self, feature_flag_id: str, *args: Any, **kwargs: Any) -> b
self._on_feature_evaluated(result)
return result.enabled

@overload # type: ignore
async def get_variant(self, feature_flag_id: str, user_id: str, **kwargs: Any) -> Optional[Variant]:
@overload
async def get_variant(self, feature_flag_id: str, **kwargs: Any) -> Optional[Variant]: ...

@overload
async def get_variant(
self, feature_flag_id: str, user_id: Union[str, TargetingContext], **kwargs: Any
) -> Optional[Variant]:
"""
Determine the variant for the given context.

:param str feature_flag_id: Name of the feature flag.
:param str user_id: User identifier.
:param user_id: User identifier or targeting context, passed positionally.
:type user_id: str or TargetingContext
:return: return: Variant instance.
:rtype: Variant
"""
Expand All @@ -96,7 +107,7 @@ async def get_variant(self, feature_flag_id: str, *args: Any, **kwargs: Any) ->
Determine the variant for the given context.

:param str feature_flag_id: Name of the feature flag
:keyword TargetingContext targeting_context: Targeting context.
:param args: Optional positional user identifier or TargetingContext.
:return: Variant instance.
:rtype: Variant
"""
Expand Down
50 changes: 50 additions & 0 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""Static typing regression tests for feature manager APIs."""

import subprocess
import sys
from pathlib import Path

import pytest


@pytest.mark.parametrize("asynchronous", [False, True], ids=["sync", "async"])
def test_feature_manager_call_types(tmp_path: Path, asynchronous: bool) -> None:
"""Verify supported calls retain their exact return types."""
module = "featuremanagement.aio" if asynchronous else "featuremanagement"
definition = "async def" if asynchronous else "def"
await_expression = "await " if asynchronous else ""
calls = [
'"Alpha"',
'feature_flag_id="Alpha"',
'"Alpha", custom_context=42',
'"Alpha", "user"',
'"Alpha", "user", custom_context=42',
'"Alpha", context',
'"Alpha", context, custom_context=42',
]
source = (
"from typing import Optional\n"
"from typing_extensions import assert_type\n"
"from featuremanagement import TargetingContext, Variant\n"
f"from {module} import FeatureManager\n\n"
f"{definition} check(manager: FeatureManager, context: TargetingContext) -> None:\n"
)
for arguments in calls:
source += f" assert_type({await_expression}manager.is_enabled({arguments}), bool)\n"
source += f" assert_type({await_expression}manager.get_variant({arguments}), Optional[Variant])\n"

fixture = tmp_path / "feature_manager_typing.py"
fixture.write_text(source, encoding="utf-8")
result = subprocess.run(
[sys.executable, "-m", "mypy", "--strict", "--no-incremental", str(fixture)],
cwd=Path(__file__).resolve().parents[1],
capture_output=True,
text=True,
check=False,
)
assert result.returncode == 0, result.stdout + result.stderr
Loading