Skip to content

Commit

Permalink
refactor: Renamed all templates to end in .jinja to resolve issues wi…
Browse files Browse the repository at this point in the history
…th latest mypy. (#320)
  • Loading branch information
dbanty committed Feb 1, 2021
1 parent cf2b002 commit d76bae8
Show file tree
Hide file tree
Showing 34 changed files with 59 additions and 81 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Lowered the minimum version of `python-dateutil` to 2.8.0 for improved compatibility (#298 & #299). Thanks @bowenwr!
- The `from_dict` method on generated models is now a `@classmethod` instead of `@staticmethod` (#215 & #292). Thanks @forest-benchling!
- 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**.

### Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Client = httpx.Client
{{ relative }}
{% endfor %}

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

{% set return_string = return_type(endpoint) %}
{% set parsed_responses = (endpoint.responses | length > 0) and return_string != "None" %}
Expand Down
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ ignore_missing_imports = True

[mypy-typer]
ignore_missing_imports = True

24 changes: 12 additions & 12 deletions openapi_python_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ def _create_package(self) -> None:
# Package __init__.py
package_init = self.package_dir / "__init__.py"

package_init_template = self.env.get_template("package_init.pyi")
package_init_template = self.env.get_template("package_init.py.jinja")
package_init.write_text(package_init_template.render(description=self.package_description))

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

types_template = self.env.get_template("types.py")
types_template = self.env.get_template("types.py.jinja")
types_path = self.package_dir / "types.py"
types_path.write_text(types_template.render())

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

# README.md
readme = self.project_dir / "README.md"
readme_template = self.env.get_template("README.md")
readme_template = self.env.get_template("README.md.jinja")
readme.write_text(
readme_template.render(
project_name=self.project_name, description=self.package_description, package_name=self.package_name
Expand All @@ -166,11 +166,11 @@ def _build_metadata(self) -> None:

# .gitignore
git_ignore_path = self.project_dir / ".gitignore"
git_ignore_template = self.env.get_template(".gitignore")
git_ignore_template = self.env.get_template(".gitignore.jinja")
git_ignore_path.write_text(git_ignore_template.render())

def _build_pyproject_toml(self, *, use_poetry: bool) -> None:
template = "pyproject.toml" if use_poetry else "pyproject_no_poetry.toml"
template = "pyproject.toml.jinja" if use_poetry else "pyproject_no_poetry.toml.jinja"
pyproject_template = self.env.get_template(template)
pyproject_path = self.project_dir / "pyproject.toml"
pyproject_path.write_text(
Expand All @@ -183,7 +183,7 @@ def _build_pyproject_toml(self, *, use_poetry: bool) -> None:
)

def _build_setup_py(self) -> None:
template = self.env.get_template("setup.py")
template = self.env.get_template("setup.py.jinja")
path = self.project_dir / "setup.py"
path.write_text(
template.render(
Expand All @@ -201,15 +201,15 @@ def _build_models(self) -> None:
models_init = models_dir / "__init__.py"
imports = []

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

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

models_init_template = self.env.get_template("models_init.pyi")
models_init_template = self.env.get_template("models_init.py.jinja")
models_init.write_text(models_init_template.render(imports=imports))

def _build_api(self) -> None:
# Generate Client
client_path = self.package_dir / "client.py"
client_template = self.env.get_template("client.pyi")
client_template = self.env.get_template("client.py.jinja")
client_path.write_text(client_template.render())

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

endpoint_template = self.env.get_template("endpoint_module.pyi")
endpoint_template = self.env.get_template("endpoint_module.py.jinja")
for tag, collection in self.openapi.endpoint_collections_by_tag.items():
tag = utils.snake_case(tag)
tag_dir = api_dir / tag
Expand Down
12 changes: 6 additions & 6 deletions openapi_python_client/parser/properties/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NoneProperty(Property):
""" A property that is always None (used for empty schemas) """

_type_string: ClassVar[str] = "None"
template: ClassVar[Optional[str]] = "none_property.pyi"
template: ClassVar[Optional[str]] = "none_property.py.jinja"


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

_type_string: ClassVar[str] = "datetime.datetime"
template: ClassVar[str] = "datetime_property.pyi"
template: ClassVar[str] = "datetime_property.py.jinja"

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

_type_string: ClassVar[str] = "datetime.date"
template: ClassVar[str] = "date_property.pyi"
template: ClassVar[str] = "date_property.py.jinja"

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

_type_string: ClassVar[str] = "File"
template: ClassVar[str] = "file_property.pyi"
template: ClassVar[str] = "file_property.py.jinja"

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

inner_property: InnerProp
template: ClassVar[str] = "list_property.pyi"
template: ClassVar[str] = "list_property.py.jinja"

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

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

def __attrs_post_init__(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion openapi_python_client/parser/properties/enum_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class EnumProperty(Property):
value_type: Type[ValueType]
default: Optional[Any] = attr.ib()

template: ClassVar[str] = "enum_property.pyi"
template: ClassVar[str] = "enum_property.py.jinja"

def get_type_string(self, no_optional: bool = False) -> str:
""" Get a string representation of type that should be used when declaring this property """
Expand Down
2 changes: 1 addition & 1 deletion openapi_python_client/parser/properties/model_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ModelProperty(Property):
relative_imports: Set[str]
additional_properties: Union[bool, Property]

template: ClassVar[str] = "model_property.pyi"
template: ClassVar[str] = "model_property.py.jinja"

def get_type_string(self, no_optional: bool = False) -> str:
""" Get a string representation of type that should be used when declaring this property """
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ from ...types import Response
{{ relative }}
{% endfor %}

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

{% set return_string = return_type(endpoint) %}
{% set parsed_responses = (endpoint.responses | length > 0) and return_string != "None" %}
Expand Down
File renamed without changes.

This file was deleted.

File renamed without changes.
File renamed without changes.
Loading

0 comments on commit d76bae8

Please sign in to comment.