Skip to content

Commit 9035bb0

Browse files
fix: restore X-Glean constructor options and preserve them across regeneration
Glean(include_experimental=True) raised a TypeError because a regeneration dropped the constructor args from sdk.py and sdkconfiguration.py. Add back exclude_deprecated_after and include_experimental, add include_beta, and enable persistentEdits (as in the TypeScript and Java SDKs) so Speakeasy three-way merges these edits instead of overwriting them. Add end-to-end tests that construct a real client with each option.
1 parent 347b60c commit 9035bb0

4 files changed

Lines changed: 105 additions & 17 deletions

File tree

‎.speakeasy/gen.yaml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ generation:
2929
versioningStrategy: automatic
3030
mockServer:
3131
disabled: false
32-
persistentEdits: {}
32+
persistentEdits:
33+
enabled: "true"
3334
tests:
3435
generateTests: true
3536
generateNewTests: true

‎src/glean/api_client/sdk.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ def __init__(
8585
retry_config: OptionalNullable[RetryConfig] = UNSET,
8686
timeout_ms: Optional[int] = None,
8787
debug_logger: Optional[Logger] = None,
88+
exclude_deprecated_after: Optional[str] = None,
89+
include_beta: Optional[bool] = None,
90+
include_experimental: Optional[bool] = None,
8891
) -> None:
8992
r"""Instantiates the SDK configuring it with the provided parameters.
9093
@@ -97,6 +100,9 @@ def __init__(
97100
:param async_client: The Async HTTP client to use for all asynchronous methods
98101
:param retry_config: The retry configuration to use for all supported methods
99102
:param timeout_ms: Optional request timeout applied to each operation in milliseconds
103+
:param exclude_deprecated_after: Exclude API endpoints deprecated after this date (YYYY-MM-DD)
104+
:param include_beta: When True, enables beta API features
105+
:param include_experimental: Alias of include_beta
100106
"""
101107
client_supplied = True
102108
if client is None:
@@ -151,6 +157,9 @@ def __init__(
151157
retry_config=retry_config,
152158
timeout_ms=timeout_ms,
153159
debug_logger=debug_logger,
160+
exclude_deprecated_after=exclude_deprecated_after,
161+
include_beta=include_beta,
162+
include_experimental=include_experimental,
154163
),
155164
parent_ref=self,
156165
)

‎src/glean/api_client/sdkconfiguration.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ class SDKConfiguration:
3939
user_agent: str = __user_agent__
4040
retry_config: OptionalNullable[RetryConfig] = Field(default_factory=lambda: UNSET)
4141
timeout_ms: Optional[int] = None
42+
exclude_deprecated_after: Optional[str] = None
43+
"""
44+
Exclude API endpoints that will be deprecated after this date.
45+
Use this to test your integration against upcoming deprecations.
46+
Format: YYYY-MM-DD (e.g., '2026-10-15')
47+
48+
More information: https://developers.glean.com/deprecations/overview
49+
"""
50+
include_beta: Optional[bool] = None
51+
"""
52+
When True, enables beta API features that are not yet generally available.
53+
Use this to preview and test new functionality.
54+
"""
55+
include_experimental: Optional[bool] = None
56+
"""
57+
Alias of include_beta. Either option enables beta API features.
58+
"""
4259

4360
def get_server_details(self) -> Tuple[str, Dict[str, str]]:
4461
if self.server_url is not None and self.server_url:

