From e821e99014743647431ec249141c2efe0c4acfa1 Mon Sep 17 00:00:00 2001 From: Matt Metcalf Date: Thu, 5 Nov 2026 09:24:08 -0800 Subject: [PATCH] Fixing Type Hints on is enabled/get varient --- CHANGELOG.md | 7 ++++ featuremanagement/_featuremanager.py | 27 +++++++++---- featuremanagement/aio/_featuremanager.py | 27 +++++++++---- tests/test_typing.py | 50 ++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 tests/test_typing.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d9e6ae..d1f0909 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. \ No newline at end of file diff --git a/featuremanagement/_featuremanager.py b/featuremanagement/_featuremanager.py index 4d829a5..b3fa469 100644 --- a/featuremanagement/_featuremanager.py +++ b/featuremanagement/_featuremanager.py @@ -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 @@ -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 """ @@ -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 """ @@ -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 """ @@ -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 """ diff --git a/featuremanagement/aio/_featuremanager.py b/featuremanagement/aio/_featuremanager.py index 4a7b475..c480a75 100644 --- a/featuremanagement/aio/_featuremanager.py +++ b/featuremanagement/aio/_featuremanager.py @@ -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 @@ -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 """ @@ -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 """ @@ -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 """ @@ -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 """ diff --git a/tests/test_typing.py b/tests/test_typing.py new file mode 100644 index 0000000..f168e9c --- /dev/null +++ b/tests/test_typing.py @@ -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