From a4f0c844573c2837f5dbd030abc6df75e2e04a33 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 1 Jan 2024 20:25:23 -1000 Subject: [PATCH] Reduce duplicate code in json_loads (#106859) --- homeassistant/util/json.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/homeassistant/util/json.py b/homeassistant/util/json.py index 1af35c604eb..83ddd373992 100644 --- a/homeassistant/util/json.py +++ b/homeassistant/util/json.py @@ -39,9 +39,10 @@ def json_loads(__obj: bytes | bytearray | memoryview | str) -> JsonValueType: This adds a workaround for orjson not handling subclasses of str, https://github.com/ijl/orjson/issues/445. """ - if type(__obj) in (bytes, bytearray, memoryview, str): - return orjson.loads(__obj) # type:ignore[no-any-return] - if isinstance(__obj, str): + # Avoid isinstance overhead for the common case + if type(__obj) not in (bytes, bytearray, memoryview, str) and isinstance( + __obj, str + ): return orjson.loads(str(__obj)) # type:ignore[no-any-return] return orjson.loads(__obj) # type:ignore[no-any-return]