Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 22, 2025

Fix list type annotations when model property is named "list"

This PR fixes a syntax error that occurs when a model property is named "list", preventing TypeError: '_RestField' object is not subscriptable errors in generated Python code.

Changes

  • Add has_property_named_list property to CodeModel class (similar to has_operation_named_list)
  • Update ListType.type_annotation() to use "List" instead of "list" when property named "list" exists in models
  • Add type alias definition in DpgModelSerializer.imports() (List = list) when needed
  • Address code review feedback (improve test structure, fix spacing)
  • Pass security checks (0 alerts)
  • Add changelog entry

Summary

When a model has a property named "list", the generated code now uses a type alias List = list and references it as List[...] instead of list[...] to avoid naming conflicts. This follows the same pattern as the existing fix for operations named "list".

Files changed:

  • code_model.py: Added has_property_named_list property
  • list_type.py: Updated type annotation logic to handle property name conflicts
  • model_serializer.py: Added type alias definition when needed
  • .chronus/changes/copilot-fix-list-property-name-2025-12-22-05-33-41.md: Added changelog entry

The fix ensures backward compatibility and only applies the workaround when necessary.

Original prompt

This section details on the original issue you should resolve

<issue_title>List type annotations leads to syntax error</issue_title>
<issue_description>I used the latest Python TypeSpec emitter on this tsplocation:

directory: specification/purview/data-plane/datamap
commit: 0c932f4a1e126784161397209484678f3adda2f6
repo: Azure/azure-rest-api-specs
additionalDirectories: 

I get annotation using list, so the code is not valid:

azure/purview/datamap/__init__.py:15: in <module>
    from ._client import DataMapClient  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
azure/purview/datamap/_client.py:19: in <module>
    from .operations import (
azure/purview/datamap/operations/__init__.py:15: in <module>
    from ._operations import EntityOperations  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
azure/purview/datamap/operations/_operations.py:30: in <module>
    from .. import models as _models
azure/purview/datamap/models/__init__.py:16: in <module>
    from ._models import (  # type: ignore
azure/purview/datamap/models/_models.py:496: in <module>
    class AtlasClassifications(_Model):
azure/purview/datamap/models/_models.py:514: in AtlasClassifications
    list: Optional[list[Any]] = rest_field(visibility=["read", "create", "update", "delete", "query"])
                   ^^^^^^^^^
E   TypeError: '_RestField' object is not subscriptable

Either we import "from future import annotation", or we use List. I think I prefer to import future annotations, since it's more oriented to the future.

I'm using Python 3.12.3, so totally recent runtime.</issue_description>

<agent_instructions>We ever fixed a similar bug that an operation is named "list"

# if there is a function named `list` we have to make sure there's no conflict with the built-in `list`
list_type = "List" if self.code_model.has_operation_named_list and kwargs.get("is_operation_file") else "list"
return f"{list_type}[{self.element_type.type_annotation(**kwargs)}]"
. And this issue is about model property so we could add similar logic to fix it.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@msyyc We ever fixed a similar bug that an operation is named "list" https://github.com/microsoft/typespec/blob/dd8b60156738aeb9ce4c854805b7a15afdd4108f/packages/http-client-python/generator/pygen/codegen/models/list_type.py#L44-L46. And this issue is about model property so we could add similar logic to fix it.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits December 22, 2025 05:14
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix list type annotations to resolve syntax error Fix list type annotations when model property is named "list" Dec 22, 2025
Copilot AI requested a review from msyyc December 22, 2025 05:24
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copilot AI requested a review from msyyc December 22, 2025 05:35
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copilot AI requested a review from msyyc December 22, 2025 06:25
@msyyc msyyc marked this pull request as ready for review December 22, 2025 07:27
@msyyc msyyc enabled auto-merge December 22, 2025 08:08
@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 22, 2025

Open in StackBlitz

npm i https://pkg.pr.new/microsoft/typespec/@typespec/http@9256
npm i https://pkg.pr.new/microsoft/typespec/@typespec/http-client@9256

commit: 52305cf

@github-actions
Copy link
Contributor

All changed packages have been documented.

  • @typespec/http-client-python
Show changes

@typespec/http-client-python - fix ✏️

Fix syntax error when model property is named "list" by using type alias to avoid naming conflicts

@msyyc msyyc added this pull request to the merge queue Dec 23, 2025
Merged via the queue into main with commit 9805016 Dec 23, 2025
34 checks passed
@msyyc msyyc deleted the copilot/fix-list-type-annotations-error branch December 23, 2025 03:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

List type annotations leads to syntax error

3 participants