Skip to content

[Bug]: [python] Reading a model property detaches the container previously returned by __getitem__, dropping mutations #12360

Open

Description

Describe the bug

In the Python emitter's generated model runtime (_utils/model_base.py, from packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2), _RestField.__get__ deserializes list, dict and set values. It then stores the new container back into obj._data[rest_name] and sets _deserialized_<rest_name>.

Any container a caller got earlier through mapping access (obj["rest_name"]) is now detached from the model, so later mutations through that reference are silently dropped from as_dict() and request serialization. No error is raised.

The problem is limited to mixing dict access and property access on the same field. Mutating through a property reference (model.field.append(...)) works correctly; the property caching fixed that, since older generated runtimes lost those mutations. The remaining gap is the dict-then-property order:

Access order Mutation kept?
dict → property → mutate dict ref ❌ lost
property → dict → mutate property ref ✅
property → mutate ✅
dict → mutate ✅
property → property → mutate first ref ✅

Lists of models aren't affected: their deserializer returns the original list. Lists and dicts of primitives (for example list[str], dict[str, str]) are affected.

Expected: mapping access and property access share one backing container, so a mutation through either reference serializes.

Reproduction

Failing unit tests: #12359 (adds to packages/http-client-python/tests/unit/test_model_base_serialization.py). 2 new tests fail, the guard and existing tests pass:

FAIL test_list_getitem_reference_survives_attribute_access: assert ['.txt'] == ['.txt', '.csv']
FAIL test_dict_getitem_reference_survives_attribute_access: assert {'a': '1'} == {'a': '1', 'b': '2'}

Equivalent with a minimal hand-written model:

from specialwords._utils.model_base import Model, rest_field   # any generated package's model_base

class M(Model):
    tags: list[str] = rest_field()

m = M(tags=["a"])
ref = m["tags"]
_ = m.tags
ref.append("b")
assert m.as_dict()["tags"] == ["a", "b"]   # fails: ['a']

Checklist

Activity

  1. github-actions commented on Oct 22, 2026

    @github-actions
    Contributor

    🎯 Issue Triage
    The generated Python model runtime can replace a list or dictionary during property deserialization, detaching a container reference previously returned by mapping access and silently omitting later mutations from serialization.

    Category: Bug
    Area: emitter:client:python

    ⚠️ No TypeSpec playground reproduction is applicable. The issue provides a minimal Python runtime reproduction and focused failing tests in #12359.

    🔍 Analysis

    The affected component is packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2, specifically _RestField.__get__. The likely cause is that deserialization of primitive lists, dictionaries, and sets creates and caches a new container in obj._data, while an earlier obj[rest_name] reference still points to the original container. Lists of models are reportedly unaffected because their deserializer preserves the original list identity.

    Focused searches found no existing issue describing the same access-order-dependent container detachment.

    📝 Suggested next steps

    Generated by Agentic Triage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingemitter:client:pythonIssue for the Python client emitter: @typespec/http-client-python

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions