Skip to content

Commit f082a35

Browse files
authored
[Python] fix object arrays giving mypy error "Incompatible types in assignment" in to_dict() (#19223)
* [python] mypy fix for multiple arrays of objects * [python] mypy test for multiple arrays of objects
1 parent 755b2ef commit f082a35

File tree

58 files changed

+902
-101
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+902
-101
lines changed

modules/openapi-generator/src/main/resources/python/model_generic.mustache

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
166166
# override the default output from pydantic by calling `to_dict()` of each item in {{{name}}} (list of list)
167167
_items = []
168168
if self.{{{name}}}:
169-
for _item in self.{{{name}}}:
170-
if _item:
169+
for _item_{{{name}}} in self.{{{name}}}:
170+
if _item_{{{name}}}:
171171
_items.append(
172-
[_inner_item.to_dict() for _inner_item in _item if _inner_item is not None]
172+
[_inner_item.to_dict() for _inner_item in _item_{{{name}}} if _inner_item is not None]
173173
)
174174
_dict['{{{baseName}}}'] = _items
175175
{{/items.items.isPrimitiveType}}
@@ -180,9 +180,9 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
180180
# override the default output from pydantic by calling `to_dict()` of each item in {{{name}}} (list)
181181
_items = []
182182
if self.{{{name}}}:
183-
for _item in self.{{{name}}}:
184-
if _item:
185-
_items.append(_item.to_dict())
183+
for _item_{{{name}}} in self.{{{name}}}:
184+
if _item_{{{name}}}:
185+
_items.append(_item_{{{name}}}.to_dict())
186186
_dict['{{{baseName}}}'] = _items
187187
{{/items.isEnumOrRef}}
188188
{{/items.isPrimitiveType}}
@@ -194,10 +194,10 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
194194
# override the default output from pydantic by calling `to_dict()` of each value in {{{name}}} (dict of array)
195195
_field_dict_of_array = {}
196196
if self.{{{name}}}:
197-
for _key in self.{{{name}}}:
198-
if self.{{{name}}}[_key] is not None:
199-
_field_dict_of_array[_key] = [
200-
_item.to_dict() for _item in self.{{{name}}}[_key]
197+
for _key_{{{name}}} in self.{{{name}}}:
198+
if self.{{{name}}}[_key_{{{name}}}] is not None:
199+
_field_dict_of_array[_key_{{{name}}}] = [
200+
_item.to_dict() for _item in self.{{{name}}}[_key_{{{name}}}]
201201
]
202202
_dict['{{{baseName}}}'] = _field_dict_of_array
203203
{{/items.items.isPrimitiveType}}
@@ -208,9 +208,9 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
208208
# override the default output from pydantic by calling `to_dict()` of each value in {{{name}}} (dict)
209209
_field_dict = {}
210210
if self.{{{name}}}:
211-
for _key in self.{{{name}}}:
212-
if self.{{{name}}}[_key]:
213-
_field_dict[_key] = self.{{{name}}}[_key].to_dict()
211+
for _key_{{{name}}} in self.{{{name}}}:
212+
if self.{{{name}}}[_key_{{{name}}}]:
213+
_field_dict[_key_{{{name}}}] = self.{{{name}}}[_key_{{{name}}}].to_dict()
214214
_dict['{{{baseName}}}'] = _field_dict
215215
{{/items.isEnumOrRef}}
216216
{{/items.isPrimitiveType}}

modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,18 @@ components:
22532253
enum:
22542254
- fish
22552255
- crab
2256+
MultiArrays:
2257+
type: object
2258+
properties:
2259+
tags:
2260+
type: array
2261+
items:
2262+
$ref: '#/components/schemas/Tag'
2263+
files:
2264+
type: array
2265+
description: Another array of objects in addition to tags (mypy check to not to reuse the same iterator)
2266+
items:
2267+
$ref: '#/components/schemas/File'
22562268
FreeFormObject:
22572269
type: object
22582270
description: A schema consisting only of additional properties

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/models/pet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ def to_dict(self) -> Dict[str, Any]:
9292
# override the default output from pydantic by calling `to_dict()` of each item in tags (list)
9393
_items = []
9494
if self.tags:
95-
for _item in self.tags:
96-
if _item:
97-
_items.append(_item.to_dict())
95+
for _item_tags in self.tags:
96+
if _item_tags:
97+
_items.append(_item_tags.to_dict())
9898
_dict['tags'] = _items
9999
return _dict
100100

samples/client/echo_api/python/openapi_client/models/pet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ def to_dict(self) -> Dict[str, Any]:
9292
# override the default output from pydantic by calling `to_dict()` of each item in tags (list)
9393
_items = []
9494
if self.tags:
95-
for _item in self.tags:
96-
if _item:
97-
_items.append(_item.to_dict())
95+
for _item_tags in self.tags:
96+
if _item_tags:
97+
_items.append(_item_tags.to_dict())
9898
_dict['tags'] = _items
9999
return _dict
100100

samples/openapi3/client/petstore/python-aiohttp/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ docs/Model200Response.md
6666
docs/ModelApiResponse.md
6767
docs/ModelField.md
6868
docs/ModelReturn.md
69+
docs/MultiArrays.md
6970
docs/Name.md
7071
docs/NullableClass.md
7172
docs/NullableProperty.md
@@ -188,6 +189,7 @@ petstore_api/models/model200_response.py
188189
petstore_api/models/model_api_response.py
189190
petstore_api/models/model_field.py
190191
petstore_api/models/model_return.py
192+
petstore_api/models/multi_arrays.py
191193
petstore_api/models/name.py
192194
petstore_api/models/nullable_class.py
193195
petstore_api/models/nullable_property.py

samples/openapi3/client/petstore/python-aiohttp/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ Class | Method | HTTP request | Description
208208
- [ModelApiResponse](docs/ModelApiResponse.md)
209209
- [ModelField](docs/ModelField.md)
210210
- [ModelReturn](docs/ModelReturn.md)
211+
- [MultiArrays](docs/MultiArrays.md)
211212
- [Name](docs/Name.md)
212213
- [NullableClass](docs/NullableClass.md)
213214
- [NullableProperty](docs/NullableProperty.md)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# MultiArrays
2+
3+
4+
## Properties
5+
6+
Name | Type | Description | Notes
7+
------------ | ------------- | ------------- | -------------
8+
**tags** | [**List[Tag]**](Tag.md) | | [optional]
9+
**files** | [**List[File]**](File.md) | Another array of objects in addition to tags (mypy check to not to reuse the same iterator) | [optional]
10+
11+
## Example
12+
13+
```python
14+
from petstore_api.models.multi_arrays import MultiArrays
15+
16+
# TODO update the JSON string below
17+
json = "{}"
18+
# create an instance of MultiArrays from a JSON string
19+
multi_arrays_instance = MultiArrays.from_json(json)
20+
# print the JSON string representation of the object
21+
print(MultiArrays.to_json())
22+
23+
# convert the object into a dict
24+
multi_arrays_dict = multi_arrays_instance.to_dict()
25+
# create an instance of MultiArrays from a dict
26+
multi_arrays_from_dict = MultiArrays.from_dict(multi_arrays_dict)
27+
```
28+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
29+
30+

samples/openapi3/client/petstore/python-aiohttp/petstore_api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
from petstore_api.models.model_api_response import ModelApiResponse
9898
from petstore_api.models.model_field import ModelField
9999
from petstore_api.models.model_return import ModelReturn
100+
from petstore_api.models.multi_arrays import MultiArrays
100101
from petstore_api.models.name import Name
101102
from petstore_api.models.nullable_class import NullableClass
102103
from petstore_api.models.nullable_property import NullableProperty

samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
from petstore_api.models.model_api_response import ModelApiResponse
7373
from petstore_api.models.model_field import ModelField
7474
from petstore_api.models.model_return import ModelReturn
75+
from petstore_api.models.multi_arrays import MultiArrays
7576
from petstore_api.models.name import Name
7677
from petstore_api.models.nullable_class import NullableClass
7778
from petstore_api.models.nullable_property import NullableProperty

samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/array_of_array_of_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ def to_dict(self) -> Dict[str, Any]:
7272
# override the default output from pydantic by calling `to_dict()` of each item in another_property (list of list)
7373
_items = []
7474
if self.another_property:
75-
for _item in self.another_property:
76-
if _item:
75+
for _item_another_property in self.another_property:
76+
if _item_another_property:
7777
_items.append(
78-
[_inner_item.to_dict() for _inner_item in _item if _inner_item is not None]
78+
[_inner_item.to_dict() for _inner_item in _item_another_property if _inner_item is not None]
7979
)
8080
_dict['another_property'] = _items
8181
return _dict

0 commit comments

Comments
 (0)