From daf507b57daa467001cd7e874df9c5fe9bbaeb9f Mon Sep 17 00:00:00 2001 From: Elton Chou Date: Tue, 27 Sep 2022 02:23:30 +0800 Subject: [PATCH] feat: Improve library interface ref: https://github.com/microsoft/pyright/blob/main/docs/typed-libraries.md#library-interface --- .../my_test_api_client/__init__.py | 5 ++ .../my_test_api_client/models/__init__.py | 49 +++++++++++++++++++ .../integration_tests/__init__.py | 5 ++ .../integration_tests/models/__init__.py | 8 +++ openapi_python_client/__init__.py | 5 +- .../templates/models_init.py.jinja | 8 +++ .../templates/package_init.py.jinja | 5 ++ 7 files changed, 84 insertions(+), 1 deletion(-) diff --git a/end_to_end_tests/golden-record/my_test_api_client/__init__.py b/end_to_end_tests/golden-record/my_test_api_client/__init__.py index 0f240c245..530928e7a 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/__init__.py +++ b/end_to_end_tests/golden-record/my_test_api_client/__init__.py @@ -1,2 +1,7 @@ """ A client library for accessing My Test API """ from .client import AuthenticatedClient, Client + +__all__ = ( + "AuthenticatedClient", + "Client", +) diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/__init__.py b/end_to_end_tests/golden-record/my_test_api_client/models/__init__.py index 5532f88ae..04344ee19 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/__init__.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/__init__.py @@ -50,3 +50,52 @@ from .test_inline_objects_json_body import TestInlineObjectsJsonBody from .test_inline_objects_response_200 import TestInlineObjectsResponse200 from .validation_error import ValidationError + +__all__ = ( + "AFormData", + "AllOfSubModel", + "AllOfSubModelTypeEnum", + "AModel", + "AModelWithPropertiesReferenceThatAreNotObject", + "AnAllOfEnum", + "AnEnum", + "AnEnumWithNull", + "AnIntEnum", + "AnotherAllOfSubModel", + "AnotherAllOfSubModelType", + "AnotherAllOfSubModelTypeEnum", + "BodyUploadFileTestsUploadPost", + "BodyUploadFileTestsUploadPostAdditionalProperty", + "BodyUploadFileTestsUploadPostSomeNullableObject", + "BodyUploadFileTestsUploadPostSomeObject", + "BodyUploadFileTestsUploadPostSomeOptionalObject", + "DifferentEnum", + "FreeFormModel", + "GetLocationHeaderTypesIntEnumHeader", + "GetLocationHeaderTypesStringEnumHeader", + "HTTPValidationError", + "Import", + "ModelFromAllOf", + "ModelName", + "ModelReferenceWithPeriods", + "ModelWithAdditionalPropertiesInlined", + "ModelWithAdditionalPropertiesInlinedAdditionalProperty", + "ModelWithAdditionalPropertiesRefed", + "ModelWithAnyJsonProperties", + "ModelWithAnyJsonPropertiesAdditionalPropertyType0", + "ModelWithDateTimeProperty", + "ModelWithPrimitiveAdditionalProperties", + "ModelWithPrimitiveAdditionalPropertiesADateHolder", + "ModelWithPropertyRef", + "ModelWithUnionProperty", + "ModelWithUnionPropertyInlined", + "ModelWithUnionPropertyInlinedFruitType0", + "ModelWithUnionPropertyInlinedFruitType1", + "None_", + "PostFormDataInlineData", + "PostResponsesUnionsSimpleBeforeComplexResponse200", + "PostResponsesUnionsSimpleBeforeComplexResponse200AType1", + "TestInlineObjectsJsonBody", + "TestInlineObjectsResponse200", + "ValidationError", +) diff --git a/integration-tests/integration_tests/__init__.py b/integration-tests/integration_tests/__init__.py index 846ce4de1..48f0fb8da 100644 --- a/integration-tests/integration_tests/__init__.py +++ b/integration-tests/integration_tests/__init__.py @@ -1,2 +1,7 @@ """ A client library for accessing OpenAPI Test Server """ from .client import AuthenticatedClient, Client + +__all__ = ( + "AuthenticatedClient", + "Client", +) diff --git a/integration-tests/integration_tests/models/__init__.py b/integration-tests/integration_tests/models/__init__.py index a8044ede4..28d550bb2 100644 --- a/integration-tests/integration_tests/models/__init__.py +++ b/integration-tests/integration_tests/models/__init__.py @@ -5,3 +5,11 @@ from .post_parameters_header_response_200 import PostParametersHeaderResponse200 from .problem import Problem from .public_error import PublicError + +__all__ = ( + "PostBodyMultipartMultipartData", + "PostBodyMultipartResponse200", + "PostParametersHeaderResponse200", + "Problem", + "PublicError", +) diff --git a/openapi_python_client/__init__.py b/openapi_python_client/__init__.py index 7c8683a90..44fc39cd4 100644 --- a/openapi_python_client/__init__.py +++ b/openapi_python_client/__init__.py @@ -233,12 +233,14 @@ def _build_models(self) -> None: models_dir.mkdir() models_init = models_dir / "__init__.py" imports = [] + alls = [] model_template = self.env.get_template("model.py.jinja") for model in self.openapi.models: module_path = models_dir / f"{model.class_info.module_name}.py" module_path.write_text(model_template.render(model=model), encoding=self.file_encoding) imports.append(import_string_from_class(model.class_info)) + alls.append(model.class_info.name) # Generate enums str_enum_template = self.env.get_template("str_enum.py.jinja") @@ -250,9 +252,10 @@ def _build_models(self) -> None: else: module_path.write_text(str_enum_template.render(enum=enum), encoding=self.file_encoding) imports.append(import_string_from_class(enum.class_info)) + alls.append(enum.class_info.name) models_init_template = self.env.get_template("models_init.py.jinja") - models_init.write_text(models_init_template.render(imports=imports), encoding=self.file_encoding) + models_init.write_text(models_init_template.render(imports=imports, alls=alls), encoding=self.file_encoding) def _build_api(self) -> None: # Generate Client diff --git a/openapi_python_client/templates/models_init.py.jinja b/openapi_python_client/templates/models_init.py.jinja index d59542263..7379e86ad 100644 --- a/openapi_python_client/templates/models_init.py.jinja +++ b/openapi_python_client/templates/models_init.py.jinja @@ -3,3 +3,11 @@ {% for import in imports | sort %} {{ import }} {% endfor %} + +{% if imports %} +__all__ = ( + {% for all in alls | sort %} + "{{ all }}", + {% endfor %} +) +{% endif %} diff --git a/openapi_python_client/templates/package_init.py.jinja b/openapi_python_client/templates/package_init.py.jinja index f146549d0..366a7e508 100644 --- a/openapi_python_client/templates/package_init.py.jinja +++ b/openapi_python_client/templates/package_init.py.jinja @@ -1,2 +1,7 @@ """ {{ package_description }} """ from .client import AuthenticatedClient, Client + +__all__ = ( + "AuthenticatedClient", + "Client", +)