Skip to content

Commit c48b357

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

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

temporalio/envconfig.py

Lines changed: 16 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

@@ -15,6 +15,8 @@
1515
import temporalio.service
1616
from temporalio.bridge.temporal_sdk_bridge import envconfig as _bridge_envconfig
1717

18+
DEFAULT_TARGET_HOST = "localhost:7233"
19+
1820
DataSource: TypeAlias = Union[
1921
Path, str, bytes
2022
] # str represents a file contents, bytes represents raw data
@@ -253,7 +255,7 @@ def to_client_connect_config(self) -> ClientConnectConfig:
253255

254256
@staticmethod
255257
def load(
256-
profile: str = "default",
258+
profile: Optional[str] = None,
257259
*,
258260
config_source: Optional[DataSource] = None,
259261
disable_file: bool = False,
@@ -268,7 +270,8 @@ def load(
268270
:py:meth:`to_client_connect_config` method on the returned profile.
269271
270272
Args:
271-
profile: Profile to load from the config.
273+
profile: Profile to load from the config. If the profile is provided,
274+
the default profile is returned.
272275
config_source: If present, this is used as the configuration source
273276
instead of default file locations. This can be a path to the file
274277
or the string/byte contents of the file.
@@ -298,7 +301,13 @@ def load(
298301
config_file_strict=config_file_strict,
299302
env_vars=override_env_vars,
300303
)
301-
return ClientConfigProfile.from_dict(raw_profile)
304+
prof = ClientConfigProfile.from_dict(raw_profile)
305+
306+
# If no address is present, default to localhost
307+
if prof.address is None:
308+
prof = replace(prof, address=DEFAULT_TARGET_HOST)
309+
310+
return prof
302311

303312

304313
@dataclass
@@ -377,7 +386,7 @@ def load(
377386

378387
@staticmethod
379388
def load_client_connect_config(
380-
profile: str = "default",
389+
profile: Optional[str] = None,
381390
*,
382391
config_file: Optional[str] = None,
383392
disable_file: bool = False,
@@ -392,7 +401,8 @@ def load_client_connect_config(
392401
process's environment for overrides unless disabled.
393402
394403
Args:
395-
profile: The profile to load from the config. Defaults to "default".
404+
profile: Profile to load from the config. If the profile is provided,
405+
the default profile is returned.
396406
config_file: Path to a specific TOML config file. If not provided,
397407
default file locations are used. This is ignored if
398408
``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)