Skip to content

Commit 40a650b

Browse files
committed
fallback address to localhost default
1 parent a299415 commit 40a650b

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

temporalio/envconfig.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from __future__ import annotations
88

9-
from dataclasses import dataclass, field
9+
from dataclasses import dataclass, field, replace
1010
from pathlib import Path
1111
from typing import Any, Dict, Literal, Mapping, Optional, Union, cast
1212

@@ -253,7 +253,7 @@ def to_client_connect_config(self) -> ClientConnectConfig:
253253

254254
@staticmethod
255255
def load(
256-
profile: str = "default",
256+
profile: Optional[str] = None,
257257
*,
258258
config_source: Optional[DataSource] = None,
259259
disable_file: bool = False,
@@ -268,7 +268,8 @@ def load(
268268
:py:meth:`to_client_connect_config` method on the returned profile.
269269
270270
Args:
271-
profile: Profile to load from the config.
271+
profile: Profile to load from the config. If the profile is provided,
272+
the default profile is returned.
272273
config_source: If present, this is used as the configuration source
273274
instead of default file locations. This can be a path to the file
274275
or the string/byte contents of the file.
@@ -298,7 +299,13 @@ def load(
298299
config_file_strict=config_file_strict,
299300
env_vars=override_env_vars,
300301
)
301-
return ClientConfigProfile.from_dict(raw_profile)
302+
prof = ClientConfigProfile.from_dict(raw_profile)
303+
304+
# If no address is present, default to localhost
305+
if prof.address is None:
306+
prof = replace(prof, address="localhost:7233")
307+
308+
return prof
302309

303310

304311
@dataclass
@@ -377,7 +384,7 @@ def load(
377384

378385
@staticmethod
379386
def load_client_connect_config(
380-
profile: str = "default",
387+
profile: Optional[str] = None,
381388
*,
382389
config_file: Optional[str] = None,
383390
disable_file: bool = False,
@@ -392,7 +399,8 @@ def load_client_connect_config(
392399
process's environment for overrides unless disabled.
393400
394401
Args:
395-
profile: The profile to load from the config. Defaults to "default".
402+
profile: Profile to load from the config. If the profile is provided,
403+
the default profile is returned.
396404
config_file: Path to a specific TOML config file. If not provided,
397405
default file locations are used. This is ignored if
398406
``disable_file`` is true.

tests/test_envconfig.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,20 @@ def test_load_profile_strict_mode_fail(tmp_path: Path):
308308
ClientConfigProfile.load(config_source=config_file, config_file_strict=True)
309309

310310

311+
def test_load_profile_default_missing_is_ok():
312+
"""Confirm that loading with no profile from a config without 'default' is ok."""
313+
profile = ClientConfigProfile.load(config_source=TOML_CONFIG_TLS_DETAILED)
314+
# Should be a profile with the default address
315+
assert profile.address == "localhost:7233"
316+
assert profile.namespace is None
317+
# But if we ask for the 'default' profile, it should fail since it's not
318+
# in the config
319+
with pytest.raises(RuntimeError, match="Profile 'default' not found"):
320+
ClientConfigProfile.load(
321+
config_source=TOML_CONFIG_TLS_DETAILED, profile="default"
322+
)
323+
324+
311325
def test_load_profiles_from_data_malformed():
312326
"""Test that loading malformed TOML data raises an error."""
313327
with pytest.raises(RuntimeError, match="TOML parse error"):

0 commit comments

Comments
 (0)