Skip to content

Commit ae5cb2a

Browse files
Refactor key formatting through shared helper
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 84baba8 commit ae5cb2a

File tree

6 files changed

+41
-35
lines changed

6 files changed

+41
-35
lines changed

hyperbrowser/client/managers/response_utils.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, Type, TypeVar
22

3-
from hyperbrowser.display_utils import normalize_display_text
3+
from hyperbrowser.display_utils import format_string_key_for_error, normalize_display_text
44
from hyperbrowser.exceptions import HyperbrowserError
55
from hyperbrowser.mapping_utils import read_string_key_mapping
66

@@ -20,13 +20,7 @@ def _normalize_operation_name_for_error(operation_name: str) -> str:
2020

2121

2222
def _normalize_response_key_for_error(key: str) -> str:
23-
normalized_key = normalize_display_text(
24-
key,
25-
max_length=_MAX_KEY_DISPLAY_LENGTH,
26-
)
27-
if not normalized_key:
28-
return "<blank key>"
29-
return normalized_key
23+
return format_string_key_for_error(key, max_length=_MAX_KEY_DISPLAY_LENGTH)
3024

3125

3226
def parse_response_model(

hyperbrowser/client/managers/session_utils.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, List, Type, TypeVar
22

3-
from hyperbrowser.display_utils import normalize_display_text
3+
from hyperbrowser.display_utils import format_string_key_for_error
44
from hyperbrowser.exceptions import HyperbrowserError
55
from hyperbrowser.models.session import SessionRecording
66
from .list_parsing_utils import parse_mapping_list_items
@@ -11,13 +11,7 @@
1111

1212

1313
def _format_recording_key_display(key: str) -> str:
14-
normalized_key = normalize_display_text(
15-
key,
16-
max_length=_MAX_KEY_DISPLAY_LENGTH,
17-
)
18-
if not normalized_key:
19-
return "<blank key>"
20-
return normalized_key
14+
return format_string_key_for_error(key, max_length=_MAX_KEY_DISPLAY_LENGTH)
2115

2216

2317
def parse_session_response_model(

hyperbrowser/display_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ def normalize_display_text(value: str, *, max_length: int) -> str:
1919
return f"{sanitized_value[:available_length]}{_TRUNCATED_DISPLAY_SUFFIX}"
2020
except Exception:
2121
return ""
22+
23+
24+
def format_string_key_for_error(
25+
key: str,
26+
*,
27+
max_length: int,
28+
blank_fallback: str = "<blank key>",
29+
) -> str:
30+
normalized_key = normalize_display_text(key, max_length=max_length)
31+
if not normalized_key:
32+
return blank_fallback
33+
return normalized_key

hyperbrowser/tools/__init__.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from collections.abc import Mapping as MappingABC
44
from typing import Any, Dict, Mapping
55

6-
from hyperbrowser.display_utils import normalize_display_text
6+
from hyperbrowser.display_utils import format_string_key_for_error
77
from hyperbrowser.exceptions import HyperbrowserError
88
from hyperbrowser.mapping_utils import copy_mapping_values_by_string_keys
99
from hyperbrowser.models.agents.browser_use import StartBrowserUseTaskParams
@@ -54,13 +54,7 @@ def _has_declared_attribute(
5454

5555

5656
def _format_tool_param_key_for_error(key: str) -> str:
57-
normalized_key = normalize_display_text(
58-
key,
59-
max_length=_MAX_KEY_DISPLAY_LENGTH,
60-
)
61-
if not normalized_key:
62-
return "<blank key>"
63-
return normalized_key
57+
return format_string_key_for_error(key, max_length=_MAX_KEY_DISPLAY_LENGTH)
6458

6559

6660
def _normalize_extract_schema_mapping(

hyperbrowser/transport/base.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from abc import ABC, abstractmethod
22
from typing import Generic, Mapping, Optional, Type, TypeVar, Union
33

4-
from hyperbrowser.display_utils import normalize_display_text
4+
from hyperbrowser.display_utils import format_string_key_for_error, normalize_display_text
55
from hyperbrowser.exceptions import HyperbrowserError
66
from hyperbrowser.mapping_utils import read_string_key_mapping
77

@@ -31,16 +31,10 @@ def _safe_model_name(model: object) -> str:
3131

3232

3333
def _format_mapping_key_for_error(key: str) -> str:
34-
normalized_key = normalize_display_text(
35-
key, max_length=_MAX_MAPPING_KEY_DISPLAY_LENGTH
34+
return format_string_key_for_error(
35+
key,
36+
max_length=_MAX_MAPPING_KEY_DISPLAY_LENGTH,
3637
)
37-
if normalized_key:
38-
return normalized_key
39-
try:
40-
_ = "".join(character for character in key)
41-
except Exception:
42-
return "<unreadable key>"
43-
return "<blank key>"
4438

4539

4640
def _normalize_transport_api_key(api_key: str) -> str:

tests/test_display_utils.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from hyperbrowser.display_utils import normalize_display_text
1+
from hyperbrowser.display_utils import format_string_key_for_error, normalize_display_text
22

33

44
def test_normalize_display_text_keeps_valid_input():
@@ -28,3 +28,21 @@ def __iter__(self): # type: ignore[override]
2828

2929
def test_normalize_display_text_returns_empty_for_non_string_inputs():
3030
assert normalize_display_text(123, max_length=20) == "" # type: ignore[arg-type]
31+
32+
33+
def test_format_string_key_for_error_returns_normalized_key():
34+
assert (
35+
format_string_key_for_error(" \nkey\t ", max_length=20)
36+
== "?key?"
37+
)
38+
39+
40+
def test_format_string_key_for_error_returns_blank_fallback_for_empty_keys():
41+
assert format_string_key_for_error(" ", max_length=20) == "<blank key>"
42+
43+
44+
def test_format_string_key_for_error_supports_custom_blank_fallback():
45+
assert (
46+
format_string_key_for_error(" ", max_length=20, blank_fallback="<empty>")
47+
== "<empty>"
48+
)

0 commit comments

Comments
 (0)