Skip to content

Commit 71330d9

Browse files
committed
refactor: rename parameters and enforce keyword-only in feature gating
1 parent d8d313b commit 71330d9

2 files changed

Lines changed: 37 additions & 34 deletions

File tree

packages/google-api-core/google/api_core/feature_gating_helpers.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,25 @@ def _get_env_bool(name: str) -> Optional[bool]:
5050

5151

5252
def _has_provider(
53-
client_options: Optional[Union[Dict[str, Any], Any]], provider_key: str
53+
*, configuration: Optional[Union[Dict[str, Any], Any]], feature_key: str
5454
) -> bool:
55-
"""Checks if a specific provider key is present and not None in client_options."""
56-
if client_options is None:
55+
"""Checks if a specific feature key is present and not None in configuration."""
56+
if configuration is None:
5757
return False
5858

59-
if isinstance(client_options, dict):
60-
return client_options.get(provider_key) is not None
59+
if isinstance(configuration, dict):
60+
return configuration.get(feature_key) is not None
6161

62-
return getattr(client_options, provider_key, None) is not None
62+
return getattr(configuration, feature_key, None) is not None
6363

6464

6565
def resolve_feature_flags(
66+
*,
6667
env_var: str,
67-
provider_key: str,
68-
client_options: Optional[Union[Dict[str, Any], Any]] = None,
68+
feature_key: str,
69+
configuration: Optional[Union[Dict[str, Any], Any]] = None,
6970
) -> bool:
70-
"""Determines if a feature is enabled based on environment variables and client options.
71+
"""Determines if a feature is enabled based on environment variables and configuration.
7172
7273
Behavior depends on whether the `env_var` name contains "EXPERIMENTAL":
7374
@@ -82,8 +83,8 @@ def resolve_feature_flags(
8283
8384
Args:
8485
env_var: The name of the environment variable controlling this feature.
85-
provider_key: The key in client_options/attributes for the programmatic provider.
86-
client_options: Optional. A dictionary or object containing client configuration.
86+
feature_key: The key in configuration/attributes for the programmatic provider.
87+
configuration: Optional. A dictionary or object containing client configuration.
8788
8889
Returns:
8990
bool: True if the feature is resolved to enabled, False otherwise.
@@ -93,7 +94,9 @@ def resolve_feature_flags(
9394
"""
9495

9596
# Check for programmatic feature provider
96-
has_provider = _has_provider(client_options, provider_key)
97+
has_provider = _has_provider(
98+
configuration=configuration, feature_key=feature_key
99+
)
97100

98101
# Read environment variable
99102
env_var_setting = _get_env_bool(env_var)

packages/google-api-core/tests/unit/test_feature_gating_helpers.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ def test_resolve_feature_flags_ga_enabled_via_env(monkeypatch):
7474
# Action
7575
result = feature_gating_helpers.resolve_feature_flags(
7676
env_var="GOOGLE_SDK_PYTHON_TRACING_ENABLED",
77-
provider_key="tracer_provider",
78-
client_options=None,
77+
feature_key="tracer_provider",
78+
configuration=None,
7979
)
8080

8181
# Assertion
@@ -96,26 +96,26 @@ def test_resolve_feature_flags_exp_blocked_with_provider_fails_fast(
9696
monkeypatch.delenv(
9797
"GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED", raising=False
9898
)
99-
client_options = {"tracer_provider": object()}
99+
configuration = {"tracer_provider": object()}
100100

101101
# Action & Assertion
102102
with pytest.raises(ValueError, match="Experimental feature"):
103103
feature_gating_helpers.resolve_feature_flags(
104104
env_var="GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED",
105-
provider_key="tracer_provider",
106-
client_options=client_options,
105+
feature_key="tracer_provider",
106+
configuration=configuration,
107107
)
108108

109109

110110
def test_resolve_feature_flags_exp_enabled_with_provider(monkeypatch):
111111
"""Verify that experimental feature is enabled if the experimental environment variable is enabled and a provider is provided."""
112112
monkeypatch.setenv("GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED", "true")
113-
client_options = {"tracer_provider": object()}
113+
configuration = {"tracer_provider": object()}
114114

115115
result = feature_gating_helpers.resolve_feature_flags(
116116
env_var="GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED",
117-
provider_key="tracer_provider",
118-
client_options=client_options,
117+
feature_key="tracer_provider",
118+
configuration=configuration,
119119
)
120120
assert result is True
121121

@@ -126,8 +126,8 @@ def test_resolve_feature_flags_exp_enabled_without_provider(monkeypatch):
126126

127127
result = feature_gating_helpers.resolve_feature_flags(
128128
env_var="GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED",
129-
provider_key="tracer_provider",
130-
client_options=None,
129+
feature_key="tracer_provider",
130+
configuration=None,
131131
)
132132
assert result is True
133133

@@ -138,8 +138,8 @@ def test_resolve_feature_flags_exp_disabled_without_provider(monkeypatch):
138138

139139
result = feature_gating_helpers.resolve_feature_flags(
140140
env_var="GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED",
141-
provider_key="tracer_provider",
142-
client_options=None,
141+
feature_key="tracer_provider",
142+
configuration=None,
143143
)
144144
assert result is False
145145

@@ -148,12 +148,12 @@ def test_resolve_feature_flags_ga_enabled_via_provider(monkeypatch):
148148
"""Verify that a GA feature is enabled if a provider is provided, ignoring the environment variable."""
149149
# Env var is False, but provider is present
150150
monkeypatch.setenv("GOOGLE_SDK_PYTHON_TRACING_ENABLED", "false")
151-
client_options = {"tracer_provider": object()}
151+
configuration = {"tracer_provider": object()}
152152

153153
result = feature_gating_helpers.resolve_feature_flags(
154154
env_var="GOOGLE_SDK_PYTHON_TRACING_ENABLED",
155-
provider_key="tracer_provider",
156-
client_options=client_options,
155+
feature_key="tracer_provider",
156+
configuration=configuration,
157157
)
158158
assert result is True
159159

@@ -169,8 +169,8 @@ def test_resolve_feature_flags_ga_fallback_to_false(monkeypatch, env_val):
169169
monkeypatch.delenv("GOOGLE_SDK_PYTHON_TRACING_ENABLED", raising=False)
170170
result = feature_gating_helpers.resolve_feature_flags(
171171
env_var="GOOGLE_SDK_PYTHON_TRACING_ENABLED",
172-
provider_key="tracer_provider",
173-
client_options=None,
172+
feature_key="tracer_provider",
173+
configuration=None,
174174
)
175175
assert result is False
176176

@@ -181,19 +181,19 @@ def __init__(self):
181181

182182

183183
@pytest.mark.parametrize(
184-
"client_options",
184+
"configuration",
185185
[
186186
{"other_option": "value"},
187187
_MockOptions(),
188188
],
189189
ids=["dict_without_key", "object_without_key"],
190190
)
191-
def test_resolve_feature_flags_options_without_key(client_options):
192-
"""Verify behavior when client_options is present but missing the provider key."""
191+
def test_resolve_feature_flags_options_without_key(configuration):
192+
"""Verify behavior when configuration is present but missing the provider key."""
193193
# GA Path: should fall through to env var / fallback
194194
result = feature_gating_helpers.resolve_feature_flags(
195195
env_var="GOOGLE_SDK_PYTHON_TRACING_ENABLED",
196-
provider_key="tracer_provider",
197-
client_options=client_options,
196+
feature_key="tracer_provider",
197+
configuration=configuration,
198198
)
199199
assert result is False

0 commit comments

Comments
 (0)