fix: str(value)

This commit is contained in:
yetval
2026-04-30 09:52:34 -04:00
parent a5a5652996
commit 334b0f5538
2 changed files with 26 additions and 1 deletions
+8 -1
View File
@@ -22,6 +22,13 @@ def _convert_to_bytes(value: str | bytes) -> bytes:
return value.encode(encoding="utf-8", errors="ignore")
def _stable_value_repr(value: Any) -> str:
try:
return orjson.dumps(value, option=orjson.OPT_SORT_KEYS, default=repr).decode()
except TypeError:
return repr(value)
class Request:
def __init__(
self,
@@ -98,7 +105,7 @@ class Request:
if include_kwargs:
filtered_kwargs = {
key.lower(): str(value)
key.lower(): _stable_value_repr(value)
for key, value in self._session_kwargs.items()
if key.lower() not in ("data", "json")
}
+18
View File
@@ -106,6 +106,24 @@ class TestRequestProperties:
assert r1.update_fingerprint(include_kwargs=True) != r2.update_fingerprint(include_kwargs=True)
def test_fingerprint_include_kwargs_handles_non_primitive_values(self):
class _Opaque:
def __repr__(self) -> str:
return "_Opaque(stable)"
opaque = _Opaque()
r1 = Request("https://example.com", proxies={"http": "p1"}, custom=opaque)
r2 = Request("https://example.com", proxies={"http": "p1"}, custom=opaque)
r3 = Request("https://example.com", proxies={"http": "p2"}, custom=opaque)
fp1 = r1.update_fingerprint(include_kwargs=True)
r2._fp = None
fp2 = r2.update_fingerprint(include_kwargs=True)
fp3 = r3.update_fingerprint(include_kwargs=True)
assert fp1 == fp2
assert fp1 != fp3
def test_fingerprint_include_headers_preserves_header_value_case(self):
"""Test header values are fingerprinted without lowercasing."""
r1 = Request("https://example.com", headers={"X-Test": "A"})