@@ -44,6 +44,7 @@ class Project:
4444 project_name_override : Optional [str ] = None
4545 package_name_override : Optional [str ] = None
4646 package_version_override : Optional [str ] = None
47+ encoding : Optional [str ] = None
4748
4849 def __init__ (self , * , openapi : GeneratorData , meta : MetaType , custom_template_path : Optional [Path ] = None ) -> None :
4950 self .openapi : GeneratorData = openapi
@@ -137,15 +138,17 @@ def _create_package(self) -> None:
137138 package_init = self .package_dir / "__init__.py"
138139
139140 package_init_template = self .env .get_template ("package_init.py.jinja" )
140- package_init .write_text (package_init_template .render (description = self .package_description ))
141+ package_init .write_text (
142+ package_init_template .render (description = self .package_description ), encoding = self .encoding
143+ )
141144
142145 if self .meta != MetaType .NONE :
143146 pytyped = self .package_dir / "py.typed"
144- pytyped .write_text ("# Marker file for PEP 561" )
147+ pytyped .write_text ("# Marker file for PEP 561" , encoding = self . encoding )
145148
146149 types_template = self .env .get_template ("types.py.jinja" )
147150 types_path = self .package_dir / "types.py"
148- types_path .write_text (types_template .render ())
151+ types_path .write_text (types_template .render (), encoding = self . encoding )
149152
150153 def _build_metadata (self ) -> None :
151154 if self .meta == MetaType .NONE :
@@ -167,7 +170,7 @@ def _build_metadata(self) -> None:
167170 # .gitignore
168171 git_ignore_path = self .project_dir / ".gitignore"
169172 git_ignore_template = self .env .get_template (".gitignore.jinja" )
170- git_ignore_path .write_text (git_ignore_template .render ())
173+ git_ignore_path .write_text (git_ignore_template .render (), encoding = self . encoding )
171174
172175 def _build_pyproject_toml (self , * , use_poetry : bool ) -> None :
173176 template = "pyproject.toml.jinja" if use_poetry else "pyproject_no_poetry.toml.jinja"
@@ -204,7 +207,7 @@ def _build_models(self) -> None:
204207 model_template = self .env .get_template ("model.py.jinja" )
205208 for model in self .openapi .models .values ():
206209 module_path = models_dir / f"{ model .reference .module_name } .py"
207- module_path .write_text (model_template .render (model = model ))
210+ module_path .write_text (model_template .render (model = model ), encoding = self . encoding )
208211 imports .append (import_string_from_reference (model .reference ))
209212
210213 # Generate enums
@@ -213,25 +216,25 @@ def _build_models(self) -> None:
213216 for enum in self .openapi .enums .values ():
214217 module_path = models_dir / f"{ enum .reference .module_name } .py"
215218 if enum .value_type is int :
216- module_path .write_text (int_enum_template .render (enum = enum ))
219+ module_path .write_text (int_enum_template .render (enum = enum ), encoding = self . encoding )
217220 else :
218- module_path .write_text (str_enum_template .render (enum = enum ))
221+ module_path .write_text (str_enum_template .render (enum = enum ), encoding = self . encoding )
219222 imports .append (import_string_from_reference (enum .reference ))
220223
221224 models_init_template = self .env .get_template ("models_init.py.jinja" )
222- models_init .write_text (models_init_template .render (imports = imports ))
225+ models_init .write_text (models_init_template .render (imports = imports ), encoding = self . encoding )
223226
224227 def _build_api (self ) -> None :
225228 # Generate Client
226229 client_path = self .package_dir / "client.py"
227230 client_template = self .env .get_template ("client.py.jinja" )
228- client_path .write_text (client_template .render ())
231+ client_path .write_text (client_template .render (), encoding = self . encoding )
229232
230233 # Generate endpoints
231234 api_dir = self .package_dir / "api"
232235 api_dir .mkdir ()
233236 api_init = api_dir / "__init__.py"
234- api_init .write_text ('""" Contains methods for accessing the API """' )
237+ api_init .write_text ('""" Contains methods for accessing the API """' , encoding = self . encoding )
235238
236239 endpoint_template = self .env .get_template ("endpoint_module.py.jinja" )
237240 for tag , collection in self .openapi .endpoint_collections_by_tag .items ():
@@ -242,7 +245,7 @@ def _build_api(self) -> None:
242245
243246 for endpoint in collection .endpoints :
244247 module_path = tag_dir / f"{ snake_case (endpoint .name )} .py"
245- module_path .write_text (endpoint_template .render (endpoint = endpoint ))
248+ module_path .write_text (endpoint_template .render (endpoint = endpoint ), encoding = self . encoding )
246249
247250
248251def _get_project_for_url_or_path (
@@ -258,7 +261,12 @@ def _get_project_for_url_or_path(
258261
259262
260263def create_new_client (
261- * , url : Optional [str ], path : Optional [Path ], meta : MetaType , custom_template_path : Optional [Path ] = None
264+ * ,
265+ url : Optional [str ],
266+ path : Optional [Path ],
267+ meta : MetaType ,
268+ custom_template_path : Optional [Path ] = None ,
269+ encoding = Optional [str ],
262270) -> Sequence [GeneratorError ]:
263271 """
264272 Generate the client library
@@ -267,6 +275,7 @@ def create_new_client(
267275 A list containing any errors encountered when generating.
268276 """
269277 project = _get_project_for_url_or_path (url = url , path = path , custom_template_path = custom_template_path , meta = meta )
278+ project .encoding = encoding
270279 if isinstance (project , GeneratorError ):
271280 return [project ]
272281 return project .build ()
0 commit comments