‎tests/test_x_glean_hook.py‎

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,29 @@ def test_include_beta(clean_x_glean_env, beta_env, experimental_env, config, exp
373373
assert "X-Glean-Beta" not in result.headers
374374

375375

376+
def send_query(**kwargs) -> httpx.Request:
377+
"""Send one request through a real Glean client and return what went out."""
378+
captured = []
379+
380+
def handler(request: httpx.Request) -> httpx.Response:
381+
captured.append(request)
382+
return httpx.Response(200, json={})
383+
384+
glean = Glean(
385+
api_token="test-token",
386+
server_url="https://example.com",
387+
client=httpx.Client(transport=httpx.MockTransport(handler)),
388+
**kwargs,
389+
)
390+
try:
391+
glean.search.query(query="test")
392+
except Exception: # pylint: disable=broad-exception-caught
393+
pass # The empty response body may not validate; only the request matters.
394+
395+
assert captured, "expected a request to be sent"
396+
return captured[0]
397+
398+
376399
@pytest.mark.parametrize(
377400
"env, expected",
378401
[
@@ -393,25 +416,63 @@ def test_sdk_sends_include_experimental_header(clean_x_glean_env, env, expected)
393416
for name, value in env.items():
394417
clean_x_glean_env.setenv(name, value)
395418

396-
captured = []
419+
request = send_query()
397420

398-
def handler(request: httpx.Request) -> httpx.Response:
399-
captured.append(request)
400-
return httpx.Response(200, json={})
421+
assert request.headers.get(INCLUDE_EXPERIMENTAL_HEADER) == expected
422+
assert "X-Glean-Beta" not in request.headers
401423

402-
glean = Glean(
403-
api_token="test-token",
404-
server_url="https://example.com",
405-
client=httpx.Client(transport=httpx.MockTransport(handler)),
406-
)
407-
try:
408-
glean.search.query(query="test")
409-
except Exception: # pylint: disable=broad-exception-caught
410-
pass # The empty response body may not validate; only the request matters.
411424

412-
assert captured, "expected a request to be sent"
413-
assert captured[0].headers.get(INCLUDE_EXPERIMENTAL_HEADER) == expected
414-
assert "X-Glean-Beta" not in captured[0].headers
425+
@pytest.mark.parametrize(
426+
"kwargs, expected_experimental, expected_deprecated",
427+
[
428+
pytest.param({}, None, None, id="no options"),
429+
pytest.param({"include_beta": True}, "true", None, id="include_beta=True"),
430+
pytest.param({"include_beta": False}, None, None, id="include_beta=False"),
431+
pytest.param(
432+
{"include_experimental": True}, "true", None, id="include_experimental=True"
433+
),
434+
pytest.param(
435+
{"include_beta": False, "include_experimental": True},
436+
"true",
437+
None,
438+
id="include_beta=False does not turn off include_experimental",
439+
),
440+
pytest.param(
441+
{"exclude_deprecated_after": "2026-10-15"},
442+
None,
443+
"2026-10-15",
444+
id="exclude_deprecated_after",
445+
),
446+
pytest.param(
447+
{"exclude_deprecated_after": "2026-10-15", "include_beta": True},
448+
"true",
449+
"2026-10-15",
450+
id="all options",
451+
),
452+
],
453+
)
454+
def test_sdk_constructor_options(
455+
clean_x_glean_env, kwargs, expected_experimental, expected_deprecated
456+
):
457+
"""The Glean constructor accepts the X-Glean options and sends their headers.
458+
459+
These options live in generated files (sdk.py and sdkconfiguration.py) and a
460+
regeneration has dropped them before, so this guards against that.
461+
"""
462+
request = send_query(**kwargs)
463+
464+
assert request.headers.get(INCLUDE_EXPERIMENTAL_HEADER) == expected_experimental
465+
assert request.headers.get(EXCLUDE_DEPRECATED_AFTER_HEADER) == expected_deprecated
466+
assert "X-Glean-Beta" not in request.headers
467+
468+
469+
def test_env_var_overrides_constructor_option(clean_x_glean_env):
470+
"""X_GLEAN_INCLUDE_BETA takes precedence over the include_beta constructor option."""
471+
clean_x_glean_env.setenv(INCLUDE_BETA_ENV_VAR, "false")
472+
473+
request = send_query(include_beta=True)
474+
475+
assert INCLUDE_EXPERIMENTAL_HEADER not in request.headers
415476

416477

417478
if __name__ == "__main__":

0 commit comments

Comments
 (0)