Skip to content

Commit

Permalink
python: type API client internal structures (OpenAPITools#16692)
Browse files Browse the repository at this point in the history
* In samples/openapi3/client/petstore/python, this reduces the mypy errors from 435 to 178 errors
* In samples/openapi3/client/petstore/python-aiohttp, this reduces the mypy errors from 381 to 124 errors
* In samples/client/echo_api/python, this reduces the mypy errors from 131 to 25 errors
  • Loading branch information
multani authored and AlanCitrix committed Oct 26, 2023
1 parent be90364 commit a85b94c
Show file tree
Hide file tree
Showing 21 changed files with 813 additions and 792 deletions.
17 changes: 9 additions & 8 deletions modules/openapi-generator/src/main/resources/python/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io
import warnings

from pydantic import validate_call, ValidationError
from typing import Dict, List, Optional, Tuple

{{#imports}}
{{import}}
Expand Down Expand Up @@ -179,10 +180,10 @@ class {{classname}}:
_params[_key] = _val
del _params['kwargs']

_collection_formats = {}
_collection_formats: Dict[str, str] = {}

# process the path parameters
_path_params = {}
_path_params: Dict[str, str] = {}
{{#pathParams}}
if _params['{{paramName}}'] is not None:
_path_params['{{baseName}}'] = _params['{{paramName}}']
Expand All @@ -193,7 +194,7 @@ class {{classname}}:
{{/pathParams}}

# process the query parameters
_query_params = []
_query_params: List[Tuple[str, str]] = []
{{#queryParams}}
if _params.get('{{paramName}}') is not None: # noqa: E501
{{#isDateTime}}
Expand Down Expand Up @@ -229,8 +230,8 @@ class {{classname}}:

{{/headerParams}}
# process the form parameters
_form_params = []
_files = {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, str] = {}
{{#formParams}}
if _params['{{paramName}}'] is not None:
{{^isFile}}
Expand Down Expand Up @@ -274,12 +275,12 @@ class {{classname}}:

{{/hasConsumes}}
# authentication setting
_auth_settings = [{{#authMethods}}'{{name}}'{{^-last}}, {{/-last}}{{/authMethods}}] # noqa: E501
_auth_settings: List[str] = [{{#authMethods}}'{{name}}'{{^-last}}, {{/-last}}{{/authMethods}}] # noqa: E501

{{#returnType}}
{{#responses}}
{{#-first}}
_response_types_map = {
_response_types_map: Dict[str, Optional[str]] = {
{{/-first}}
{{^isWildcard}}
'{{code}}': {{#dataType}}"{{.}}"{{/dataType}}{{^dataType}}None{{/dataType}},
Expand All @@ -290,7 +291,7 @@ class {{classname}}:
{{/responses}}
{{/returnType}}
{{^returnType}}
_response_types_map = {}
_response_types_map: Dict[str, Optional[str]] = {}
{{/returnType}}

return {{#asyncio}}await {{/asyncio}}self.api_client.call_api(
Expand Down
15 changes: 8 additions & 7 deletions samples/client/echo_api/python/openapi_client/api/auth_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import warnings

from pydantic import validate_call, ValidationError
from typing import Dict, List, Optional, Tuple


from openapi_client.api_client import ApiClient
Expand Down Expand Up @@ -130,28 +131,28 @@ def test_auth_http_basic_with_http_info(self, **kwargs) -> ApiResponse: # noqa:
_params[_key] = _val
del _params['kwargs']

_collection_formats = {}
_collection_formats: Dict[str, str] = {}

# process the path parameters
_path_params = {}
_path_params: Dict[str, str] = {}

# process the query parameters
_query_params = []
_query_params: List[Tuple[str, str]] = []
# process the header parameters
_header_params = dict(_params.get('_headers', {}))
# process the form parameters
_form_params = []
_files = {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, str] = {}
# process the body parameter
_body_params = None
# set the HTTP header `Accept`
_header_params['Accept'] = self.api_client.select_header_accept(
['text/plain']) # noqa: E501

# authentication setting
_auth_settings = ['http_auth'] # noqa: E501
_auth_settings: List[str] = ['http_auth'] # noqa: E501

_response_types_map = {
_response_types_map: Dict[str, Optional[str]] = {
'200': "str",
}

Expand Down
99 changes: 50 additions & 49 deletions samples/client/echo_api/python/openapi_client/api/body_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import warnings

from pydantic import validate_call, ValidationError
from typing import Dict, List, Optional, Tuple

from pydantic import Field
from typing_extensions import Annotated
Expand Down Expand Up @@ -138,28 +139,28 @@ def test_binary_gif_with_http_info(self, **kwargs) -> ApiResponse: # noqa: E501
_params[_key] = _val
del _params['kwargs']

_collection_formats = {}
_collection_formats: Dict[str, str] = {}

# process the path parameters
_path_params = {}
_path_params: Dict[str, str] = {}

# process the query parameters
_query_params = []
_query_params: List[Tuple[str, str]] = []
# process the header parameters
_header_params = dict(_params.get('_headers', {}))
# process the form parameters
_form_params = []
_files = {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, str] = {}
# process the body parameter
_body_params = None
# set the HTTP header `Accept`
_header_params['Accept'] = self.api_client.select_header_accept(
['image/gif']) # noqa: E501

# authentication setting
_auth_settings = [] # noqa: E501
_auth_settings: List[str] = [] # noqa: E501

_response_types_map = {
_response_types_map: Dict[str, Optional[str]] = {
'200': "bytearray",
}

Expand Down Expand Up @@ -275,18 +276,18 @@ def test_body_application_octetstream_binary_with_http_info(self, body : Optiona
_params[_key] = _val
del _params['kwargs']

_collection_formats = {}
_collection_formats: Dict[str, str] = {}

# process the path parameters
_path_params = {}
_path_params: Dict[str, str] = {}

# process the query parameters
_query_params = []
_query_params: List[Tuple[str, str]] = []
# process the header parameters
_header_params = dict(_params.get('_headers', {}))
# process the form parameters
_form_params = []
_files = {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, str] = {}
# process the body parameter
_body_params = None
if _params['body'] is not None:
Expand All @@ -309,9 +310,9 @@ def test_body_application_octetstream_binary_with_http_info(self, body : Optiona
_header_params['Content-Type'] = _content_types_list

# authentication setting
_auth_settings = [] # noqa: E501
_auth_settings: List[str] = [] # noqa: E501

_response_types_map = {
_response_types_map: Dict[str, Optional[str]] = {
'200': "str",
}

Expand Down Expand Up @@ -427,18 +428,18 @@ def test_body_multipart_formdata_array_of_binary_with_http_info(self, files : Li
_params[_key] = _val
del _params['kwargs']

_collection_formats = {}
_collection_formats: Dict[str, str] = {}

# process the path parameters
_path_params = {}
_path_params: Dict[str, str] = {}

# process the query parameters
_query_params = []
_query_params: List[Tuple[str, str]] = []
# process the header parameters
_header_params = dict(_params.get('_headers', {}))
# process the form parameters
_form_params = []
_files = {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, str] = {}
if _params['files'] is not None:
_files['files'] = _params['files']
_collection_formats['files'] = 'csv'
Expand All @@ -457,9 +458,9 @@ def test_body_multipart_formdata_array_of_binary_with_http_info(self, files : Li
_header_params['Content-Type'] = _content_types_list

# authentication setting
_auth_settings = [] # noqa: E501
_auth_settings: List[str] = [] # noqa: E501

_response_types_map = {
_response_types_map: Dict[str, Optional[str]] = {
'200': "str",
}

Expand Down Expand Up @@ -575,18 +576,18 @@ def test_echo_body_free_form_object_response_string_with_http_info(self, body :
_params[_key] = _val
del _params['kwargs']

_collection_formats = {}
_collection_formats: Dict[str, str] = {}

# process the path parameters
_path_params = {}
_path_params: Dict[str, str] = {}

# process the query parameters
_query_params = []
_query_params: List[Tuple[str, str]] = []
# process the header parameters
_header_params = dict(_params.get('_headers', {}))
# process the form parameters
_form_params = []
_files = {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, str] = {}
# process the body parameter
_body_params = None
if _params['body'] is not None:
Expand All @@ -604,9 +605,9 @@ def test_echo_body_free_form_object_response_string_with_http_info(self, body :
_header_params['Content-Type'] = _content_types_list

# authentication setting
_auth_settings = [] # noqa: E501
_auth_settings: List[str] = [] # noqa: E501

_response_types_map = {
_response_types_map: Dict[str, Optional[str]] = {
'200': "str",
}

Expand Down Expand Up @@ -722,18 +723,18 @@ def test_echo_body_pet_with_http_info(self, pet : Annotated[Optional[Pet], Field
_params[_key] = _val
del _params['kwargs']

_collection_formats = {}
_collection_formats: Dict[str, str] = {}

# process the path parameters
_path_params = {}
_path_params: Dict[str, str] = {}

# process the query parameters
_query_params = []
_query_params: List[Tuple[str, str]] = []
# process the header parameters
_header_params = dict(_params.get('_headers', {}))
# process the form parameters
_form_params = []
_files = {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, str] = {}
# process the body parameter
_body_params = None
if _params['pet'] is not None:
Expand All @@ -751,9 +752,9 @@ def test_echo_body_pet_with_http_info(self, pet : Annotated[Optional[Pet], Field
_header_params['Content-Type'] = _content_types_list

# authentication setting
_auth_settings = [] # noqa: E501
_auth_settings: List[str] = [] # noqa: E501

_response_types_map = {
_response_types_map: Dict[str, Optional[str]] = {
'200': "Pet",
}

Expand Down Expand Up @@ -869,18 +870,18 @@ def test_echo_body_pet_response_string_with_http_info(self, pet : Annotated[Opti
_params[_key] = _val
del _params['kwargs']

_collection_formats = {}
_collection_formats: Dict[str, str] = {}

# process the path parameters
_path_params = {}
_path_params: Dict[str, str] = {}

# process the query parameters
_query_params = []
_query_params: List[Tuple[str, str]] = []
# process the header parameters
_header_params = dict(_params.get('_headers', {}))
# process the form parameters
_form_params = []
_files = {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, str] = {}
# process the body parameter
_body_params = None
if _params['pet'] is not None:
Expand All @@ -898,9 +899,9 @@ def test_echo_body_pet_response_string_with_http_info(self, pet : Annotated[Opti
_header_params['Content-Type'] = _content_types_list

# authentication setting
_auth_settings = [] # noqa: E501
_auth_settings: List[str] = [] # noqa: E501

_response_types_map = {
_response_types_map: Dict[str, Optional[str]] = {
'200': "str",
}

Expand Down Expand Up @@ -1016,18 +1017,18 @@ def test_echo_body_tag_response_string_with_http_info(self, tag : Annotated[Opti
_params[_key] = _val
del _params['kwargs']

_collection_formats = {}
_collection_formats: Dict[str, str] = {}

# process the path parameters
_path_params = {}
_path_params: Dict[str, str] = {}

# process the query parameters
_query_params = []
_query_params: List[Tuple[str, str]] = []
# process the header parameters
_header_params = dict(_params.get('_headers', {}))
# process the form parameters
_form_params = []
_files = {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, str] = {}
# process the body parameter
_body_params = None
if _params['tag'] is not None:
Expand All @@ -1045,9 +1046,9 @@ def test_echo_body_tag_response_string_with_http_info(self, tag : Annotated[Opti
_header_params['Content-Type'] = _content_types_list

# authentication setting
_auth_settings = [] # noqa: E501
_auth_settings: List[str] = [] # noqa: E501

_response_types_map = {
_response_types_map: Dict[str, Optional[str]] = {
'200': "str",
}

Expand Down
Loading

0 comments on commit a85b94c

Please sign in to comment.