Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading