-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Use a JSON
return type for requests.Response.json
#9335
Comments
Unfortunately, no. As I state in the comment you link:
Typeshed typically avoids union return types in cases where callers typically know their structure, since we prefer to avoid false positives over false negatives. You may be interested in python/typing#1096 |
Ok good to know that we are trying to avoid union return types! So for instance when making an API wrapper using class MyClient:
...
def get_ressource(self) -> dict[str, Any]:
return cast(dict[str, Any], requests.get("http://api.example.com")) If I know that my ressource will have the type Thanks in advance |
Yes, that could work, but I'd do it slightly differently:
This avoids the cast, which is as unsafe as That is, possible solutions are:
|
Great catch indeed |
As stated here, recursive type aliases are supported by most type checkers, and a
JSON
type can now be properly defined. I'd like to know if changing the return type of thejson
method to this could be feasible.typeshed/stubs/requests/requests/models.pyi
Lines 129 to 139 in 89fb1d5
The text was updated successfully, but these errors were encountered: