Skip to content

Commit 810a3ef

Browse files
authored
Fixing Type Hints on is enabled/get varient (#78)
1 parent 5b31042 commit 810a3ef

4 files changed

Lines changed: 95 additions & 16 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
# Release History
22

3+
## Unreleased
4+
5+
### Bugs Fixed
6+
7+
- Corrected sync and async `FeatureManager.is_enabled` and `FeatureManager.get_variant` overloads to accept calls
8+
without a user identifier and calls with a positional `TargetingContext`, including custom keyword arguments.
9+
310
See [Release Notes](https://github.com/Azure/AppConfiguration/blob/main/releaseNotes/PythonFeatureManagement.md) for the full release history.

‎featuremanagement/_featuremanager.py‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""Synchronous feature manager implementation."""
77

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

47-
@overload # type: ignore
48-
def is_enabled(self, feature_flag_id: str, user_id: str, **kwargs: Any) -> bool:
47+
@overload
48+
def is_enabled(self, feature_flag_id: str, **kwargs: Any) -> bool: ...
49+
50+
@overload
51+
def is_enabled(self, feature_flag_id: str, user_id: Union[str, TargetingContext], **kwargs: Any) -> bool:
4952
"""
5053
Determine if the feature flag is enabled for the given context.
5154
5255
:param str feature_flag_id: Name of the feature flag.
53-
:param str user_id: User identifier.
56+
:param user_id: User identifier or targeting context, passed positionally.
57+
:type user_id: str or TargetingContext
5458
:return: True if the feature flag is enabled for the given context.
5559
:rtype: bool
5660
"""
@@ -60,6 +64,7 @@ def is_enabled(self, feature_flag_id: str, *args: Any, **kwargs: Any) -> bool:
6064
Determine if the feature flag is enabled for the given context.
6165
6266
:param str feature_flag_id: Name of the feature flag.
67+
:param args: Optional positional user identifier or TargetingContext.
6368
:return: True if the feature flag is enabled for the given context.
6469
:rtype: bool
6570
"""
@@ -76,13 +81,19 @@ def is_enabled(self, feature_flag_id: str, *args: Any, **kwargs: Any) -> bool:
7681
self._on_feature_evaluated(result)
7782
return result.enabled
7883

79-
@overload # type: ignore
80-
def get_variant(self, feature_flag_id: str, user_id: str, **kwargs: Any) -> Optional[Variant]:
84+
@overload
85+
def get_variant(self, feature_flag_id: str, **kwargs: Any) -> Optional[Variant]: ...
86+
87+
@overload
88+
def get_variant(
89+
self, feature_flag_id: str, user_id: Union[str, TargetingContext], **kwargs: Any
90+
) -> Optional[Variant]:
8191
"""
8292
Determine the variant for the given context.
8393
8494
:param str feature_flag_id: Name of the feature flag.
85-
:param str user_id: User identifier.
95+
:param user_id: User identifier or targeting context, passed positionally.
96+
:type user_id: str or TargetingContext
8697
:return: return: Variant instance.
8798
:rtype: Variant
8899
"""
@@ -92,7 +103,7 @@ def get_variant(self, feature_flag_id: str, *args: Any, **kwargs: Any) -> Option
92103
Determine the variant for the given context.
93104
94105
:param str feature_flag_id: Name of the feature flag
95-
:keyword TargetingContext targeting_context: Targeting context.
106+
:param args: Optional positional user identifier or TargetingContext.
96107
:return: Variant instance.
97108
:rtype: Variant
98109
"""

‎featuremanagement/aio/_featuremanager.py‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

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

48-
@overload # type: ignore
49-
async def is_enabled(self, feature_flag_id: str, user_id: str, **kwargs: Any) -> bool:
48+
@overload
49+
async def is_enabled(self, feature_flag_id: str, **kwargs: Any) -> bool: ...
50+
51+
@overload
52+
async def is_enabled(self, feature_flag_id: str, user_id: Union[str, TargetingContext], **kwargs: Any) -> bool:
5053
"""
5154
Determine if the feature flag is enabled for the given context.
5255
5356
:param str feature_flag_id: Name of the feature flag.
54-
:param str user_id: User identifier.
57+
:param user_id: User identifier or targeting context, passed positionally.
58+
:type user_id: str or TargetingContext
5559
:return: True if the feature flag is enabled for the given context.
5660
:rtype: bool
5761
"""
@@ -61,6 +65,7 @@ async def is_enabled(self, feature_flag_id: str, *args: Any, **kwargs: Any) -> b
6165
Determine if the feature flag is enabled for the given context.
6266
6367
:param str feature_flag_id: Name of the feature flag.
68+
:param args: Optional positional user identifier or TargetingContext.
6469
:return: True if the feature flag is enabled for the given context.
6570
:rtype: bool
6671
"""
@@ -80,13 +85,19 @@ async def is_enabled(self, feature_flag_id: str, *args: Any, **kwargs: Any) -> b
8085
self._on_feature_evaluated(result)
8186
return result.enabled
8287

83-
@overload # type: ignore
84-
async def get_variant(self, feature_flag_id: str, user_id: str, **kwargs: Any) -> Optional[Variant]:
88+
@overload
89+
async def get_variant(self, feature_flag_id: str, **kwargs: Any) -> Optional[Variant]: ...
90+
91+
@overload
92+
async def get_variant(
93+
self, feature_flag_id: str, user_id: Union[str, TargetingContext], **kwargs: Any
94+
) -> Optional[Variant]:
8595
"""
8696
Determine the variant for the given context.
8797
8898
:param str feature_flag_id: Name of the feature flag.
89-
:param str user_id: User identifier.
99+
:param user_id: User identifier or targeting context, passed positionally.
100+
:type user_id: str or TargetingContext
90101
:return: return: Variant instance.
91102
:rtype: Variant
92103
"""
@@ -96,7 +107,7 @@ async def get_variant(self, feature_flag_id: str, *args: Any, **kwargs: Any) ->
96107
Determine the variant for the given context.
97108
98109
:param str feature_flag_id: Name of the feature flag
99-
:keyword TargetingContext targeting_context: Targeting context.
110+
:param args: Optional positional user identifier or TargetingContext.
100111
:return: Variant instance.
101112
:rtype: Variant
102113
"""

‎tests/test_typing.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
"""Static typing regression tests for feature manager APIs."""
7+
8+
import subprocess
9+
import sys
10+
from pathlib import Path
11+
12+
import pytest
13+
14+
15+
@pytest.mark.parametrize("asynchronous", [False, True], ids=["sync", "async"])
16+
def test_feature_manager_call_types(tmp_path: Path, asynchronous: bool) -> None:
17+
"""Verify supported calls retain their exact return types."""
18+
module = "featuremanagement.aio" if asynchronous else "featuremanagement"
19+
definition = "async def" if asynchronous else "def"
20+
await_expression = "await " if asynchronous else ""
21+
calls = [
22+
'"Alpha"',
23+
'feature_flag_id="Alpha"',
24+
'"Alpha", custom_context=42',
25+
'"Alpha", "user"',
26+
'"Alpha", "user", custom_context=42',
27+
'"Alpha", context',
28+
'"Alpha", context, custom_context=42',
29+
]
30+
source = (
31+
"from typing import Optional\n"
32+
"from typing_extensions import assert_type\n"
33+
"from featuremanagement import TargetingContext, Variant\n"
34+
f"from {module} import FeatureManager\n\n"
35+
f"{definition} check(manager: FeatureManager, context: TargetingContext) -> None:\n"
36+
)
37+
for arguments in calls:
38+
source += f" assert_type({await_expression}manager.is_enabled({arguments}), bool)\n"
39+
source += f" assert_type({await_expression}manager.get_variant({arguments}), Optional[Variant])\n"
40+
41+
fixture = tmp_path / "feature_manager_typing.py"
42+
fixture.write_text(source, encoding="utf-8")
43+
result = subprocess.run(
44+
[sys.executable, "-m", "mypy", "--strict", "--no-incremental", str(fixture)],
45+
cwd=Path(__file__).resolve().parents[1],
46+
capture_output=True,
47+
text=True,
48+
check=False,
49+
)
50+
assert result.returncode == 0, result.stdout + result.stderr

0 commit comments

Comments
 (0)