Repository: locustio/locust
File: locust/contrib/fasthttp.py (Python)

Code before:
    """Dict like object containing the response headers"""

    _response = None

    encoding: Optional[str] = None
    """In some cases setting the encoding explicitly is needed. If so, do it before calling .text"""

    @property
    def text(self) -> Optional[str]:
        """
        Returns the text content of the response as a decoded string
        """
        if self.content is None:
            return None
        if self.encoding is None:
            if self.headers is None:
                self.encoding = "utf-8"
            else:
                self.encoding = self.headers.get("content-type", "").partition("charset=")[2] or "utf-8"
        return str(self.content, self.encoding, errors="replace")

    def json(self) -> dict:
        """
        Parses the response as json and returns a dict
        """
        if self.text is not None:
            return json.loads(self.text)
        else:
            return {}

    def raise_for_status(self):
        """Raise any connection errors that occurred during the request"""
        if hasattr(self, "error") and self.error:
            raise self.error

    @property
    def status_code(self) -> int:
        """
        We override status_code in order to return None if no valid response was
        returned. E.g. in the case of connection errors
        """
        return self._response is not None and self._response.get_code() or 0

    def _content(self):
        if self.headers is None:
            return None
        return super()._content()

    def success(self):
        raise LocustError(
            "If you want to change the state of the request, you must pass catch_response=True. See http://docs.locust.io/en/stable/writing-a-locustfile.html#validating-responses"

Review comment (suggestion):
This changes actual behaviour. Better to add type ignore (it could definitely be argued that your implementation is better, but this would be a breaking change)

Code after:
    """Dict like object containing the response headers"""

    _response = None

    encoding: Optional[str] = None
    """In some cases setting the encoding explicitly is needed. If so, do it before calling .text"""

    @property
    def text(self) -> Optional[str]:
        """
        Returns the text content of the response as a decoded string
        """
        if self.content is None:
            return None
        if self.encoding is None:
            if self.headers is None:
                self.encoding = "utf-8"
            else:
                self.encoding = self.headers.get("content-type", "").partition("charset=")[2] or "utf-8"
        return str(self.content, self.encoding, errors="replace")

    def json(self) -> dict:
        """
        Parses the response as json and returns a dict
        """
        return json.loads(self.text)  # type: ignore

    def raise_for_status(self):
        """Raise any connection errors that occurred during the request"""
        if hasattr(self, "error") and self.error:
            raise self.error

    @property
    def status_code(self) -> int:
        """
        We override status_code in order to return None if no valid response was
        returned. E.g. in the case of connection errors
        """
        return self._response is not None and self._response.get_code() or 0

    def _content(self):
        if self.headers is None:
            return None
        return super()._content()

    def success(self):
        raise LocustError(
            "If you want to change the state of the request, you must pass catch_response=True. See http://docs.locust.io/en/stable/writing-a-locustfile.html#validating-responses"
        )

    def failure(self):