From a66c70a6a5caa7bd6896a51e9edd6ce8d01514f1 Mon Sep 17 00:00:00 2001 From: antisch Date: Fri, 15 May 2026 09:43:07 +1200 Subject: [PATCH 1/8] Speed up xml deserialization --- .../codegen/serializers/model_serializer.py | 87 ++++- .../codegen/templates/model_base.py.jinja2 | 366 +++++++++++++++--- .../codegen/templates/serialization.py.jinja2 | 21 + .../unit/test_model_base_xml_serialization.py | 216 +++++++++++ 4 files changed, 645 insertions(+), 45 deletions(-) diff --git a/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py b/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py index d428a113e5e..a189a9a6301 100644 --- a/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py +++ b/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py @@ -6,13 +6,65 @@ from typing import Optional from abc import ABC, abstractmethod -from ..models import ModelType, Property, ConstantType, EnumValue +from ..models import ModelType, Property, ConstantType, EnumValue, EnumType from ..models.imports import FileImport, TypingSection, MsrestImportType, ImportType +from ..models.primitive_types import ( + BooleanType, + IntegerType, + FloatType, + DecimalType, + StringType, + DatetimeType, + UnixTimeType, + TimeType, + DateType, + DurationType, + ByteArraySchema, +) from .import_serializer import FileImportSerializer from .base_serializer import BaseSerializer from ..models.utils import NamespaceType +def _get_xml_deserializer_name(prop: Property) -> Optional[str]: + """Return the _xml_deser_* function name for a scalar XML property, or None.""" + prop_type = prop.type + # Unwrap ConstantType to get the underlying value type + if isinstance(prop_type, ConstantType): + prop_type = prop_type.value_type + + if isinstance(prop_type, StringType): + return "_xml_deser_str" + if isinstance(prop_type, IntegerType): + return "_xml_deser_int" + if isinstance(prop_type, FloatType): + return "_xml_deser_float" + if isinstance(prop_type, BooleanType): + return "_xml_deser_bool" + if isinstance(prop_type, DecimalType): + return "_xml_deser_decimal" + if isinstance(prop_type, DatetimeType): + if prop_type.encode == "rfc7231": + return "_xml_deser_datetime_rfc7231" + return "_xml_deser_datetime" + if isinstance(prop_type, UnixTimeType): + return "_xml_deser_datetime_unix_timestamp" + if isinstance(prop_type, TimeType): + return "_xml_deser_time" + if isinstance(prop_type, DateType): + return "_xml_deser_date" + if isinstance(prop_type, DurationType): + return "_xml_deser_duration" + if isinstance(prop_type, ByteArraySchema): + encode = getattr(prop_type, "encode", None) or getattr(prop, "encode", None) + if encode == "base64url": + return "_xml_deser_bytes_base64url" + return "_xml_deser_bytes" + if isinstance(prop_type, EnumType): + return f"enum:{prop_type.name}" + return None + + def _documentation_string(prop: Property, description_keyword: str, docstring_type_keyword: str) -> list[str]: retval: list[str] = [] sphinx_prefix = f":{description_keyword} {prop.client_name}:" @@ -253,6 +305,8 @@ def imports(self) -> FileImport: TypingSection.REGULAR, alias="_Model", ) + # Collect XML deserializer functions needed by models in this file + xml_deser_names: set[str] = set() for model in self.models: if model.base == "json": continue @@ -271,6 +325,11 @@ def imports(self) -> FileImport: called_by_property=True, ) ) + # Track XML deserializer functions needed + if prop.xml_metadata: + deser_name = _get_xml_deserializer_name(prop) + if deser_name: + xml_deser_names.add(deser_name) for parent in model.parents: if parent.client_namespace != model.client_namespace: file_import.add_submodule_import( @@ -285,6 +344,24 @@ def imports(self) -> FileImport: file_import.add_submodule_import("typing", "overload", ImportType.STDLIB) file_import.add_submodule_import("typing", "Mapping", ImportType.STDLIB) file_import.add_submodule_import("typing", "Any", ImportType.STDLIB) + # Add imports for XML deserializer functions + has_enum_deser = False + for deser_name in sorted(xml_deser_names): + if deser_name.startswith("enum:"): + has_enum_deser = True + continue + file_import.add_submodule_import( + self.code_model.get_relative_import_path(self.serialize_namespace, module_name="_utils.model_base"), + deser_name, + ImportType.LOCAL, + ) + if has_enum_deser: + file_import.add_import("functools", ImportType.STDLIB) + file_import.add_submodule_import( + self.code_model.get_relative_import_path(self.serialize_namespace, module_name="_utils.model_base"), + "_xml_deser_enum_or_str", + ImportType.LOCAL, + ) # if there is a property named `list` we have to make sure there's no conflict with the built-in `list` if self.code_model.has_property_named_list: file_import.define_mypy_type("List", "list") @@ -337,6 +414,14 @@ def declare_property(self, prop: Property) -> str: if prop.xml_metadata: args.append(f"xml={prop.xml_metadata}") + # Add fast-path deserializer for scalar XML fields + deser_name = _get_xml_deserializer_name(prop) + if deser_name: + if deser_name.startswith("enum:"): + enum_name = deser_name[5:] + args.append(f"deserializer=functools.partial(_xml_deser_enum_or_str, {enum_name})") + else: + args.append(f"deserializer={deser_name}") if prop.original_tsp_name: args.append(f'original_tsp_name="{prop.original_tsp_name}"') diff --git a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 index 19f8f222acf..b5070e3c341 100644 --- a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 +++ b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 @@ -612,6 +612,235 @@ def _create_value(rf: typing.Optional["_RestField"], value: typing.Any) -> typin return _serialize(value, rf._format) +# ============================================================================ +# Fast-path scalar deserializer functions for rest_field(deserializer=...) +# These are referenced from rest_field declarations to bypass the generic +# _deserialize -> _deserialize_with_callable chain. +# Only simple/primitive types — no models or container types. +# ============================================================================ + + +def _xml_deser_str(value): + if isinstance(value, ET.Element): + return value.text or "" + return str(value) if value is not None else None + + +def _xml_deser_int(value): + if isinstance(value, ET.Element): + return int(value.text) if value.text else None + return int(value) if value is not None else None + + +def _xml_deser_float(value): + if isinstance(value, ET.Element): + return float(value.text) if value.text else None + return float(value) if value is not None else None + + +def _xml_deser_bool(value): + if isinstance(value, ET.Element): + text = value.text + else: + text = value + if text is None: + return None + if text in (True, False): + return text + return text.lower() == "true" + + +def _xml_deser_bytes(value): + """Deserialize bytes from XML (base64).""" + if isinstance(value, ET.Element): + text = value.text + else: + text = value + if text is None: + return None + return _deserialize_bytes(text) + + +def _xml_deser_bytes_base64url(value): + """Deserialize bytes from XML (base64url).""" + if isinstance(value, ET.Element): + text = value.text + else: + text = value + if text is None: + return None + return _deserialize_bytes_base64(text) + + +def _xml_deser_datetime(value): + """Deserialize a datetime from XML (ISO 8601 / rfc3339).""" + if isinstance(value, ET.Element): + text = value.text + else: + text = value + if text is None: + return None + return _deserialize_datetime(text) + + +def _xml_deser_datetime_rfc7231(value): + """Deserialize a datetime from XML (RFC7231 format).""" + if isinstance(value, ET.Element): + text = value.text + else: + text = value + if text is None: + return None + return _deserialize_datetime_rfc7231(text) + + +def _xml_deser_datetime_unix_timestamp(value): + """Deserialize a datetime from XML (Unix timestamp).""" + if isinstance(value, ET.Element): + text = value.text + else: + text = value + if text is None: + return None + return _deserialize_datetime_unix_timestamp(float(text)) + + +def _xml_deser_date(value): + """Deserialize a date from XML (ISO 8601).""" + if isinstance(value, ET.Element): + text = value.text + else: + text = value + if text is None: + return None + return _deserialize_date(text) + + +def _xml_deser_time(value): + """Deserialize a time from XML (ISO 8601).""" + if isinstance(value, ET.Element): + text = value.text + else: + text = value + if text is None: + return None + return _deserialize_time(text) + + +def _xml_deser_duration(value): + """Deserialize a timedelta from XML (ISO 8601 duration).""" + if isinstance(value, ET.Element): + text = value.text + else: + text = value + if text is None: + return None + return _deserialize_duration(text) + + +def _xml_deser_decimal(value): + """Deserialize a Decimal from XML.""" + if isinstance(value, ET.Element): + text = value.text + else: + text = value + if text is None: + return None + return _deserialize_decimal(text) + + +def _xml_deser_enum_or_str(enum_cls, value): + """Deserialize a Union[EnumType, str] from XML.""" + text = value.text if isinstance(value, ET.Element) else value + if text is None: + return None + try: + return enum_cls(text) + except ValueError: + return text + + +def _extract_xml_model_type(rf_type): + """Extract the concrete Model class from a resolved rf._type partial chain. + + Unwraps ``Optional[Model]`` and ``_deserialize_model(Model, ...)`` + wrappers. Only handles Model and Optional[Model] — other composite + types (List, Dict, Union, etc.) return None and fall through to the + generic ``_deserialize`` path at runtime. + """ + if rf_type is None: + return None + if isinstance(rf_type, type) and _is_model(rf_type): + return rf_type + if not isinstance(rf_type, functools.partial): + return None + func = rf_type.func + args = rf_type.args + if func is _deserialize_with_optional and args: + return _extract_xml_model_type(args[0]) + if func is _deserialize_model and args: + cls = args[0] + return cls if isinstance(cls, type) and _is_model(cls) else None + return None + + +def _build_xml_field_plan(cls, attr_to_rest_field: dict) -> list: + """Build a precomputed XML field plan for fast _init_from_xml iteration. + + Called once per model class in __new__. Returns a list of tuples: + (rest_name, xml_name, kind, deser, rf_type, is_optional, items_name) + + kind: 0=wrapped, 1=attribute, 2=unwrapped, 3=text + + For Model and Optional[Model] fields that lack a scalar + ``_deserializer``, this function precomputes the Model class as the + deserializer so ``_init_from_xml`` can call ``ModelClass(element)`` + directly instead of going through the expensive + ``_get_deserialize_callable_from_annotation`` chain at runtime. + """ + model_meta = getattr(cls, "_xml", {}) + model_ns = model_meta.get("ns") or model_meta.get("namespace") + plan = [] + + for rf in attr_to_rest_field.values(): + prop_meta = getattr(rf, "_xml", {}) + deser = rf._deserializer + + xml_name = prop_meta.get("name", rf._rest_name) + xml_ns = _resolve_xml_ns(prop_meta, model_meta) + if xml_ns: + xml_name = "{" + xml_ns + "}" + xml_name + + is_optional = rf._is_optional + + # For Model / Optional[Model] fields without a scalar deserializer, + # precompute the Model class as the deserializer. + if deser is None and rf._type is not None: + model_cls = _extract_xml_model_type(rf._type) + if model_cls is not None: + deser = model_cls + + if prop_meta.get("attribute", False): + plan.append((rf._rest_name, xml_name, 1, deser, rf._type, is_optional, None)) + elif prop_meta.get("unwrapped", False): + items_name = prop_meta.get("itemsName") + if items_name: + items_ns = prop_meta.get("itemsNs") + if items_ns is not None: + xml_ns = items_ns + if xml_ns: + items_name = "{" + xml_ns + "}" + items_name + else: + items_name = xml_name + plan.append((rf._rest_name, xml_name, 2, deser, rf._type, is_optional, items_name)) + elif prop_meta.get("text", False): + plan.append((rf._rest_name, xml_name, 3, deser, rf._type, is_optional, None)) + else: + plan.append((rf._rest_name, xml_name, 0, deser, rf._type, is_optional, None)) + + return plan + + class Model(_MyMutableMapping): _is_model = True # label whether current class's _attr_to_rest_field has been calculated @@ -660,53 +889,89 @@ class Model(_MyMutableMapping): :rtype: dict """ result: dict[str, typing.Any] = {} - model_meta = getattr(self, "_xml", {}) existed_attr_keys: list[str] = [] - for rf in self._attr_to_rest_field.values(): - prop_meta = getattr(rf, "_xml", {}) - xml_name = prop_meta.get("name", rf._rest_name) - xml_ns = _resolve_xml_ns(prop_meta, model_meta) - if xml_ns: - xml_name = "{" + xml_ns + "}" + xml_name - - # attribute - if prop_meta.get("attribute", False) and element.get(xml_name) is not None: - existed_attr_keys.append(xml_name) - result[rf._rest_name] = _deserialize(rf._type, element.get(xml_name)) - continue - - # unwrapped element is array - if prop_meta.get("unwrapped", False): - # unwrapped array could either use prop items meta/prop meta - _items_name = prop_meta.get("itemsName") - if _items_name: - xml_name = _items_name - _items_ns = prop_meta.get("itemsNs") - if _items_ns is not None: - xml_ns = _items_ns - if xml_ns: - xml_name = "{" + xml_ns + "}" + xml_name - items = element.findall(xml_name) # pyright: ignore - if len(items) > 0: + field_plan = getattr(self, "_xml_field_plan", None) + if field_plan: + for rest_name, xml_name, kind, deser, rf_type, is_optional, items_name in field_plan: + if kind == 0: # wrapped element (most common) + item = element.find(xml_name) + if item is not None: + existed_attr_keys.append(xml_name) + if deser: + result[rest_name] = deser(item) + else: + result[rest_name] = _deserialize(rf_type, item) + elif kind == 1: # attribute + attr_val = element.get(xml_name) + if attr_val is not None: + existed_attr_keys.append(xml_name) + if deser: + result[rest_name] = deser(attr_val) + else: + result[rest_name] = attr_val + elif kind == 2: # unwrapped array + items = element.findall(items_name) # pyright: ignore + if len(items) > 0: + existed_attr_keys.append(items_name) + if deser: + result[rest_name] = deser(items) + else: + result[rest_name] = _deserialize(rf_type, items) + elif not is_optional: + existed_attr_keys.append(items_name) + result[rest_name] = [] + elif kind == 3: # text + if element.text is not None: + if deser: + result[rest_name] = deser(element.text) + else: + result[rest_name] = element.text + else: + model_meta = getattr(self, "_xml", {}) + for rf in self._attr_to_rest_field.values(): + prop_meta = getattr(rf, "_xml", {}) + xml_name = prop_meta.get("name", rf._rest_name) + xml_ns = _resolve_xml_ns(prop_meta, model_meta) + if xml_ns: + xml_name = "{" + xml_ns + "}" + xml_name + + # attribute + if prop_meta.get("attribute", False) and element.get(xml_name) is not None: existed_attr_keys.append(xml_name) - result[rf._rest_name] = _deserialize(rf._type, items) - elif not rf._is_optional: + result[rf._rest_name] = _deserialize(rf._type, element.get(xml_name)) + continue + + # unwrapped element is array + if prop_meta.get("unwrapped", False): + _items_name = prop_meta.get("itemsName") + if _items_name: + xml_name = _items_name + _items_ns = prop_meta.get("itemsNs") + if _items_ns is not None: + xml_ns = _items_ns + if xml_ns: + xml_name = "{" + xml_ns + "}" + xml_name + items = element.findall(xml_name) # pyright: ignore + if len(items) > 0: + existed_attr_keys.append(xml_name) + result[rf._rest_name] = _deserialize(rf._type, items) + elif not rf._is_optional: + existed_attr_keys.append(xml_name) + result[rf._rest_name] = [] + continue + + # text element is primitive type + if prop_meta.get("text", False): + if element.text is not None: + result[rf._rest_name] = _deserialize(rf._type, element.text) + continue + + # wrapped element could be normal property or array + item = element.find(xml_name) + if item is not None: existed_attr_keys.append(xml_name) - result[rf._rest_name] = [] - continue - - # text element is primitive type - if prop_meta.get("text", False): - if element.text is not None: - result[rf._rest_name] = _deserialize(rf._type, element.text) - continue - - # wrapped element could be normal property or array, it should only have one element - item = element.find(xml_name) - if item is not None: - existed_attr_keys.append(xml_name) - result[rf._rest_name] = _deserialize(rf._type, item) + result[rf._rest_name] = _deserialize(rf._type, item) # rest thing is additional properties for e in element: @@ -745,6 +1010,9 @@ class Model(_MyMutableMapping): ._attr_to_rest_field.items() } {% endif %} + # Build XML field plan for fast _init_from_xml (only for XML models) + if getattr(cls, "_xml", None): + cls._xml_field_plan = _build_xml_field_plan(cls, attr_to_rest_field) cls._calculated.add(f"{cls.__module__}.{cls.__qualname__}") return super().__new__(cls) @@ -1132,6 +1400,7 @@ class _RestField: format: typing.Optional[str] = None, is_multipart_file_input: bool = False, xml: typing.Optional[dict[str, typing.Any]] = None, + deserializer: typing.Optional[typing.Callable] = None, {% if code_model.has_padded_model_property %} original_tsp_name: typing.Optional[str] = None, {% endif %} @@ -1147,6 +1416,7 @@ class _RestField: self._format = format self._is_multipart_file_input = is_multipart_file_input self._xml = xml if xml is not None else {} + self._deserializer = deserializer {% if code_model.has_padded_model_property %} self._original_tsp_name = original_tsp_name {% endif %} @@ -1185,7 +1455,11 @@ class _RestField: # Return the value from _data directly (it's been deserialized in place) return obj._data.get(self._rest_name) - deserialized = _deserialize(self._type, _serialize(item, self._format), rf=self) + # Fast path: use _deserializer directly (avoids _serialize/_deserialize chain) + if self._deserializer: + deserialized = self._deserializer(item) + else: + deserialized = _deserialize(self._type, _serialize(item, self._format), rf=self) # For mutable types, store the deserialized value back in _data # so mutations directly affect _data @@ -1231,6 +1505,7 @@ def rest_field( format: typing.Optional[str] = None, is_multipart_file_input: bool = False, xml: typing.Optional[dict[str, typing.Any]] = None, + deserializer: typing.Optional[typing.Callable] = None, {% if code_model.has_padded_model_property %} original_tsp_name: typing.Optional[str] = None, {% endif %} @@ -1243,6 +1518,7 @@ def rest_field( format=format, is_multipart_file_input=is_multipart_file_input, xml=xml, + deserializer=deserializer, {% if code_model.has_padded_model_property %} original_tsp_name=original_tsp_name, {% endif %} @@ -1481,6 +1757,8 @@ def _deserialize_xml( value: str, ) -> typing.Any: element = ET.fromstring(value) # nosec + if _is_model(deserializer): + return deserializer._deserialize(element, []) return _deserialize(deserializer, element) diff --git a/packages/http-client-python/generator/pygen/codegen/templates/serialization.py.jinja2 b/packages/http-client-python/generator/pygen/codegen/templates/serialization.py.jinja2 index de51bf9343f..986b872f07b 100644 --- a/packages/http-client-python/generator/pygen/codegen/templates/serialization.py.jinja2 +++ b/packages/http-client-python/generator/pygen/codegen/templates/serialization.py.jinja2 @@ -1411,6 +1411,27 @@ class Deserializer: :return: Deserialized object. :rtype: object """ + # Fast path for header deserialization: response_data is a plain str or None + # and target_obj is a simple scalar type. This avoids the expensive + # _unpack_content → _deserialize → _classify_target → deserialize_data chain. + if response_data is None: + return None + if target_obj == "str" and isinstance(response_data, str): + return response_data + if isinstance(response_data, str): + if target_obj == "int": + return int(response_data) + if target_obj == "bool": + if response_data in ("true", "1", "True"): + return True + if response_data in ("false", "0", "False"): + return False + return bool(response_data) + if target_obj == "rfc-1123": + return Deserializer.deserialize_rfc(response_data) + if target_obj == "bytearray": + return Deserializer.deserialize_bytearray(response_data) + data = self._unpack_content(response_data, content_type) return self._deserialize(target_obj, data) diff --git a/packages/http-client-python/tests/unit/test_model_base_xml_serialization.py b/packages/http-client-python/tests/unit/test_model_base_xml_serialization.py index f026fa6afd5..373f763eded 100644 --- a/packages/http-client-python/tests/unit/test_model_base_xml_serialization.py +++ b/packages/http-client-python/tests/unit/test_model_base_xml_serialization.py @@ -12,6 +12,7 @@ rest_field, rest_discriminator, _deserialize_xml, + _xml_deser_str, ) @@ -1237,3 +1238,218 @@ def __init__(self, *args, **kwargs): message_count_details=MessageCountDetails(active_message_count=12), ) assert_xml_equals(_get_element(xml_model), basic_xml) + + +class TestXmlDeserializerAttribute: + """Tests for the rest_field(deserializer=...) fast-path deserialization.""" + + def test_scalar_deserializers(self): + """Test that explicit deserializer functions work for scalar types.""" + from specialwords._utils.model_base import ( + _xml_deser_str, + _xml_deser_int, + _xml_deser_float, + _xml_deser_bool, + ) + + basic_xml = """ + + test + 42 + 12.34 + true + """ + + class XmlModel(Model): + name: str = rest_field(name="Name", xml={"name": "Name"}, deserializer=_xml_deser_str) + count: int = rest_field(name="Count", xml={"name": "Count"}, deserializer=_xml_deser_int) + price: float = rest_field(name="Price", xml={"name": "Price"}, deserializer=_xml_deser_float) + active: bool = rest_field(name="Active", xml={"name": "Active"}, deserializer=_xml_deser_bool) + country: str = rest_field( + name="country", xml={"name": "country", "attribute": True}, deserializer=_xml_deser_str + ) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + _xml = {"name": "Data"} + + result = _deserialize_xml(XmlModel, basic_xml) + assert result.name == "test" + assert result.count == 42 + assert result.price == 12.34 + assert result.active is True + assert result.country == "france" + + def test_datetime_deserializers(self): + """Test datetime deserializer variants.""" + from specialwords._utils.model_base import ( + _xml_deser_datetime, + _xml_deser_datetime_rfc7231, + ) + + basic_xml = """ + + 2023-01-15T10:30:00Z + Sun, 15 Jan 2023 10:30:00 GMT + """ + + class XmlModel(Model): + created: str = rest_field(name="Created", xml={"name": "Created"}, deserializer=_xml_deser_datetime) + modified: str = rest_field( + name="Modified", xml={"name": "Modified"}, deserializer=_xml_deser_datetime_rfc7231 + ) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + _xml = {"name": "Data"} + + import datetime + + result = _deserialize_xml(XmlModel, basic_xml) + assert isinstance(result.created, datetime.datetime) + assert result.created.year == 2023 + assert isinstance(result.modified, datetime.datetime) + assert result.modified.year == 2023 + + def test_field_plan_with_deserializers(self): + """Test that field plan is built and used when _xml is present.""" + from specialwords._utils.model_base import _xml_deser_str, _xml_deser_int + + basic_xml = """ + + widget + 100 + """ + + class Item(Model): + name: str = rest_field(name="Name", xml={"name": "Name"}, deserializer=_xml_deser_str) + qty: int = rest_field(name="Qty", xml={"name": "Qty"}, deserializer=_xml_deser_int) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + _xml = {"name": "Item"} + + result = _deserialize_xml(Item, basic_xml) + assert hasattr(Item, "_xml_field_plan") + assert len(Item._xml_field_plan) == 2 + assert result.name == "widget" + assert result.qty == 100 + + def test_mixed_deserializer_and_model_fields(self): + """Test models with both scalar deserializers and nested model fields.""" + from specialwords._utils.model_base import _xml_deser_str, _xml_deser_int + + basic_xml = """ + + my-container + + widget + 5 + + """ + + class Item(Model): + name: str = rest_field(name="Name", xml={"name": "Name"}, deserializer=_xml_deser_str) + count: int = rest_field(name="Count", xml={"name": "Count"}, deserializer=_xml_deser_int) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + _xml = {"name": "Item"} + + class Container(Model): + name: str = rest_field(name="Name", xml={"name": "Name"}, deserializer=_xml_deser_str) + item: Item = rest_field(name="Item", xml={"name": "Item"}) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + _xml = {"name": "Container"} + + result = _deserialize_xml(Container, basic_xml) + assert result.name == "my-container" + assert result.item.name == "widget" + assert result.item.count == 5 + + def test_no_field_plan_without_xml(self): + """Non-XML models should not get _xml_field_plan.""" + + class JsonModel(Model): + name: str = rest_field(name="name") + count: int = rest_field(name="count") + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + m = JsonModel({"name": "test", "count": 42}) + assert not hasattr(JsonModel, "_xml_field_plan") + assert m.name == "test" + assert m.count == 42 + + def test_enum_deserializer(self): + """Test that _xml_deser_enum_or_str works for enum fields.""" + import functools + from enum import Enum + from specialwords._utils.model_base import _xml_deser_enum_or_str + + class BlobType(str, Enum): + BLOCK_BLOB = "BlockBlob" + PAGE_BLOB = "PageBlob" + APPEND_BLOB = "AppendBlob" + + class BlobItem(Model): + _xml = {"name": "Blob"} + + name: str = rest_field( + name="Name", + xml={"name": "Name"}, + deserializer=_xml_deser_str, + ) + blob_type: str = rest_field( + name="BlobType", + xml={"name": "BlobType"}, + deserializer=functools.partial(_xml_deser_enum_or_str, BlobType), + ) + + xml = """ + + myblob + BlockBlob + """ + root = ET.fromstring(xml) + blob = BlobItem._deserialize(root, []) + assert blob.name == "myblob" + assert blob.blob_type == BlobType.BLOCK_BLOB + assert isinstance(blob.blob_type, BlobType) + + def test_enum_deserializer_unknown_value(self): + """Test that _xml_deser_enum_or_str falls back to str for unknown values.""" + import functools + from enum import Enum + from specialwords._utils.model_base import _xml_deser_enum_or_str + + class Status(str, Enum): + ACTIVE = "active" + INACTIVE = "inactive" + + class Item(Model): + _xml = {"name": "Item"} + + status: str = rest_field( + name="Status", + xml={"name": "Status"}, + deserializer=functools.partial(_xml_deser_enum_or_str, Status), + ) + + xml = """ + + unknown_value + """ + root = ET.fromstring(xml) + item = Item._deserialize(root, []) + assert item.status == "unknown_value" + assert isinstance(item.status, str) + assert not isinstance(item.status, Status) \ No newline at end of file From fcedf27216c621e8f61556ac32981178a634deec Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 15 May 2026 16:05:10 +0000 Subject: [PATCH 2/8] fix(changelog): add chronus entry for http-client-python xml perf changes Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/2f94ce6d-ca3d-4e72-9cb1-201a89e8d177 Co-authored-by: l0lawrence <100643745+l0lawrence@users.noreply.github.com> --- ...fix-http-client-python-xml-deserialization-2026-5-15.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/fix-http-client-python-xml-deserialization-2026-5-15.md diff --git a/.chronus/changes/fix-http-client-python-xml-deserialization-2026-5-15.md b/.chronus/changes/fix-http-client-python-xml-deserialization-2026-5-15.md new file mode 100644 index 00000000000..96873423aeb --- /dev/null +++ b/.chronus/changes/fix-http-client-python-xml-deserialization-2026-5-15.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" +--- + +Improve generated XML deserialization performance in Python by avoiding unnecessary parent traversal during field lookup. From 7405625f8f43ae26b19cd4b8746a248c53bf4e98 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 15 May 2026 17:08:40 +0000 Subject: [PATCH 3/8] fix: add deser and myblob to cspell word list Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/ce6fff89-aa07-4a38-881f-b8ac55b866c5 Co-authored-by: l0lawrence <100643745+l0lawrence@users.noreply.github.com> --- cspell.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cspell.yaml b/cspell.yaml index 31d3b03ac98..58ec6e0489f 100644 --- a/cspell.yaml +++ b/cspell.yaml @@ -62,6 +62,7 @@ words: - dedented - Dedupes - deps + - deser - destructures - devdiv - Diagnoser @@ -166,6 +167,7 @@ words: - multis - munge - muscidae + - myblob - mylib - myname - mypy From f223eaab1d66a5b474ebd44fb97a0971eb77f4a9 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Fri, 15 May 2026 14:36:48 -0700 Subject: [PATCH 4/8] fix(pylint): disable too-many-return-statements on _get_xml_deserializer_name Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../generator/pygen/codegen/serializers/model_serializer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py b/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py index a189a9a6301..341751ef640 100644 --- a/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py +++ b/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py @@ -26,7 +26,7 @@ from ..models.utils import NamespaceType -def _get_xml_deserializer_name(prop: Property) -> Optional[str]: +def _get_xml_deserializer_name(prop: Property) -> Optional[str]: # pylint: disable=too-many-return-statements """Return the _xml_deser_* function name for a scalar XML property, or None.""" prop_type = prop.type # Unwrap ConstantType to get the underlying value type From 47365da0a0476a2f77f83a773aaab0791707a39f Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Fri, 15 May 2026 15:17:30 -0700 Subject: [PATCH 5/8] fix(http-client-python): import xml enum classes at runtime in generated models The XML deserializer fast-path emits `functools.partial(_xml_deser_enum_or_str, EnumName)` in generated `_models.py`. That call is evaluated at class-body time, so `EnumName` must be in the module's runtime globals. Previously the enum import was routed to the `TYPE_CHECKING` block (see `EnumType.imports` for `NamespaceType.MODEL` + `called_by_property`), causing `NameError` at import time. Track each enum used as an XML deserializer and add a runtime `from ._enums import EnumName` import alongside the existing `_xml_deser_enum_or_str` import. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../codegen/serializers/model_serializer.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py b/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py index 341751ef640..78d7e169ae3 100644 --- a/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py +++ b/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py @@ -65,6 +65,14 @@ def _get_xml_deserializer_name(prop: Property) -> Optional[str]: # pylint: disa return None +def _get_xml_deserializer_enum_type(prop: Property) -> Optional[EnumType]: + """Return the EnumType for an XML enum property (unwrapping ConstantType), or None.""" + prop_type = prop.type + if isinstance(prop_type, ConstantType): + prop_type = prop_type.value_type + return prop_type if isinstance(prop_type, EnumType) else None + + def _documentation_string(prop: Property, description_keyword: str, docstring_type_keyword: str) -> list[str]: retval: list[str] = [] sphinx_prefix = f":{description_keyword} {prop.client_name}:" @@ -307,6 +315,7 @@ def imports(self) -> FileImport: ) # Collect XML deserializer functions needed by models in this file xml_deser_names: set[str] = set() + xml_deser_enums: dict[str, EnumType] = {} for model in self.models: if model.base == "json": continue @@ -330,6 +339,10 @@ def imports(self) -> FileImport: deser_name = _get_xml_deserializer_name(prop) if deser_name: xml_deser_names.add(deser_name) + if deser_name and deser_name.startswith("enum:"): + enum_type = _get_xml_deserializer_enum_type(prop) + if enum_type is not None: + xml_deser_enums[enum_type.name] = enum_type for parent in model.parents: if parent.client_namespace != model.client_namespace: file_import.add_submodule_import( @@ -362,6 +375,20 @@ def imports(self) -> FileImport: "_xml_deser_enum_or_str", ImportType.LOCAL, ) + # Ensure each referenced enum class is imported at runtime (not just + # under TYPE_CHECKING), so functools.partial can bind the class at + # class-body evaluation time. + for enum_name, enum_type in xml_deser_enums.items(): + file_import.add_submodule_import( + self.code_model.get_relative_import_path( + self.serialize_namespace, + enum_type.client_namespace, + module_name=self.code_model.enums_filename, + ), + enum_name, + ImportType.LOCAL, + TypingSection.REGULAR, + ) # if there is a property named `list` we have to make sure there's no conflict with the built-in `list` if self.code_model.has_property_named_list: file_import.define_mypy_type("List", "list") From 62c8929312218686ddf0a2839a98121b111477dd Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Fri, 15 May 2026 15:43:44 -0700 Subject: [PATCH 6/8] fix(http-client-python): correct xml enum runtime import path to sibling _enums Generated _models.py was emitting rom .._enums import Status because the relative path was computed against the enum's client_namespace (e.g. payload.xml) rather than the models subpackage where _enums.py actually resides (payload.xml.models). Use get_imported_namespace_for_model to target the sibling _enums module. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../generator/pygen/codegen/serializers/model_serializer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py b/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py index 78d7e169ae3..c7523c61feb 100644 --- a/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py +++ b/packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py @@ -382,7 +382,7 @@ def imports(self) -> FileImport: file_import.add_submodule_import( self.code_model.get_relative_import_path( self.serialize_namespace, - enum_type.client_namespace, + self.code_model.get_imported_namespace_for_model(enum_type.client_namespace), module_name=self.code_model.enums_filename, ), enum_name, From 1b5da57851654667962ab309c95e3c3bb8f3bd36 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 18 May 2026 15:56:01 +0000 Subject: [PATCH 7/8] fix(http-client-python): suppress generated pylint regressions in xml templates Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/7923e1b0-320c-4564-a96e-458926397afb Co-authored-by: l0lawrence <100643745+l0lawrence@users.noreply.github.com> --- .../generator/pygen/codegen/templates/model_base.py.jinja2 | 3 ++- .../generator/pygen/codegen/templates/serialization.py.jinja2 | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 index b5070e3c341..caae02af52c 100644 --- a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 +++ b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 @@ -2,7 +2,8 @@ {% if code_model.license_header %} {{ code_model.license_header }} {% endif %} -# pylint: disable=protected-access, broad-except +# pylint: disable=protected-access, broad-except, too-many-branches, too-many-statements, unused-variable +# pylint: disable=docstring-missing-param, docstring-missing-return, docstring-missing-rtype import copy import calendar diff --git a/packages/http-client-python/generator/pygen/codegen/templates/serialization.py.jinja2 b/packages/http-client-python/generator/pygen/codegen/templates/serialization.py.jinja2 index 986b872f07b..9d611b54b85 100644 --- a/packages/http-client-python/generator/pygen/codegen/templates/serialization.py.jinja2 +++ b/packages/http-client-python/generator/pygen/codegen/templates/serialization.py.jinja2 @@ -1401,7 +1401,7 @@ class Deserializer: # Otherwise, result are unexpected self.additional_properties_detection = True - def __call__(self, target_obj, response_data, content_type=None): + def __call__(self, target_obj, response_data, content_type=None): # pylint: disable=too-many-return-statements """Call the deserializer to process a REST response. :param str target_obj: Target data type to deserialize to. From e81f986a614ba8a3d4543b1353c54e9906f948fa Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 18 May 2026 15:59:38 +0000 Subject: [PATCH 8/8] fix(http-client-python): narrow pylint suppressions to xml helpers Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/7923e1b0-320c-4564-a96e-458926397afb Co-authored-by: l0lawrence <100643745+l0lawrence@users.noreply.github.com> --- .../pygen/codegen/templates/model_base.py.jinja2 | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 index caae02af52c..90af28ba13e 100644 --- a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 +++ b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 @@ -2,8 +2,7 @@ {% if code_model.license_header %} {{ code_model.license_header }} {% endif %} -# pylint: disable=protected-access, broad-except, too-many-branches, too-many-statements, unused-variable -# pylint: disable=docstring-missing-param, docstring-missing-return, docstring-missing-rtype +# pylint: disable=protected-access, broad-except import copy import calendar @@ -651,6 +650,7 @@ def _xml_deser_bool(value): return text.lower() == "true" +# pylint: disable=docstring-missing-param def _xml_deser_bytes(value): """Deserialize bytes from XML (base64).""" if isinstance(value, ET.Element): @@ -785,7 +785,9 @@ def _extract_xml_model_type(rf_type): return None -def _build_xml_field_plan(cls, attr_to_rest_field: dict) -> list: +def _build_xml_field_plan( # pylint: disable=docstring-missing-return, docstring-missing-rtype, unused-variable + cls, attr_to_rest_field: dict +) -> list: """Build a precomputed XML field plan for fast _init_from_xml iteration. Called once per model class in __new__. Returns a list of tuples: @@ -842,6 +844,7 @@ def _build_xml_field_plan(cls, attr_to_rest_field: dict) -> list: return plan +# pylint: enable=docstring-missing-param class Model(_MyMutableMapping): _is_model = True # label whether current class's _attr_to_rest_field has been calculated @@ -882,7 +885,9 @@ class Model(_MyMutableMapping): dict_to_pass[rf._rest_name] = _create_value(rf, rf._default) super().__init__(dict_to_pass) - def _init_from_xml(self, element: ET.Element) -> dict[str, typing.Any]: + def _init_from_xml( # pylint: disable=too-many-branches, too-many-statements + self, element: ET.Element + ) -> dict[str, typing.Any]: """Deserialize an XML element into a dict mapping rest field names to values. :param ET.Element element: The XML element to deserialize from.