Skip to content

Commit 5b211ba

Browse files
Refactor error-utils plain string guards
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent e441576 commit 5b211ba

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

hyperbrowser/transport/error_utils.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@
4646
}
4747

4848

49+
def _is_plain_string(value: Any) -> bool:
50+
return type(value) is str
51+
52+
53+
def _is_string_subclass_instance(value: Any) -> bool:
54+
value_type = type(value)
55+
return value_type is not str and str in value_type.__mro__
56+
57+
4958
def _safe_to_string(value: Any) -> str:
5059
try:
5160
normalized_value = str(value)
@@ -78,7 +87,7 @@ def _sanitize_error_message_text(message: str) -> str:
7887

7988

8089
def _has_non_blank_text(value: Any) -> bool:
81-
if type(value) is not str:
90+
if not _is_plain_string(value):
8291
return False
8392
try:
8493
stripped_value = value.strip()
@@ -100,10 +109,9 @@ def _normalize_request_method(method: Any) -> str:
100109
raw_method = memoryview(raw_method).tobytes().decode("ascii")
101110
except (TypeError, ValueError, UnicodeDecodeError):
102111
return "UNKNOWN"
103-
elif isinstance(raw_method, str):
104-
if type(raw_method) is not str:
105-
return "UNKNOWN"
106-
elif type(raw_method) is not str:
112+
elif _is_string_subclass_instance(raw_method):
113+
return "UNKNOWN"
114+
elif not _is_plain_string(raw_method):
107115
try:
108116
raw_method = str(raw_method)
109117
except Exception:
@@ -148,10 +156,9 @@ def _normalize_request_url(url: Any) -> str:
148156
raw_url = memoryview(raw_url).tobytes().decode("utf-8")
149157
except (TypeError, ValueError, UnicodeDecodeError):
150158
return "unknown URL"
151-
elif isinstance(raw_url, str):
152-
if type(raw_url) is not str:
153-
return "unknown URL"
154-
elif type(raw_url) is not str:
159+
elif _is_string_subclass_instance(raw_url):
160+
return "unknown URL"
161+
elif not _is_plain_string(raw_url):
155162
try:
156163
raw_url = str(raw_url)
157164
except Exception:
@@ -197,15 +204,15 @@ def _truncate_error_message(message: str) -> str:
197204

198205

199206
def _normalize_response_text_for_error_message(response_text: Any) -> str:
200-
if type(response_text) is str:
207+
if _is_plain_string(response_text):
201208
try:
202209
normalized_response_text = "".join(character for character in response_text)
203210
if type(normalized_response_text) is not str:
204211
raise TypeError("normalized response text must be a string")
205212
return normalized_response_text
206213
except Exception:
207214
return _safe_to_string(response_text)
208-
if isinstance(response_text, str):
215+
if _is_string_subclass_instance(response_text):
209216
return _safe_to_string(response_text)
210217
if isinstance(response_text, (bytes, bytearray, memoryview)):
211218
try:
@@ -218,15 +225,15 @@ def _normalize_response_text_for_error_message(response_text: Any) -> str:
218225
def _stringify_error_value(value: Any, *, _depth: int = 0) -> str:
219226
if _depth > 10:
220227
return _safe_to_string(value)
221-
if type(value) is str:
228+
if _is_plain_string(value):
222229
try:
223230
normalized_value = "".join(character for character in value)
224231
if type(normalized_value) is not str:
225232
raise TypeError("normalized error value must be a string")
226233
return normalized_value
227234
except Exception:
228235
return _safe_to_string(value)
229-
if isinstance(value, str):
236+
if _is_string_subclass_instance(value):
230237
return _safe_to_string(value)
231238
if isinstance(value, Mapping):
232239
for key in ("message", "error", "detail", "errors", "msg", "title", "reason"):

0 commit comments

Comments
 (0)