Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible types for json parameter to Route.respond #290

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

viccie30
Copy link

According to the Python documentation, more than just strings, dicts, and lists can be encoded to JSON.

@lundberg
Copy link
Owner

lundberg commented Jan 24, 2025

You're right @viccie30, though we need to align with the httpx api here, rather than python's, since thats what we're mocking.

Looks like httpx is typing json as Any, probably due to the option of switching in custom json library over build-int 🤔

Also, the python docs you're referring to touches encoding, e.g. python accepts tuples when dumping to json, but the response we're talking about here is about a loaded/decoded json ...

>>> json.loads(json.dumps(("httpx", "respx")))
['httpx', 'respx']

@lundberg
Copy link
Owner

Looks like we've diverged from httpx on the request/encoding side as well .. also typed as Any

@viccie30
Copy link
Author

Also, the python docs you're referring to touches encoding, e.g. python accepts tuples when dumping to json, but the response we're talking about here is about a loaded/decoded json ...

Oh right, because this is mocking the returned value of httpx.Response's json property.

I think you're right therefore to copy httpx's annotations.

@lundberg
Copy link
Owner

I think you're right therefore to copy httpx's annotations.

Yes, let's change to Any for the respond/response.

About the request/encoding side, I think we're fine with current typing. We're mimicking httpx api there as well, but we're not patching/mocking, but rather pattern matching encoded request json.

@g-as
Copy link

g-as commented Jan 24, 2025

The current state of the art:
python/typing#182 (comment)

@lundberg
Copy link
Owner

lundberg commented Jan 27, 2025

The current state of the art: python/typing#182 (comment)

Might be worth discussing, but since json.loads is typed to return Any, it may be incorrect to type it as your linked suggestion @g-as, even though I agree it would be neat.

Sticking with how httpx is typed is probably what we're "forced" to do .. maybe take your suggestion to a httpx and discuss the Any choice vs Json type there?

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.

3 participants