diff --git a/.chronus/changes/copilot-fix-list-property-name-2025-12-22-05-33-41.md b/.chronus/changes/copilot-fix-list-property-name-2025-12-22-05-33-41.md new file mode 100644 index 00000000000..1de960b2340 --- /dev/null +++ b/.chronus/changes/copilot-fix-list-property-name-2025-12-22-05-33-41.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" +--- + +Fix syntax error when model property is named "list" by using type alias to avoid naming conflicts diff --git a/packages/http-client-python/generator/pygen/codegen/models/code_model.py b/packages/http-client-python/generator/pygen/codegen/models/code_model.py index beba1909cb3..81f5f20bf8b 100644 --- a/packages/http-client-python/generator/pygen/codegen/models/code_model.py +++ b/packages/http-client-python/generator/pygen/codegen/models/code_model.py @@ -491,6 +491,10 @@ def _get_relative_generation_dir(self, root_dir: Path, namespace: str) -> Path: def has_operation_named_list(self) -> bool: return any(o.name.lower() == "list" for c in self.clients for og in c.operation_groups for o in og.operations) + @property + def has_property_named_list(self) -> bool: + return any(p.client_name.lower() == "list" for m in self.model_types for p in m.properties) + @property def has_padded_model_property(self) -> bool: for model_type in self.model_types: diff --git a/packages/http-client-python/generator/pygen/codegen/models/list_type.py b/packages/http-client-python/generator/pygen/codegen/models/list_type.py index 39db5a91534..91fa8b20da9 100644 --- a/packages/http-client-python/generator/pygen/codegen/models/list_type.py +++ b/packages/http-client-python/generator/pygen/codegen/models/list_type.py @@ -41,8 +41,13 @@ def type_annotation(self, **kwargs: Any) -> str: # this means we're version tolerant XML, we just return the XML element return self.element_type.type_annotation(**kwargs) - # 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" + # if there is a function/property named `list` we have to make sure there's no conflict with the built-in `list` + is_operation_file = kwargs.get("is_operation_file", False) + use_list_import = ( + (self.code_model.has_operation_named_list and is_operation_file) or + (self.code_model.has_property_named_list and not is_operation_file) + ) + list_type = "List" if use_list_import else "list" return f"{list_type}[{self.element_type.type_annotation(**kwargs)}]" def description(self, *, is_operation_file: bool) -> str: 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 cf17f26e1fd..d428a113e5e 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 @@ -285,6 +285,9 @@ 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) + # 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") return file_import def declare_model(self, model: ModelType) -> str: