Skip to content

Commit d82ef32

Browse files
Require concrete operation-name inputs in polling validators
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 43e169c commit d82ef32

2 files changed

Lines changed: 4 additions & 111 deletions

File tree

hyperbrowser/client/polling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def _normalize_non_negative_real(value: float, *, field_name: str) -> float:
116116

117117

118118
def _validate_operation_name(operation_name: str) -> None:
119-
if not isinstance(operation_name, str):
119+
if type(operation_name) is not str:
120120
raise HyperbrowserError("operation_name must be a string")
121121
try:
122122
normalized_operation_name = operation_name.strip()

tests/test_polling.py

Lines changed: 3 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -7316,116 +7316,9 @@ async def _operation() -> str:
73167316
"runner",
73177317
[_run_retry_operation_sync_with_name, _run_retry_operation_async_with_name],
73187318
)
7319-
def test_retry_operation_wraps_operation_name_strip_runtime_errors(runner):
7319+
def test_retry_operation_rejects_string_subclass_operation_names(runner):
73207320
class _BrokenOperationName(str):
7321-
def strip(self, chars=None): # type: ignore[override]
7322-
_ = chars
7323-
raise RuntimeError("operation_name strip exploded")
7324-
7325-
with pytest.raises(
7326-
HyperbrowserError, match="Failed to normalize operation_name"
7327-
) as exc_info:
7328-
runner(_BrokenOperationName("poll operation"))
7329-
7330-
assert isinstance(exc_info.value.original_error, RuntimeError)
7331-
7332-
7333-
@pytest.mark.parametrize(
7334-
"runner",
7335-
[_run_retry_operation_sync_with_name, _run_retry_operation_async_with_name],
7336-
)
7337-
def test_retry_operation_preserves_operation_name_strip_hyperbrowser_errors(runner):
7338-
class _BrokenOperationName(str):
7339-
def strip(self, chars=None): # type: ignore[override]
7340-
_ = chars
7341-
raise HyperbrowserError("custom operation_name strip failure")
7342-
7343-
with pytest.raises(
7344-
HyperbrowserError, match="custom operation_name strip failure"
7345-
) as exc_info:
7346-
runner(_BrokenOperationName("poll operation"))
7347-
7348-
assert exc_info.value.original_error is None
7349-
7350-
7351-
@pytest.mark.parametrize(
7352-
"runner",
7353-
[_run_retry_operation_sync_with_name, _run_retry_operation_async_with_name],
7354-
)
7355-
def test_retry_operation_wraps_non_string_operation_name_strip_results(runner):
7356-
class _BrokenOperationName(str):
7357-
def strip(self, chars=None): # type: ignore[override]
7358-
_ = chars
7359-
return object()
7360-
7361-
with pytest.raises(
7362-
HyperbrowserError, match="Failed to normalize operation_name"
7363-
) as exc_info:
7364-
runner(_BrokenOperationName("poll operation"))
7365-
7366-
assert isinstance(exc_info.value.original_error, TypeError)
7367-
7368-
7369-
@pytest.mark.parametrize(
7370-
"runner",
7371-
[_run_retry_operation_sync_with_name, _run_retry_operation_async_with_name],
7372-
)
7373-
def test_retry_operation_wraps_operation_name_length_runtime_errors(runner):
7374-
class _BrokenOperationName(str):
7375-
def strip(self, chars=None): # type: ignore[override]
7376-
_ = chars
7377-
return "poll operation"
7378-
7379-
def __len__(self):
7380-
raise RuntimeError("operation_name length exploded")
7381-
7382-
with pytest.raises(
7383-
HyperbrowserError, match="Failed to validate operation_name length"
7384-
) as exc_info:
7385-
runner(_BrokenOperationName("poll operation"))
7386-
7387-
assert isinstance(exc_info.value.original_error, RuntimeError)
7388-
7389-
7390-
@pytest.mark.parametrize(
7391-
"runner",
7392-
[_run_retry_operation_sync_with_name, _run_retry_operation_async_with_name],
7393-
)
7394-
def test_retry_operation_wraps_operation_name_character_validation_failures(runner):
7395-
class _BrokenOperationName(str):
7396-
def strip(self, chars=None): # type: ignore[override]
7397-
_ = chars
7398-
return "poll operation"
7399-
7400-
def __iter__(self):
7401-
raise RuntimeError("operation_name iteration exploded")
7402-
7403-
with pytest.raises(
7404-
HyperbrowserError, match="Failed to validate operation_name characters"
7405-
) as exc_info:
7406-
runner(_BrokenOperationName("poll operation"))
7407-
7408-
assert isinstance(exc_info.value.original_error, RuntimeError)
7409-
7410-
7411-
@pytest.mark.parametrize(
7412-
"runner",
7413-
[_run_retry_operation_sync_with_name, _run_retry_operation_async_with_name],
7414-
)
7415-
def test_retry_operation_preserves_operation_name_character_validation_hyperbrowser_errors(
7416-
runner,
7417-
):
7418-
class _BrokenOperationName(str):
7419-
def strip(self, chars=None): # type: ignore[override]
7420-
_ = chars
7421-
return "poll operation"
7422-
7423-
def __iter__(self):
7424-
raise HyperbrowserError("custom operation_name character failure")
7321+
pass
74257322

7426-
with pytest.raises(
7427-
HyperbrowserError, match="custom operation_name character failure"
7428-
) as exc_info:
7323+
with pytest.raises(HyperbrowserError, match="operation_name must be a string"):
74297324
runner(_BrokenOperationName("poll operation"))
7430-
7431-
assert exc_info.value.original_error is None

0 commit comments

Comments
 (0)