Skip to content

Commit d76bae8

Browse files
authored
refactor: Renamed all templates to end in .jinja to resolve issues with latest mypy. (#320)
1 parent cf2b002 commit d76bae8

34 files changed

+59
-81
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222

2323
- Lowered the minimum version of `python-dateutil` to 2.8.0 for improved compatibility (#298 & #299). Thanks @bowenwr!
2424
- The `from_dict` method on generated models is now a `@classmethod` instead of `@staticmethod` (#215 & #292). Thanks @forest-benchling!
25+
- Renamed all templates to end in `.jinja`, and all python-templates to end in `.py.jinja` to fix confusion with the latest version of mypy. Note **this will break existing custom templates until you update your template file names**.
2526

2627
### Fixes
2728

end_to_end_tests/test_custom_templates/endpoint_module.pyi renamed to end_to_end_tests/test_custom_templates/endpoint_module.py.jinja

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Client = httpx.Client
1010
{{ relative }}
1111
{% endfor %}
1212

13-
{% from "endpoint_macros.pyi" import header_params, query_params, json_body, return_type, arguments, client, kwargs, parse_response %}
13+
{% from "endpoint_macros.py.jinja" import header_params, query_params, json_body, return_type, arguments, client, kwargs, parse_response %}
1414

1515
{% set return_string = return_type(endpoint) %}
1616
{% set parsed_responses = (endpoint.responses | length > 0) and return_string != "None" %}

mypy.ini

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ ignore_missing_imports = True
1212

1313
[mypy-typer]
1414
ignore_missing_imports = True
15+

openapi_python_client/__init__.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ def _create_package(self) -> None:
136136
# Package __init__.py
137137
package_init = self.package_dir / "__init__.py"
138138

139-
package_init_template = self.env.get_template("package_init.pyi")
139+
package_init_template = self.env.get_template("package_init.py.jinja")
140140
package_init.write_text(package_init_template.render(description=self.package_description))
141141

142142
if self.meta != MetaType.NONE:
143143
pytyped = self.package_dir / "py.typed"
144144
pytyped.write_text("# Marker file for PEP 561")
145145

146-
types_template = self.env.get_template("types.py")
146+
types_template = self.env.get_template("types.py.jinja")
147147
types_path = self.package_dir / "types.py"
148148
types_path.write_text(types_template.render())
149149

@@ -157,7 +157,7 @@ def _build_metadata(self) -> None:
157157

158158
# README.md
159159
readme = self.project_dir / "README.md"
160-
readme_template = self.env.get_template("README.md")
160+
readme_template = self.env.get_template("README.md.jinja")
161161
readme.write_text(
162162
readme_template.render(
163163
project_name=self.project_name, description=self.package_description, package_name=self.package_name
@@ -166,11 +166,11 @@ def _build_metadata(self) -> None:
166166

167167
# .gitignore
168168
git_ignore_path = self.project_dir / ".gitignore"
169-
git_ignore_template = self.env.get_template(".gitignore")
169+
git_ignore_template = self.env.get_template(".gitignore.jinja")
170170
git_ignore_path.write_text(git_ignore_template.render())
171171

172172
def _build_pyproject_toml(self, *, use_poetry: bool) -> None:
173-
template = "pyproject.toml" if use_poetry else "pyproject_no_poetry.toml"
173+
template = "pyproject.toml.jinja" if use_poetry else "pyproject_no_poetry.toml.jinja"
174174
pyproject_template = self.env.get_template(template)
175175
pyproject_path = self.project_dir / "pyproject.toml"
176176
pyproject_path.write_text(
@@ -183,7 +183,7 @@ def _build_pyproject_toml(self, *, use_poetry: bool) -> None:
183183
)
184184

185185
def _build_setup_py(self) -> None:
186-
template = self.env.get_template("setup.py")
186+
template = self.env.get_template("setup.py.jinja")
187187
path = self.project_dir / "setup.py"
188188
path.write_text(
189189
template.render(
@@ -201,15 +201,15 @@ def _build_models(self) -> None:
201201
models_init = models_dir / "__init__.py"
202202
imports = []
203203

204-
model_template = self.env.get_template("model.pyi")
204+
model_template = self.env.get_template("model.py.jinja")
205205
for model in self.openapi.models.values():
206206
module_path = models_dir / f"{model.reference.module_name}.py"
207207
module_path.write_text(model_template.render(model=model))
208208
imports.append(import_string_from_reference(model.reference))
209209

210210
# Generate enums
211-
str_enum_template = self.env.get_template("str_enum.pyi")
212-
int_enum_template = self.env.get_template("int_enum.pyi")
211+
str_enum_template = self.env.get_template("str_enum.py.jinja")
212+
int_enum_template = self.env.get_template("int_enum.py.jinja")
213213
for enum in self.openapi.enums.values():
214214
module_path = models_dir / f"{enum.reference.module_name}.py"
215215
if enum.value_type is int:
@@ -218,13 +218,13 @@ def _build_models(self) -> None:
218218
module_path.write_text(str_enum_template.render(enum=enum))
219219
imports.append(import_string_from_reference(enum.reference))
220220

221-
models_init_template = self.env.get_template("models_init.pyi")
221+
models_init_template = self.env.get_template("models_init.py.jinja")
222222
models_init.write_text(models_init_template.render(imports=imports))
223223

224224
def _build_api(self) -> None:
225225
# Generate Client
226226
client_path = self.package_dir / "client.py"
227-
client_template = self.env.get_template("client.pyi")
227+
client_template = self.env.get_template("client.py.jinja")
228228
client_path.write_text(client_template.render())
229229

230230
# Generate endpoints
@@ -233,7 +233,7 @@ def _build_api(self) -> None:
233233
api_init = api_dir / "__init__.py"
234234
api_init.write_text('""" Contains methods for accessing the API """')
235235

236-
endpoint_template = self.env.get_template("endpoint_module.pyi")
236+
endpoint_template = self.env.get_template("endpoint_module.py.jinja")
237237
for tag, collection in self.openapi.endpoint_collections_by_tag.items():
238238
tag = utils.snake_case(tag)
239239
tag_dir = api_dir / tag

openapi_python_client/parser/properties/__init__.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class NoneProperty(Property):
1919
""" A property that is always None (used for empty schemas) """
2020

2121
_type_string: ClassVar[str] = "None"
22-
template: ClassVar[Optional[str]] = "none_property.pyi"
22+
template: ClassVar[Optional[str]] = "none_property.py.jinja"
2323

2424

2525
@attr.s(auto_attribs=True, frozen=True)
@@ -38,7 +38,7 @@ class DateTimeProperty(Property):
3838
"""
3939

4040
_type_string: ClassVar[str] = "datetime.datetime"
41-
template: ClassVar[str] = "datetime_property.pyi"
41+
template: ClassVar[str] = "datetime_property.py.jinja"
4242

4343
def get_imports(self, *, prefix: str) -> Set[str]:
4444
"""
@@ -58,7 +58,7 @@ class DateProperty(Property):
5858
""" A property of type datetime.date """
5959

6060
_type_string: ClassVar[str] = "datetime.date"
61-
template: ClassVar[str] = "date_property.pyi"
61+
template: ClassVar[str] = "date_property.py.jinja"
6262

6363
def get_imports(self, *, prefix: str) -> Set[str]:
6464
"""
@@ -78,7 +78,7 @@ class FileProperty(Property):
7878
""" A property used for uploading files """
7979

8080
_type_string: ClassVar[str] = "File"
81-
template: ClassVar[str] = "file_property.pyi"
81+
template: ClassVar[str] = "file_property.py.jinja"
8282

8383
def get_imports(self, *, prefix: str) -> Set[str]:
8484
"""
@@ -122,7 +122,7 @@ class ListProperty(Property, Generic[InnerProp]):
122122
""" A property representing a list (array) of other properties """
123123

124124
inner_property: InnerProp
125-
template: ClassVar[str] = "list_property.pyi"
125+
template: ClassVar[str] = "list_property.py.jinja"
126126

127127
def get_type_string(self, no_optional: bool = False) -> str:
128128
""" Get a string representation of type that should be used when declaring this property """
@@ -158,7 +158,7 @@ class UnionProperty(Property):
158158
""" A property representing a Union (anyOf) of other properties """
159159

160160
inner_properties: List[Property]
161-
template: ClassVar[str] = "union_property.pyi"
161+
template: ClassVar[str] = "union_property.py.jinja"
162162
has_properties_without_templates: bool = attr.ib(init=False)
163163

164164
def __attrs_post_init__(self) -> None:

openapi_python_client/parser/properties/enum_property.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class EnumProperty(Property):
2020
value_type: Type[ValueType]
2121
default: Optional[Any] = attr.ib()
2222

23-
template: ClassVar[str] = "enum_property.pyi"
23+
template: ClassVar[str] = "enum_property.py.jinja"
2424

2525
def get_type_string(self, no_optional: bool = False) -> str:
2626
""" Get a string representation of type that should be used when declaring this property """

openapi_python_client/parser/properties/model_property.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ModelProperty(Property):
1818
relative_imports: Set[str]
1919
additional_properties: Union[bool, Property]
2020

21-
template: ClassVar[str] = "model_property.pyi"
21+
template: ClassVar[str] = "model_property.py.jinja"
2222

2323
def get_type_string(self, no_optional: bool = False) -> str:
2424
""" Get a string representation of type that should be used when declaring this property """

openapi_python_client/templates/endpoint_module.pyi renamed to openapi_python_client/templates/endpoint_module.py.jinja

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ from ...types import Response
1010
{{ relative }}
1111
{% endfor %}
1212

13-
{% from "endpoint_macros.pyi" import header_params, query_params, json_body, return_type, arguments, client, kwargs, parse_response %}
13+
{% from "endpoint_macros.py.jinja" import header_params, query_params, json_body, return_type, arguments, client, kwargs, parse_response %}
1414

1515
{% set return_string = return_type(endpoint) %}
1616
{% set parsed_responses = (endpoint.responses | length > 0) and return_string != "None" %}

openapi_python_client/templates/property_templates/dict_property.pyi

-18
This file was deleted.

0 commit comments

Comments
 (0)