Repository navigation
[http-client-python] Keep containers from dict access attached after property access - #12644
Minh Triet Nguyen (binnguyenx) wants to merge 3 commits into
Conversation
…property access Reading a list/dict/set field via property after getting it via dict syntax replaced the container in _data, so mutations through the earlier reference were silently dropped from as_dict() and serialization. When deserialization leaves every element untouched (compared by identity), keep the original container instead of swapping in a copy. Fixes microsoft#12360 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
🟡 Changes recommended
The set identity check makes property access quadratic, causing a large-set slowdown that should be avoided by checking against a set of original element IDs.
1 open issue
What changed in this PR
Fixes #12360 in the Python client emitter’s generated model runtime by preserving container references obtained through dictionary access when later property access leaves elements unchanged.
Changes:
- Adds identity-based checks to retain unchanged list, dict, and set containers.
- Adds regression tests for mixed access patterns and mutation serialization.
- Records the fix in a package changelog entry.
| File | Description |
|---|---|
packages/http-client-python/tests/unit/test_model_base_serialization.py |
Adds container-reference regression tests and a property-first mutation guard. |
packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 |
Preserves existing containers when deserialized elements retain their identities. |
.chronus/changes/python-model-base-container-detach-2026-10-23-18-0-0.md |
Documents the Python model mutation fix. |
Review effort: Balanced
|
Yuchao Yan (@msyyc) could you please comment |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
commit: |
|
All changed packages have been documented.
Show changes
|
Python emitter diffBaseline Diff summary: 213 file(s), +5751 / -0 Rendered diff: inline on the run summary, or the emitter-diff-html artifact. Informational check (eng/emitter-diff); does not block the PR. |

Fixes #12360.
Problem
In the generated model runtime (
_utils/model_base.py),_RestField.__get__deserializes list, dict and set fields and stores the result back intoobj._data. For primitive containers likelist[str]ordict[str, str], the deserializer returns a new container with the same elements. So a container a caller already got through dict access is detached from the model, and later mutations through it are silently dropped:Only the dict access, then property access order was affected. The other access orders already kept mutations.
Fix
In
__get__, if every element of the deserialized container is the same object as in the stored container, the stored container is kept instead of the copy. The new_has_same_elementshelper compares elements by identity, not with==. Using==would callModel.__eq__when a list of models is compared against its raw list of dicts, and that raises.Containers whose elements do get converted (models, datetimes, bytes, etc.) behave exactly as before. In those cases the dict view and the property view are different objects by design. The caller's original container is never mutated.
Tests
test_list_getitem_reference_survives_attribute_accessandtest_dict_getitem_reference_survives_attribute_access(from the repro in [python] Repro: property access detaches container returned by __getitem__ #12359), plustest_set_getitem_reference_survives_attribute_access. They checkas_dict()andSdkJSONEncoderoutput where relevant.test_list_attribute_reference_mutation_serializesas a guard for the order that already worked.getitemtests fail onmainand pass with the fix. All 127 tests intests/unit/test_model_base_serialization.pypass.eng/scripts/ci/config/pyproject.toml) is clean on the test file. pylint (witheng/scripts/ci/config/pylintrc) reports the same findings on the renderedmodel_base.pybefore and after the change.I ran these against
model_base.pyrendered straight from the template, because the generated test packages aren't present locally. I couldn't run the fullnpm run regenerate/npm run ci, so CI will be the first full regeneration with this change.🤖 Generated with Claude Code