respx icon indicating copy to clipboard operation
respx copied to clipboard

Update Route.respond json type hint to Any to match HTTPX

Open JacobHayes opened this issue 11 months ago • 5 comments

The json parameter to Route.respond is missing hints for the List and Dict elements, which makes pyright sad in strict mode:

.../tests/test_url.py:6:5 - error: Type of "respond" is partially unknown
    Type of "respond" is "(status_code: int = 200, *, headers: Headers | Dict[str, str] | Dict[bytes, bytes] | Sequence[Tuple[str, str]] | Sequence[Tuple[bytes, bytes]] | None = None, cookies: Dict[str, str] | Sequence[Tuple[str, str]] | Sequence[SetCookie] | None = None, content: str | bytes | Iterable[bytes] | AsyncIterable[bytes] | None = None, text: str | None = None, html: str | None = None, json: str | List[Unknown] | Dict[Unknown, Unknown] | None = None, stream: SyncByteStream | AsyncByteStream | None = None, content_type: str | None = None, http_version: str | None = None, **kwargs: Any) -> Route" (reportUnknownMemberType)
1 error, 0 warnings, 0 informations

This PR just adds the hints for the elements. I think python's json.dump will accept arbitrary key types, but the JSON spec only defines strings so I used Dict[str, ...]. I'm happy to change that to Any if you'd prefer.

Thanks for the helpful library!

JacobHayes avatar Jan 11 '25 17:01 JacobHayes

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 100.00%. Comparing base (1f3626d) to head (d477ddc). Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #284   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           22        22           
  Lines         2915      2915           
  Branches       192       192           
=========================================
  Hits          2915      2915           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Jan 21 '25 08:01 codecov[bot]

@JacobHayes, new discussions regarding this has come up in #290 and we're leaning towards typing json as Any instead, to align with httpx. That should please pyright for you as well, agree?

lundberg avatar Jan 24 '25 11:01 lundberg

Yup, that should make pyright happy too! Just updated this PR to use Any, but feel free to merge whichever PR makes sense.

JacobHayes avatar Jan 24 '25 16:01 JacobHayes

Just a ping. Hoping this can get in so Pyright will get off my back. :P

mikenerone avatar Jul 15 '25 16:07 mikenerone

FYI @lundberg I'm guessing the nox version has changed and is causing the CI issues (which just does pip install nox, w/o pinning a version).

JacobHayes avatar Aug 11 '25 15:08 JacobHayes