Skip to content

Commit 7a75caa

Browse files
authored
Merge pull request #35 from triaxtec/feature/type-checking
Run mypy in end to end test
2 parents 6515e42 + c2301ab commit 7a75caa

File tree

19 files changed

+75
-50
lines changed

19 files changed

+75
-50
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## 0.3.0 - Unreleased
88
### Additions
9-
- Link to the GitHub repository from PyPI (#26) (Thanks @theY4Kman)
9+
- Link to the GitHub repository from PyPI (#26). Thanks @theY4Kman!
10+
11+
### Fixes
12+
- Fixed some typing issues in generated clients and incorporate mypy into end to end tests (#32). Thanks @agray!
1013

1114
## 0.2.1 - 2020-03-22
1215
### Fixes

openapi_python_client/openapi_parser/openapi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ def dict(d: Dict[str, Dict[str, Any]], /) -> Dict[str, Schema]:
191191

192192
@dataclass
193193
class OpenAPI:
194-
""" Top level OpenAPI spec """
194+
""" Top level OpenAPI document """
195195

196196
title: str
197-
description: str
197+
description: Optional[str]
198198
version: str
199199
schemas: Dict[str, Schema]
200200
endpoint_collections_by_tag: Dict[str, EndpointCollection]
@@ -243,7 +243,7 @@ def from_dict(d: Dict[str, Dict[str, Any]], /) -> OpenAPI:
243243

244244
return OpenAPI(
245245
title=d["info"]["title"],
246-
description=d["info"]["description"],
246+
description=d["info"].get("description"),
247247
version=d["info"]["version"],
248248
endpoint_collections_by_tag=endpoint_collections_by_tag,
249249
schemas=schemas,

openapi_python_client/openapi_parser/properties.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ def transform(self) -> str:
167167

168168
def constructor_from_dict(self, dict_name: str) -> str:
169169
""" How to load this property from a dict (used in generated model from_dict function """
170-
return f'{self.reference.class_name}({dict_name}["{self.name}"]) if "{self.name}" in {dict_name} else None'
170+
constructor = f'{self.reference.class_name}({dict_name}["{self.name}"])'
171+
if not self.required:
172+
constructor += f' if "{self.name}" in {dict_name} else None'
173+
return constructor
171174

172175
@staticmethod
173176
def values_from_list(l: List[str], /) -> Dict[str, str]:
@@ -208,15 +211,15 @@ def transform(self) -> str:
208211
class DictProperty(Property):
209212
""" Property that is a general Dict """
210213

211-
_type_string: ClassVar[str] = "Dict"
214+
_type_string: ClassVar[str] = "Dict[Any, Any]"
212215

213216

214217
_openapi_types_to_python_type_strings = {
215218
"string": "str",
216219
"number": "float",
217220
"integer": "int",
218221
"boolean": "bool",
219-
"object": "Dict",
222+
"object": "Dict[Any, Any]",
220223
}
221224

222225

openapi_python_client/openapi_parser/responses.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def return_string(self) -> str:
3333

3434
def constructor(self) -> str:
3535
""" How the return value of this response should be constructed """
36-
return f"[{self.reference.class_name}.from_dict(item) for item in response.json()]"
36+
return f"[{self.reference.class_name}.from_dict(item) for item in cast(List[Dict[str, Any]], response.json())]"
3737

3838

3939
@dataclass
@@ -48,7 +48,7 @@ def return_string(self) -> str:
4848

4949
def constructor(self) -> str:
5050
""" How the return value of this response should be constructed """
51-
return f"{self.reference.class_name}.from_dict(response.json())"
51+
return f"{self.reference.class_name}.from_dict(cast(Dict[str, Any], response.json()))"
5252

5353

5454
@dataclass

openapi_python_client/templates/async_endpoint_module.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import asdict
2-
from typing import Dict, List, Optional, Union
2+
from typing import Any, Dict, List, Optional, Union, cast
33

44
import httpx
55

@@ -60,8 +60,8 @@ async def {{ endpoint.name }}(
6060
{% endfor %}
6161
{% endif %}
6262

63-
with httpx.AsyncClient() as client:
64-
response = await client.{{ endpoint.method }}(
63+
async with httpx.AsyncClient() as _client:
64+
response = await _client.{{ endpoint.method }}(
6565
url=url,
6666
headers=client.get_headers(),
6767
{% if endpoint.form_body_reference %}

openapi_python_client/templates/endpoint_module.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import asdict
2-
from typing import Dict, List, Optional, Union
2+
from typing import Any, Dict, List, Optional, Union, cast
33

44
import httpx
55

openapi_python_client/templates/model.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from __future__ import annotations
22

33
from dataclasses import dataclass
44
from datetime import datetime
5-
from typing import Dict, List, Optional, cast
5+
from typing import Any, Dict, List, Optional, cast
66

77
{% for relative in schema.relative_imports %}
88
{{ relative }}
@@ -16,7 +16,7 @@ class {{ schema.reference.class_name }}:
1616
{{ property.to_string() }}
1717
{% endfor %}
1818

19-
def to_dict(self) -> Dict:
19+
def to_dict(self) -> Dict[str, Any]:
2020
return {
2121
{% for property in schema.required_properties %}
2222
"{{ property.name }}": self.{{ property.transform() }},
@@ -27,7 +27,7 @@ class {{ schema.reference.class_name }}:
2727
}
2828

2929
@staticmethod
30-
def from_dict(d: Dict) -> {{ schema.reference.class_name }}:
30+
def from_dict(d: Dict[str, Any]) -> {{ schema.reference.class_name }}:
3131
{% for property in schema.required_properties + schema.optional_properties %}
3232

3333
{% if property.constructor_template %}

tests/test_end_to_end/golden-master/my_test_api_client/api/default.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import asdict
2-
from typing import Dict, List, Optional, Union
2+
from typing import Any, Dict, List, Optional, Union, cast
33

44
import httpx
55

@@ -19,6 +19,6 @@ def ping_ping_get(
1919
response = httpx.get(url=url, headers=client.get_headers(),)
2020

2121
if response.status_code == 200:
22-
return ABCResponse.from_dict(response.json())
22+
return ABCResponse.from_dict(cast(Dict[str, Any], response.json()))
2323
else:
2424
raise ApiResponseError(response=response)

tests/test_end_to_end/golden-master/my_test_api_client/api/users.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import asdict
2-
from typing import Dict, List, Optional, Union
2+
from typing import Any, Dict, List, Optional, Union, cast
33

44
import httpx
55

@@ -25,8 +25,8 @@ def get_list_tests__get(
2525
response = httpx.get(url=url, headers=client.get_headers(), params=params,)
2626

2727
if response.status_code == 200:
28-
return [AModel.from_dict(item) for item in response.json()]
28+
return [AModel.from_dict(item) for item in cast(List[Dict[str, Any]], response.json())]
2929
if response.status_code == 422:
30-
return HTTPValidationError.from_dict(response.json())
30+
return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json()))
3131
else:
3232
raise ApiResponseError(response=response)

tests/test_end_to_end/golden-master/my_test_api_client/async_api/default.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import asdict
2-
from typing import Dict, List, Optional, Union
2+
from typing import Any, Dict, List, Optional, Union, cast
33

44
import httpx
55

@@ -16,10 +16,10 @@ async def ping_ping_get(
1616
""" A quick check to see if the system is running """
1717
url = f"{client.base_url}/ping"
1818

19-
with httpx.AsyncClient() as client:
20-
response = await client.get(url=url, headers=client.get_headers(),)
19+
async with httpx.AsyncClient() as _client:
20+
response = await _client.get(url=url, headers=client.get_headers(),)
2121

2222
if response.status_code == 200:
23-
return ABCResponse.from_dict(response.json())
23+
return ABCResponse.from_dict(cast(Dict[str, Any], response.json()))
2424
else:
2525
raise ApiResponseError(response=response)

0 commit comments

Comments
 (0)