Skip to content

Commit 51cb6bd

Browse files
committed
Simplify error handling in data validation
Remove unnecessary try-except block that was catching TypeError during iteration. The validation logic doesn't need exception handling since we're only checking types of items in a list/tuple that we've already confirmed exists.
1 parent a936c47 commit 51cb6bd

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

httpx/_content.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,12 @@ def encode_request(
206206
# Validate that data is bytes-like or an iterable of bytes, not other types
207207
if isinstance(data, (list, tuple)):
208208
# Check if it's a list/tuple of bytes
209-
try:
210-
for item in data:
211-
if not isinstance(item, (bytes, bytearray, memoryview)):
212-
raise TypeError(
213-
f"Expected bytes-like object in 'data' sequence, got {type(item).__name__}. "
214-
f"Use 'json=' for JSON data or 'data={{...}}' for form data."
215-
)
216-
except TypeError:
217-
# Re-raise our custom error, not the iteration error
218-
raise
209+
for item in data:
210+
if not isinstance(item, (bytes, bytearray, memoryview)):
211+
raise TypeError(
212+
f"Expected bytes-like object in 'data' sequence, got {type(item).__name__}. "
213+
f"Use 'json=' for JSON data or 'data={{...}}' for form data."
214+
)
219215

220216
message = "Use 'content=<...>' to upload raw bytes/text content."
221217
warnings.warn(message, DeprecationWarning, stacklevel=2)

0 commit comments

Comments
 (0)