Skip to content

Replace PreparedRequest with a library-agnostic internal request type #2974

@adamtheturtle

Description

@adamtheturtle

Summary

The handler layer currently uses requests.PreparedRequest as its internal request representation. This ties the core business logic to the requests library unnecessarily, since handlers immediately decompose it into primitive types (str, bytes, Mapping[str, str]) before passing them to validators.

Problem

  • The respx adapter must convert httpx.RequestPreparedRequest, which caused type-checking conflicts between mypy, pyrefly, and ruff (see Add respx support for mocking httpx requests #2973)
  • The Flask server path already doesn't use PreparedRequest — it extracts primitives directly from Flask/werkzeug's request object
  • PreparedRequest.body is bytes | str | None, requiring a _body_bytes() normalizer in two separate files
  • PreparedRequest.method is str | None, requiring or "" guards in every handler

Proposed Solution

Replace PreparedRequest with a simple, library-agnostic dataclass:

@dataclass(frozen=True)
class MockRequest:
    method: str
    path: str
    headers: Mapping[str, str]
    body: bytes

Each adapter converts to this at the boundary:

  • responses adapter: PreparedRequestMockRequest
  • respx adapter: httpx.RequestMockRequest
  • Flask server: flask.requestMockRequest

Benefits

  • Removes dependency on requests types in the handler/validator layer
  • Eliminates type-checking workarounds (# type: ignore, # noqa) in the respx adapter
  • Removes the duplicated _body_bytes() helper (body normalization happens once at conversion)
  • Removes or "" guards for method (normalization happens once at conversion)
  • Makes the architecture consistent — all three adapters convert to the same internal type

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions