Skip to content

Commit ec9e9ee

Browse files
Tighten request method/url strip output typing
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 1b53d59 commit ec9e9ee

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

hyperbrowser/transport/error_utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def _has_non_blank_text(value: Any) -> bool:
8080
return False
8181
try:
8282
stripped_value = value.strip()
83-
if not isinstance(stripped_value, str):
83+
if type(stripped_value) is not str:
8484
return False
8585
return bool(stripped_value)
8686
except Exception:
@@ -107,13 +107,13 @@ def _normalize_request_method(method: Any) -> str:
107107
if not isinstance(raw_method, str):
108108
return "UNKNOWN"
109109
stripped_method = raw_method.strip()
110-
if not isinstance(stripped_method, str) or not stripped_method:
110+
if type(stripped_method) is not str or not stripped_method:
111111
return "UNKNOWN"
112112
normalized_method = stripped_method.upper()
113-
if not isinstance(normalized_method, str):
113+
if type(normalized_method) is not str:
114114
return "UNKNOWN"
115115
lowered_method = normalized_method.lower()
116-
if not isinstance(lowered_method, str):
116+
if type(lowered_method) is not str:
117117
return "UNKNOWN"
118118
except Exception:
119119
return "UNKNOWN"
@@ -151,10 +151,10 @@ def _normalize_request_url(url: Any) -> str:
151151

152152
try:
153153
normalized_url = raw_url.strip()
154-
if not isinstance(normalized_url, str) or not normalized_url:
154+
if type(normalized_url) is not str or not normalized_url:
155155
return "unknown URL"
156156
lowered_url = normalized_url.lower()
157-
if not isinstance(lowered_url, str):
157+
if type(lowered_url) is not str:
158158
return "unknown URL"
159159
except Exception:
160160
return "unknown URL"

tests/test_transport_error_utils.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ def __len__(self):
250250
raise RuntimeError("method length exploded")
251251

252252

253+
class _StringSubclassMethodStripResult(str):
254+
class _StripResult(str):
255+
pass
256+
257+
def strip(self, chars=None): # type: ignore[override]
258+
_ = chars
259+
return self._StripResult("get")
260+
261+
253262
class _BrokenStripUrl(str):
254263
def strip(self, chars=None): # type: ignore[override]
255264
_ = chars
@@ -277,6 +286,15 @@ def __iter__(self):
277286
raise RuntimeError("url iteration exploded")
278287

279288

289+
class _StringSubclassUrlStripResult(str):
290+
class _StripResult(str):
291+
pass
292+
293+
def strip(self, chars=None): # type: ignore[override]
294+
_ = chars
295+
return self._StripResult("https://example.com/path")
296+
297+
280298
class _StringifiesToBrokenSubclass:
281299
class _BrokenString(str):
282300
def __iter__(self):
@@ -771,6 +789,15 @@ def test_format_generic_request_failure_message_normalizes_method_length_failure
771789
assert message == "Request UNKNOWN https://example.com/path failed"
772790

773791

792+
def test_format_generic_request_failure_message_normalizes_method_strip_string_subclass_results():
793+
message = format_generic_request_failure_message(
794+
method=_StringSubclassMethodStripResult("get"),
795+
url="https://example.com/path",
796+
)
797+
798+
assert message == "Request UNKNOWN https://example.com/path failed"
799+
800+
774801
def test_format_generic_request_failure_message_normalizes_url_strip_failures():
775802
message = format_generic_request_failure_message(
776803
method="GET",
@@ -798,6 +825,15 @@ def test_format_generic_request_failure_message_normalizes_url_iteration_failure
798825
assert message == "Request GET unknown URL failed"
799826

800827

828+
def test_format_generic_request_failure_message_normalizes_url_strip_string_subclass_results():
829+
message = format_generic_request_failure_message(
830+
method="GET",
831+
url=_StringSubclassUrlStripResult("https://example.com/path"),
832+
)
833+
834+
assert message == "Request GET unknown URL failed"
835+
836+
801837
def test_format_request_failure_message_truncates_very_long_fallback_urls():
802838
very_long_url = "https://example.com/" + ("a" * 1200)
803839
message = format_request_failure_message(

0 commit comments

Comments
 (0)