From 4bc25a414926fa7269dc1781f62c1ca4c0ca4aa7 Mon Sep 17 00:00:00 2001 From: Ben Brady Date: Thu, 28 Aug 2025 18:33:31 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20added=20comments=20to=20type=20c?= =?UTF-8?q?hecks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maxim Martynov --- fastapi/encoders.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/fastapi/encoders.py b/fastapi/encoders.py index 851fdc3ee..0d46c059b 100644 --- a/fastapi/encoders.py +++ b/fastapi/encoders.py @@ -236,9 +236,12 @@ def encode_value( sqlalchemy_safe: bool = True, ) -> Any: if custom_encoder: - if type(obj) in custom_encoder: - return custom_encoder[type(obj)](obj) + # fastpath for exact class match + obj_type = type(obj) + if obj_type in custom_encoder: + return custom_encoder[obj_type](obj) + # fallback to isinstance which uses MRO for encoder_type, encoder_instance in custom_encoder.items(): if isinstance(obj, encoder_type): return encoder_instance(obj)