diff --git a/.chronus/changes/python-fix-paging-list-typing-2026-4-28-5-36-18.md b/.chronus/changes/python-fix-paging-list-typing-2026-4-28-5-36-18.md new file mode 100644 index 00000000000..839d6545130 --- /dev/null +++ b/.chronus/changes/python-fix-paging-list-typing-2026-4-28-5-36-18.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" +--- + +Fix typing in generated paging operations when an operation is named `list` and the page item is a collection type. The return type annotation now correctly uses the `List` alias (e.g. `AsyncItemPaged[List[str]]`) instead of the built-in `list` (which would shadow the operation name) to stay consistent with other annotations in the same file. diff --git a/packages/http-client-python/generator/pygen/codegen/models/response.py b/packages/http-client-python/generator/pygen/codegen/models/response.py index d93d46bd897..d37986146fd 100644 --- a/packages/http-client-python/generator/pygen/codegen/models/response.py +++ b/packages/http-client-python/generator/pygen/codegen/models/response.py @@ -181,7 +181,21 @@ def get_pager(self, async_mode: bool) -> str: def type_annotation(self, **kwargs: Any) -> str: iterable = "AsyncItemPaged" if kwargs["async_mode"] else "ItemPaged" - return f"{iterable}[{self.item_type.type_annotation(**kwargs)}]" + return f"{iterable}[{self._item_type_annotation(**kwargs)}]" + + def _item_type_annotation(self, **kwargs: Any) -> str: + # When the page item is a ListType, render the outer `List`/`list` + # wrapper here using the operation-file alias decision so a list page + # item rendered inside an operation file named `list` uses the `List` + # alias (avoiding the built-in `list` shadowed by `List = list`). + # Recurse into the element type without is_operation_file so nested + # generated model types keep their forward-reference quoting + # (e.g. ItemPaged[List["_models.Product"]]). + if isinstance(self.item_type, ListType): + use_list_import = self.code_model.has_operation_named_list + list_type = "List" if use_list_import else "list" + return f"{list_type}[{self.item_type.element_type.type_annotation(**kwargs)}]" + return self.item_type.type_annotation(**kwargs) def docstring_text(self, **kwargs: Any) -> str: base_description = "An iterator like instance of "