Skip to content

Commit 794672f

Browse files
committed
Merge branch 'master' into pr/30
2 parents 48c2af1 + 50392cd commit 794672f

File tree

21 files changed

+104
-69
lines changed

21 files changed

+104
-69
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## 0.3.0 - Unreleased
8+
### Additions
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 @acgray!
13+
714
## 0.2.1 - 2020-03-22
815
### Fixes
916
- Fixed import of errors.py in generated api modules

openapi_python_client/openapi_parser/openapi.py

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

205205
@dataclass
206206
class OpenAPI:
207-
""" Top level OpenAPI spec """
207+
""" Top level OpenAPI document """
208208

209209
title: str
210-
description: str
210+
description: Optional[str]
211211
version: str
212212
schemas: Dict[str, Schema]
213213
endpoint_collections_by_tag: Dict[str, EndpointCollection]
@@ -256,7 +256,7 @@ def from_dict(d: Dict[str, Dict[str, Any]], /) -> OpenAPI:
256256

257257
return OpenAPI(
258258
title=d["info"]["title"],
259-
description=d["info"]["description"],
259+
description=d["info"].get("description"),
260260
version=d["info"]["version"],
261261
endpoint_collections_by_tag=endpoint_collections_by_tag,
262262
schemas=schemas,

openapi_python_client/openapi_parser/properties.py

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

182182
def constructor_from_dict(self, dict_name: str) -> str:
183183
""" How to load this property from a dict (used in generated model from_dict function """
184-
return f'{self.reference.class_name}({dict_name}["{self.name}"]) if "{self.name}" in {dict_name} else None'
184+
constructor = f'{self.reference.class_name}({dict_name}["{self.name}"])'
185+
if not self.required:
186+
constructor += f' if "{self.name}" in {dict_name} else None'
187+
return constructor
185188

186189
@staticmethod
187190
def values_from_list(l: List[str], /) -> Dict[str, str]:
@@ -222,15 +225,15 @@ def transform(self) -> str:
222225
class DictProperty(Property):
223226
""" Property that is a general Dict """
224227

225-
_type_string: ClassVar[str] = "Dict"
228+
_type_string: ClassVar[str] = "Dict[Any, Any]"
226229

227230

228231
_openapi_types_to_python_type_strings = {
229232
"string": "str",
230233
"number": "float",
231234
"integer": "int",
232235
"boolean": "bool",
233-
"object": "Dict",
236+
"object": "Dict[Any, Any]",
234237
}
235238

236239

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 %}

poetry.lock

Lines changed: 24 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "openapi-python-client"
33
version = "0.2.1"
44
description = "Generate modern Python clients from OpenAPI"
5+
repository = "https://github.com/triaxtec/openapi-python-client"
56

67
authors = [
78
"Dylan Anthony <danthony@triaxtec.com>",
@@ -36,7 +37,7 @@ mypy = ">=0.761"
3637
taskipy = "^1.1.3"
3738
safety = "^1.8.5"
3839
pytest-cov = "^2.8.1"
39-
fastapi = "^0.52.0"
40+
fastapi = "^0.54.1"
4041

4142
[tool.taskipy.tasks]
4243
check = "isort --recursive --apply && black . && safety check && mypy openapi_python_client"

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)

0 commit comments

Comments
 (0)