DEV Community

Discussion on: HTTP Calls in Python Without Requests or Other External Dependencies

Collapse
amal profile image
Amal Shaji

Great article!!

Can we have def json(self) -> Optional[Dict] instead of def json(self) -> typing.Any in Response class?

Collapse
bowmanjd profile image
Jonathan Bowman Author

You certainly can, as long as you are 100% sure that all your API calls will only return a JSON object, not a number, string, boolean, or null. Since JSON supports all those types, one cannot be assured that only a JSON object (the equivalent of a Python dict) will be returned.

I know of several APIs that may return an array instead of just an object, so I would be somewhat concerned with this approach. But if you know or control the endpoints and return values, then you can define this how you would like.