Skip to content

Extract multipart parsing into _parse_multipart_files helper#2958

Merged
adamtheturtle merged 1 commit intomainfrom
adamtheturtle/image-validator-cleanup
Feb 18, 2026
Merged

Extract multipart parsing into _parse_multipart_files helper#2958
adamtheturtle merged 1 commit intomainfrom
adamtheturtle/image-validator-cleanup

Conversation

@adamtheturtle
Copy link
Member

@adamtheturtle adamtheturtle commented Feb 18, 2026

Summary

Refactors five image validators to eliminate duplicate multipart parsing code by extracting it into a reusable _parse_multipart_files helper with @beartype decoration. Also simplifies validate_image_is_image by directly accessing the stream from the files dict.

Changes

  • New _parse_multipart_files() helper function that encapsulates the common multipart parsing pattern
  • All five validators (validate_image_field_given, validate_image_file_size, validate_image_dimensions, validate_image_format, validate_image_is_image) now use the helper
  • Removed ~50 lines of duplicated code while improving maintainability

🤖 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_image to 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.

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>
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Fix in Cursor Fix in Web

Raises:
BadImageError: Image data is given and it is not an image file.
"""
email_message = EmailMessage()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Fix in Cursor Fix in Web

@adamtheturtle adamtheturtle merged commit 7bf40c4 into main Feb 18, 2026
104 of 106 checks passed
@adamtheturtle adamtheturtle deleted the adamtheturtle/image-validator-cleanup branch February 18, 2026 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant