Extract multipart parsing into _parse_multipart_files helper#2958
Extract multipart parsing into _parse_multipart_files helper#2958adamtheturtle merged 1 commit intomainfrom
Conversation
Refactor five image validators to eliminate duplicate multipart parsing code by extracting it into a reusable helper with @beartype decoration. Also simplify validate_image_is_image by directly accessing the stream from the files dict. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
| boundary=boundary.encode(encoding="utf-8"), | ||
| content_length=len(request_body), | ||
| ) | ||
| return files |
There was a problem hiding this comment.
Duplicate multipart parsing logic across modules
Low Severity
The new _parse_multipart_files helper duplicates multipart parsing logic already present in get_query_match_response_text in _query_tools.py and validate_extra_fields in fields_validators.py. All three locations use identical code to extract boundary and parse multipart data. A shared utility function could eliminate this duplication and improve maintainability.
| Raises: | ||
| BadImageError: Image data is given and it is not an image file. | ||
| """ | ||
| email_message = EmailMessage() |
There was a problem hiding this comment.
Missing keyword-only argument enforcement
Medium Severity
The validate_image_is_image function signature is missing the *, parameter that enforces keyword-only arguments, while all four other validators (validate_image_field_given, validate_image_file_size, validate_image_dimensions, validate_image_format) correctly include it. This inconsistency breaks the API contract and allows positional arguments where they weren't previously permitted, potentially breaking external code that relied on this enforcement.


Summary
Refactors five image validators to eliminate duplicate multipart parsing code by extracting it into a reusable
_parse_multipart_fileshelper with@beartypedecoration. Also simplifiesvalidate_image_is_imageby directly accessing the stream from the files dict.Changes
_parse_multipart_files()helper function that encapsulates the common multipart parsing patternvalidate_image_field_given,validate_image_file_size,validate_image_dimensions,validate_image_format,validate_image_is_image) now use the helper🤖 Generated with Claude Code
Note
Low Risk
Low risk refactor that centralizes multipart parsing used by multiple image validators; behavior should remain the same but any parsing regression would affect all five validations.
Overview
Refactors query image validation to eliminate duplicated multipart parsing by introducing
_parse_multipart_files()and updating the five image validators to use it.Also simplifies
validate_image_is_imageto read the uploaded image stream directly from the parsed files.Written by Cursor Bugbot for commit 641a748. This will update automatically on new commits. Configure here.