From c08230cb237f8103c91ef0994ef91cecc5065d60 Mon Sep 17 00:00:00 2001 From: xmo-odoo Date: Thu, 10 Jul 2025 09:37:23 +0200 Subject: [PATCH] Reduce overhead of `Response.ok` Every time `ok` is used it feels like it should be essentially free, but it's not as it goes through `raise_for_status` which does a bunch of preparatory work to manage the reason and check for the two main error categories separately. Reduce `ok` to just check for what it says it does in the first line. Also makes the behaviour consistent with the docstring: before this change `ok` would return `True` if the status code is 600 or above (which some API providers are known to do e.g. Shopify can return 783[^1], LinkedIn has been known to return 999[^2]. However for that reason it is, technically, a breaking change. [^1]: https://shopify.dev/docs/api/usage/response-codes [^2]: https://stackoverflow.com/questions/27231113/999-error-code-on-head-request-to-linkedin --- src/requests/models.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/requests/models.py b/src/requests/models.py index c4b25fa079..e05dd0d475 100644 --- a/src/requests/models.py +++ b/src/requests/models.py @@ -760,11 +760,7 @@ def ok(self): the status code is between 200 and 400, this will return True. This is **not** a check to see if the response code is ``200 OK``. """ - try: - self.raise_for_status() - except HTTPError: - return False - return True + return self.status_code < 400 @property def is_redirect(self):