fediverse-devnet / feditest

A testing framework for distributed, heterogeneous systems communicating with complex protocols, such as the Fediverse
https://feditest.org/
MIT License
31 stars 6 forks source link

AnyObject: json_field error #288

Closed steve-bate closed 1 month ago

steve-bate commented 1 month ago

In 'AnyObject', 'json_field' is defined like:

    def json_field(self, name:str):
        """
        Convenience method to access field 'name' in the JSON.
        """
        self._ensure_fetched()
        json = cast(dict, self._json)
        return json.get(name)

However, it appears that self._json is an httpx.Response.

    def _ensure_fetched(self):
        """
        Make sure the uri has been dereferenced.

        Note: this could potentially be a smart factory, but currently it is not because
        we'd have to figure out when to expire the cache and that has some time.
        """
        if not self._json:
            self._json = httpx.get(...)

The get operation is not valid for an httpx.Response. The cast in json_field doesn't do a type conversion to a dict. It just overrides the type hint.

I'm not sure what the intention is here, but it seems that self._json should be typed as a dict|None and be set to httpx.Response.json? It may make sense to initialize the self._json in the constructor and avoid the None checking.