diff --git a/src/mistralai/models/updateftmodelin.py b/src/mistralai/models/updateftmodelin.py index 1bd0eaf..1ced33c 100644 --- a/src/mistralai/models/updateftmodelin.py +++ b/src/mistralai/models/updateftmodelin.py @@ -18,29 +18,35 @@ class UpdateFTModelIn(BaseModel): @model_serializer(mode="wrap") def serialize_model(self, handler): - optional_fields = ["name", "description"] - nullable_fields = ["name", "description"] - null_default_fields = [] + # Convert lists to sets for O(1) membership tests + optional_fields = {"name", "description"} + nullable_fields = {"name", "description"} + null_default_fields = set() serialized = handler(self) m = {} - for n, f in type(self).model_fields.items(): + # Cache intersection method and field values for performance + fields_set = self.__pydantic_fields_set__ + + # Avoid repeated lookups in loop + model_fields_items = type(self).model_fields.items() + + for n, f in model_fields_items: k = f.alias or n - val = serialized.get(k) - serialized.pop(k, None) + # Use get and pop only once + val = serialized.pop(k, None) optional_nullable = k in optional_fields and k in nullable_fields - is_set = ( - self.__pydantic_fields_set__.intersection({n}) - or k in null_default_fields - ) # pylint: disable=no-member + # Use set intersection directly on k + is_set = n in fields_set or k in null_default_fields # pylint: disable=no-member + # Reduce number of comparisons and branches if val is not None and val != UNSET_SENTINEL: m[k] = val elif val != UNSET_SENTINEL and ( - not k in optional_fields or (optional_nullable and is_set) + k not in optional_fields or (optional_nullable and is_set) ): m[k] = val