Skip to content

Commit 6dd2a55

Browse files
Use shared plain-type helpers in manager and tool boundaries
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 3272384 commit 6dd2a55

5 files changed

Lines changed: 20 additions & 20 deletions

File tree

hyperbrowser/client/managers/async_manager/computer_action.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pydantic import BaseModel
22
from typing import Union, List, Optional
33
from hyperbrowser.exceptions import HyperbrowserError
4-
from hyperbrowser.type_utils import is_string_subclass_instance
4+
from hyperbrowser.type_utils import is_plain_string, is_string_subclass_instance
55
from ..response_utils import parse_response_model
66
from ..serialization_utils import serialize_model_dump_to_dict
77
from hyperbrowser.models import (
@@ -32,7 +32,7 @@ def __init__(self, client):
3232
async def _execute_request(
3333
self, session: Union[SessionDetail, str], params: ComputerActionParams
3434
) -> ComputerActionResponse:
35-
if type(session) is str:
35+
if is_plain_string(session):
3636
session = await self._client.sessions.get(session)
3737
elif is_string_subclass_instance(session):
3838
raise HyperbrowserError(
@@ -53,11 +53,11 @@ async def _execute_request(
5353
raise HyperbrowserError(
5454
"Computer action endpoint not available for this session"
5555
)
56-
if type(computer_action_endpoint) is not str:
56+
if not is_plain_string(computer_action_endpoint):
5757
raise HyperbrowserError("session computer_action_endpoint must be a string")
5858
try:
5959
normalized_computer_action_endpoint = computer_action_endpoint.strip()
60-
if type(normalized_computer_action_endpoint) is not str:
60+
if not is_plain_string(normalized_computer_action_endpoint):
6161
raise TypeError("normalized computer_action_endpoint must be a string")
6262
except HyperbrowserError:
6363
raise

hyperbrowser/client/managers/async_manager/session.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import IO, List, Optional, Union, overload
44
import warnings
55
from hyperbrowser.exceptions import HyperbrowserError
6-
from hyperbrowser.type_utils import is_string_subclass_instance
6+
from hyperbrowser.type_utils import is_plain_string, is_string_subclass_instance
77
from ...file_utils import ensure_existing_file_path
88
from ..serialization_utils import serialize_model_dump_to_dict
99
from ..session_utils import (
@@ -166,7 +166,7 @@ async def get_downloads_url(self, id: str) -> GetSessionDownloadsUrlResponse:
166166
async def upload_file(
167167
self, id: str, file_input: Union[str, PathLike[str], IO]
168168
) -> UploadFileResponse:
169-
if type(file_input) is str or isinstance(file_input, PathLike):
169+
if is_plain_string(file_input) or isinstance(file_input, PathLike):
170170
try:
171171
raw_file_path = os.fspath(file_input)
172172
except HyperbrowserError:

hyperbrowser/client/managers/sync_manager/computer_action.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pydantic import BaseModel
22
from typing import Union, List, Optional
33
from hyperbrowser.exceptions import HyperbrowserError
4-
from hyperbrowser.type_utils import is_string_subclass_instance
4+
from hyperbrowser.type_utils import is_plain_string, is_string_subclass_instance
55
from ..response_utils import parse_response_model
66
from ..serialization_utils import serialize_model_dump_to_dict
77
from hyperbrowser.models import (
@@ -32,7 +32,7 @@ def __init__(self, client):
3232
def _execute_request(
3333
self, session: Union[SessionDetail, str], params: ComputerActionParams
3434
) -> ComputerActionResponse:
35-
if type(session) is str:
35+
if is_plain_string(session):
3636
session = self._client.sessions.get(session)
3737
elif is_string_subclass_instance(session):
3838
raise HyperbrowserError(
@@ -53,11 +53,11 @@ def _execute_request(
5353
raise HyperbrowserError(
5454
"Computer action endpoint not available for this session"
5555
)
56-
if type(computer_action_endpoint) is not str:
56+
if not is_plain_string(computer_action_endpoint):
5757
raise HyperbrowserError("session computer_action_endpoint must be a string")
5858
try:
5959
normalized_computer_action_endpoint = computer_action_endpoint.strip()
60-
if type(normalized_computer_action_endpoint) is not str:
60+
if not is_plain_string(normalized_computer_action_endpoint):
6161
raise TypeError("normalized computer_action_endpoint must be a string")
6262
except HyperbrowserError:
6363
raise

hyperbrowser/client/managers/sync_manager/session.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import IO, List, Optional, Union, overload
44
import warnings
55
from hyperbrowser.exceptions import HyperbrowserError
6-
from hyperbrowser.type_utils import is_string_subclass_instance
6+
from hyperbrowser.type_utils import is_plain_string, is_string_subclass_instance
77
from ...file_utils import ensure_existing_file_path
88
from ..serialization_utils import serialize_model_dump_to_dict
99
from ..session_utils import (
@@ -158,7 +158,7 @@ def get_downloads_url(self, id: str) -> GetSessionDownloadsUrlResponse:
158158
def upload_file(
159159
self, id: str, file_input: Union[str, PathLike[str], IO]
160160
) -> UploadFileResponse:
161-
if type(file_input) is str or isinstance(file_input, PathLike):
161+
if is_plain_string(file_input) or isinstance(file_input, PathLike):
162162
try:
163163
raw_file_path = os.fspath(file_input)
164164
except HyperbrowserError:

hyperbrowser/tools/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
copy_mapping_values_by_string_keys,
1010
read_string_mapping_keys,
1111
)
12-
from hyperbrowser.type_utils import is_string_subclass_instance
12+
from hyperbrowser.type_utils import is_plain_string, is_string_subclass_instance
1313
from hyperbrowser.models.agents.browser_use import StartBrowserUseTaskParams
1414
from hyperbrowser.models.crawl import StartCrawlJobParams
1515
from hyperbrowser.models.extract import StartExtractJobParams
@@ -86,12 +86,12 @@ def _prepare_extract_tool_params(params: Mapping[str, Any]) -> Dict[str, Any]:
8686
normalized_params = _to_param_dict(params)
8787
schema_value = normalized_params.get("schema")
8888
if schema_value is not None and not (
89-
type(schema_value) is str or isinstance(schema_value, MappingABC)
89+
is_plain_string(schema_value) or isinstance(schema_value, MappingABC)
9090
):
9191
raise HyperbrowserError(
9292
"Extract tool `schema` must be an object or JSON string"
9393
)
94-
if type(schema_value) is str:
94+
if is_plain_string(schema_value):
9595
try:
9696
parsed_schema = json.loads(schema_value)
9797
except HyperbrowserError:
@@ -121,7 +121,7 @@ def _to_param_dict(params: Mapping[str, Any]) -> Dict[str, Any]:
121121
for key in param_keys:
122122
try:
123123
normalized_key = key.strip()
124-
if type(normalized_key) is not str:
124+
if not is_plain_string(normalized_key):
125125
raise TypeError("normalized tool param key must be a string")
126126
is_empty_key = len(normalized_key) == 0
127127
except HyperbrowserError:
@@ -176,7 +176,7 @@ def _serialize_extract_tool_data(data: Any) -> str:
176176
return ""
177177
try:
178178
serialized_data = json.dumps(data, allow_nan=False)
179-
if type(serialized_data) is not str:
179+
if not is_plain_string(serialized_data):
180180
raise TypeError("serialized extract tool response data must be a string")
181181
return serialized_data
182182
except HyperbrowserError:
@@ -195,10 +195,10 @@ def _normalize_optional_text_field_value(
195195
) -> str:
196196
if field_value is None:
197197
return ""
198-
if type(field_value) is str:
198+
if is_plain_string(field_value):
199199
try:
200200
normalized_field_value = "".join(character for character in field_value)
201-
if type(normalized_field_value) is not str:
201+
if not is_plain_string(normalized_field_value):
202202
raise TypeError("normalized text field must be a string")
203203
return normalized_field_value
204204
except HyperbrowserError:
@@ -213,7 +213,7 @@ def _normalize_optional_text_field_value(
213213
if isinstance(field_value, (bytes, bytearray, memoryview)):
214214
try:
215215
normalized_field_value = memoryview(field_value).tobytes().decode("utf-8")
216-
if type(normalized_field_value) is not str:
216+
if not is_plain_string(normalized_field_value):
217217
raise TypeError("normalized text field must be a string")
218218
return normalized_field_value
219219
except (TypeError, ValueError, UnicodeDecodeError) as exc:

0 commit comments

Comments
 (0)