apify / apify-client-python

Apify API client for Python
https://docs.apify.com/api/client/python/
Apache License 2.0
44 stars 11 forks source link

Address accessing of non-existing field `_maybe_parsed_body` within `httpx.Response` object #166

Open vdusek opened 9 months ago

vdusek commented 9 months ago

It seems that the following code:

response: httpx.Response = await self.http_client.call(
    url=self._url(f'records/{key}'),
    method='GET',
    params=self._params(),
)

returns a response of type httpx.Response. Later the field _maybe_parsed_body is accessed:

return {
    'value': response._maybe_parsed_body,
    '...': '...',
}

There are 2 occurrences of this:

Check if http_client.call really returns a httpx.Response object and in such case fix the accessing of non-existing field _maybe_parsed_body.

fnesveda commented 9 months ago

http_client.call returns a httpx.Response which is in some cases enhanced with a _maybe_parsed_body. It's a bit of a hack which was really useful when we still used requests, and simplified the code a lot, but when we switched to using httpx and did some more changes, it ended up being used only in one method, when fetching a key-value store record body. So now we could put the "maybe" response parsing into that method, instead of having it in the http_client, to have a better separation of concerns, and less hacks.

vdusek commented 9 months ago

My IDE was not able to recognize that, instead it raised an accessing of non-existing field error. The self.http_client.call is annotated to return httpx.Response object. So it might be the source of the problem.

fnesveda commented 9 months ago

Yeah, that's probably it. I wonder how this ever passed lint without a #noqa comment 🤷

vdusek commented 9 months ago

Yeah, that's probably it. I wonder how this ever passed lint without a #noqa comment 🤷

I think because accessing fields is checked by Mypy. That's the reason why there is # type: ignore .