Skip to content

Commit 14b7d6c

Browse files
iscai-msftCopilot
andcommitted
fix: resolve CI failures in type checking, lint, and mock API tests
- Add 'List' import to ListType.imports() when property/operation named 'list' exists (fixes NameError in special-words types.py) - Use has_non_json_models() for has_models in ModelInitSerializer to avoid generating 'from . import _models' when _models.py doesn't exist - Remove 'from . import types' from __init__.py — each TypedDict type's imports() already handles adding the types import where needed (operations) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1ff2c23 commit 14b7d6c

5 files changed

Lines changed: 10 additions & 17 deletions

File tree

packages/http-client-python/generator/pygen/codegen/models/list_type.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# --------------------------------------------------------------------------
66
from typing import Any, Optional, Union, TYPE_CHECKING
77
from .base import BaseType
8-
from .imports import FileImport
8+
from .imports import FileImport, ImportType
99

1010
if TYPE_CHECKING:
1111
from .code_model import CodeModel
@@ -136,6 +136,12 @@ def from_yaml(cls, yaml_data: dict[str, Any], code_model: "CodeModel") -> "ListT
136136
def imports(self, **kwargs: Any) -> FileImport:
137137
file_import = FileImport(self.code_model)
138138
file_import.merge(self.element_type.imports(**kwargs))
139+
is_operation_file = kwargs.get("is_operation_file", False)
140+
use_list_import = (self.code_model.has_operation_named_list and is_operation_file) or (
141+
self.code_model.has_property_named_list and not is_operation_file
142+
)
143+
if use_list_import:
144+
file_import.add_submodule_import("typing", "List", ImportType.STDLIB)
139145
return file_import
140146

141147
@property

packages/http-client-python/generator/pygen/codegen/serializers/__init__.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,8 @@ def serialize(self) -> None:
182182
)
183183
elif client_namespace_type.clients:
184184
# add clients folder if there are clients in this namespace
185-
# compute has_types early so __init__.py can include `from . import types`
186-
has_types_models = any(
187-
m.is_used_in_operations_via_types for m in client_namespace_type.models if m.base != "json"
188-
)
189-
has_types_enums = any(e.is_typeddict_mode for e in client_namespace_type.enums)
190-
has_types = has_types_models or has_types_enums
191185
self._serialize_client_and_config_files(
192-
client_namespace, client_namespace_type.clients, env, has_types=has_types
186+
client_namespace, client_namespace_type.clients, env
193187
)
194188
# When generation-subdir is configured, generated code goes into a subdirectory
195189
# (e.g., _generated/). We also need an __init__.py in the parent namespace dir
@@ -471,7 +465,6 @@ def _serialize_client_and_config_files(
471465
namespace: str,
472466
clients: list[Client],
473467
env: Environment,
474-
has_types: bool = False,
475468
) -> None:
476469
generation_path = self.code_model.get_generation_dir(namespace)
477470
for async_mode, async_path in self.serialize_loop:
@@ -483,8 +476,6 @@ def _serialize_client_and_config_files(
483476
generation_path / Path(f"{async_path}__init__.py"),
484477
general_serializer.serialize_init_file(
485478
[c for c in clients if c.has_operations],
486-
# types.py is only generated at the sync (non-aio) level
487-
has_types=has_types and not async_mode,
488479
),
489480
)
490481

packages/http-client-python/generator/pygen/codegen/serializers/general_serializer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,13 @@ def serialize_pkgutil_init_file(self) -> str:
198198
template = self.env.get_template("pkgutil_init.py.jinja2")
199199
return template.render()
200200

201-
def serialize_init_file(self, clients: list[Client], has_types: bool = False) -> str:
201+
def serialize_init_file(self, clients: list[Client]) -> str:
202202
template = self.env.get_template("init.py.jinja2")
203203
return template.render(
204204
code_model=self.code_model,
205205
clients=clients,
206206
async_mode=self.async_mode,
207207
serialize_namespace=self.serialize_namespace,
208-
has_types=has_types,
209208
)
210209

211210
def serialize_service_client_file(self, clients: list[Client]) -> str:

packages/http-client-python/generator/pygen/codegen/serializers/model_init_serializer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def serialize(self) -> str:
3232
", ".join(model_enum_name_intersection)
3333
)
3434
)
35-
has_models = self.models
35+
has_models = self.code_model.has_non_json_models(self.models)
3636
has_enums = self.enums
3737
template = self.env.get_template("model_init.py.jinja2")
3838
return template.render(

packages/http-client-python/generator/pygen/codegen/templates/init.py.jinja2

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
from .{{ client.filename }} import {{ client.name }} # type: ignore
1010
{% endfor %}
1111
{% endif %}
12-
{% if has_types %}
13-
from . import types # type: ignore
14-
{% endif %}
1512
{% if not async_mode and code_model.options.get("package-version") %}
1613
from {{ code_model.get_relative_import_path(serialize_namespace, module_name="_version") }} import VERSION
1714

0 commit comments

Comments
 (0)