From 02173faa5b78301e03da68690a7c21a9c0e53661 Mon Sep 17 00:00:00 2001 From: nickyoung-github Date: Tue, 24 Dec 2024 10:48:31 -0500 Subject: [PATCH] Store descriptor fields on the model, rather than getting for each instance --- src/serializers/type_serializers/model.rs | 20 +++++++++++++------- uv.lock | 10 +++++----- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/serializers/type_serializers/model.rs b/src/serializers/type_serializers/model.rs index 45a7a1184..aaad860aa 100644 --- a/src/serializers/type_serializers/model.rs +++ b/src/serializers/type_serializers/model.rs @@ -79,6 +79,7 @@ pub struct ModelSerializer { has_extra: bool, root_model: bool, name: String, + descriptor_fields: Py, } impl BuildSerializer for ModelSerializer { @@ -100,12 +101,21 @@ impl BuildSerializer for ModelSerializer { let root_model = schema.get_as(intern!(py, "root_model"))?.unwrap_or(false); let name = class.bind(py).getattr(intern!(py, "__name__"))?.extract()?; + let try_descriptor_fields = class.bind(py).getattr(intern!(py, "__pydantic_descriptor_fields__")); + + let descriptor_fields = if try_descriptor_fields.is_ok() { + try_descriptor_fields?.downcast_into::()? + } else { + PySet::empty(py)? + }; + Ok(Self { class, serializer, has_extra: has_extra(schema, config.as_ref())?, root_model, name, + descriptor_fields: descriptor_fields.into(), } .into()) } @@ -130,14 +140,10 @@ impl ModelSerializer { fn get_inner_value<'py>(&self, model: &Bound<'py, PyAny>, extra: &Extra) -> PyResult> { let py = model.py(); let mut attrs = model.getattr(intern!(py, "__dict__"))?.downcast_into::()?; - let try_descriptor_fields = model.getattr(intern!(py, "__pydantic_descriptor_fields__")); - if try_descriptor_fields.is_ok() { - let descriptor_fields = try_descriptor_fields?.downcast_into::()?; - for f in descriptor_fields { - let field = f.downcast_into::()?; - attrs.set_item(&field, model.getattr(&field)?)?; - } + for f in self.descriptor_fields.bind(py) { + let field = f.downcast_into::()?; + attrs.set_item(&field, model.getattr(&field)?)?; } if extra.exclude_unset { diff --git a/uv.lock b/uv.lock index 17e350df9..0c963a7c0 100644 --- a/uv.lock +++ b/uv.lock @@ -1,8 +1,8 @@ version = 1 requires-python = ">=3.8" resolution-markers = [ - "python_full_version < '3.9' or (python_full_version < '3.12' and implementation_name != 'cpython') or (python_full_version < '3.12' and platform_machine != 'x86_64')", - "(python_full_version == '3.12.*' and implementation_name != 'cpython') or (python_full_version == '3.12.*' and platform_machine != 'x86_64') or (python_full_version == '3.13.*' and implementation_name != 'cpython') or python_full_version >= '3.14'", + "(python_full_version < '3.12' and implementation_name != 'cpython') or (python_full_version < '3.9' and implementation_name == 'cpython' and platform_machine == 'x86_64') or (python_full_version < '3.12' and platform_machine != 'x86_64')", + "(python_full_version >= '3.12' and implementation_name != 'cpython') or (python_full_version >= '3.14' and implementation_name == 'cpython') or (python_full_version == '3.12.*' and implementation_name == 'cpython' and platform_machine != 'x86_64')", "python_full_version == '3.13.*' and implementation_name == 'cpython'", "python_full_version >= '3.9' and python_full_version < '3.11' and implementation_name == 'cpython' and platform_machine == 'x86_64'", "python_full_version == '3.11.*' and implementation_name == 'cpython' and platform_machine == 'x86_64'", @@ -26,8 +26,8 @@ name = "astunparse" version = "1.6.3" source = { registry = "https://pypi.org/simple" } dependencies = [ - { name = "six", marker = "python_full_version < '3.9' or (python_full_version < '3.12' and implementation_name != 'cpython') or (python_full_version < '3.12' and platform_machine != 'x86_64')" }, - { name = "wheel", marker = "python_full_version < '3.9' or (python_full_version < '3.12' and implementation_name != 'cpython') or (python_full_version < '3.12' and platform_machine != 'x86_64')" }, + { name = "six", marker = "(python_full_version < '3.12' and implementation_name != 'cpython') or (python_full_version < '3.9' and implementation_name == 'cpython' and platform_machine == 'x86_64') or (python_full_version < '3.12' and platform_machine != 'x86_64')" }, + { name = "wheel", marker = "(python_full_version < '3.12' and implementation_name != 'cpython') or (python_full_version < '3.9' and implementation_name == 'cpython' and platform_machine == 'x86_64') or (python_full_version < '3.12' and platform_machine != 'x86_64')" }, ] sdist = { url = "https://files.pythonhosted.org/packages/f3/af/4182184d3c338792894f34a62672919db7ca008c89abee9b564dd34d8029/astunparse-1.6.3.tar.gz", hash = "sha256:5ad93a8456f0d084c3456d059fd9a92cce667963232cbf763eac3bc5b7940872", size = 18290 } wheels = [ @@ -624,7 +624,7 @@ wheels = [ [[package]] name = "pydantic-core" -version = "2.27.2" +version = "2.27.1" source = { virtual = "." } dependencies = [ { name = "typing-extensions" },