Skip to content

Commit 4ced32a

Browse files
committed
fix: sort remaining lazy imports in model template
1 parent 410784b commit 4ced32a

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

openapi_python_client/templates/model.py.jinja

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ return field_dict
144144
{% endmacro %}
145145

146146
def to_dict(self) -> dict[str, Any]:
147-
{% for lazy_import in model.lazy_imports %}
147+
{% for lazy_import in model.lazy_imports | sort %}
148148
{{ lazy_import }}
149149
{% endfor %}
150150
{{ _to_dict() | indent(8) }}
151151

152152
{% if model.is_multipart_body %}
153153
def to_multipart(self) -> types.RequestFiles:
154-
{% for lazy_import in model.lazy_imports %}
154+
{% for lazy_import in model.lazy_imports | sort %}
155155
{{ lazy_import }}
156156
{% endfor %}
157157
files: types.RequestFiles = []
@@ -204,7 +204,7 @@ return field_dict
204204
{% import "property_templates/" + model.additional_properties.template as prop_template %}
205205

206206
{% if model.additional_properties.lazy_imports %}
207-
{% for lazy_import in model.additional_properties.lazy_imports %}
207+
{% for lazy_import in model.additional_properties.lazy_imports | sort %}
208208
{{ lazy_import }}
209209
{% endfor %}
210210
{% endif %}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from __future__ import annotations
2+
3+
from types import SimpleNamespace
4+
5+
6+
def _section(content: str, start: str, end: str | None = None) -> str:
7+
section = content.split(start, 1)[1]
8+
if end is not None:
9+
section = section.split(end, 1)[0]
10+
return section
11+
12+
13+
def test_model_template_renders_lazy_imports_in_stable_order(env) -> None:
14+
template = env.get_template("model.py.jinja")
15+
16+
model = SimpleNamespace(
17+
is_multipart_body=True,
18+
relative_imports=set(),
19+
lazy_imports={"from ..models.z import Z", "from ..models.a import A"},
20+
additional_properties=False,
21+
class_info=SimpleNamespace(name="MyModel", module_name="my_model"),
22+
title="",
23+
description="",
24+
example="",
25+
required_properties=[],
26+
optional_properties=[],
27+
)
28+
config = SimpleNamespace(docstrings_on_attributes=False)
29+
30+
content = template.render(model=model, config=config)
31+
32+
sections = [
33+
_section(content, "if TYPE_CHECKING:", "T = TypeVar"),
34+
_section(content, "def to_dict(self)", "def to_multipart(self)"),
35+
_section(content, "def to_multipart(self)", "@classmethod"),
36+
_section(content, "def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:", "return my_model"),
37+
]
38+
for section in sections:
39+
assert section.index("from ..models.a import A") < section.index("from ..models.z import Z")

0 commit comments

Comments
 (0)