Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] fix object arrays giving mypy error "Incompatible types in assignment" in to_dict() #19223

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
# override the default output from pydantic by calling `to_dict()` of each item in {{{name}}} (list of list)
_items = []
if self.{{{name}}}:
for _item in self.{{{name}}}:
if _item:
for _item_{{{name}}} in self.{{{name}}}:
if _item_{{{name}}}:
_items.append(
[_inner_item.to_dict() for _inner_item in _item if _inner_item is not None]
[_inner_item.to_dict() for _inner_item in _item_{{{name}}} if _inner_item is not None]
)
_dict['{{{baseName}}}'] = _items
{{/items.items.isPrimitiveType}}
Expand All @@ -180,9 +180,9 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
# override the default output from pydantic by calling `to_dict()` of each item in {{{name}}} (list)
_items = []
if self.{{{name}}}:
for _item in self.{{{name}}}:
if _item:
_items.append(_item.to_dict())
for _item_{{{name}}} in self.{{{name}}}:
if _item_{{{name}}}:
_items.append(_item_{{{name}}}.to_dict())
_dict['{{{baseName}}}'] = _items
{{/items.isEnumOrRef}}
{{/items.isPrimitiveType}}
Expand All @@ -194,10 +194,10 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
# override the default output from pydantic by calling `to_dict()` of each value in {{{name}}} (dict of array)
_field_dict_of_array = {}
if self.{{{name}}}:
for _key in self.{{{name}}}:
if self.{{{name}}}[_key] is not None:
_field_dict_of_array[_key] = [
_item.to_dict() for _item in self.{{{name}}}[_key]
for _key_{{{name}}} in self.{{{name}}}:
if self.{{{name}}}[_key_{{{name}}}] is not None:
_field_dict_of_array[_key_{{{name}}}] = [
_item.to_dict() for _item in self.{{{name}}}[_key_{{{name}}}]
]
_dict['{{{baseName}}}'] = _field_dict_of_array
{{/items.items.isPrimitiveType}}
Expand All @@ -208,9 +208,9 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
# override the default output from pydantic by calling `to_dict()` of each value in {{{name}}} (dict)
_field_dict = {}
if self.{{{name}}}:
for _key in self.{{{name}}}:
if self.{{{name}}}[_key]:
_field_dict[_key] = self.{{{name}}}[_key].to_dict()
for _key_{{{name}}} in self.{{{name}}}:
if self.{{{name}}}[_key_{{{name}}}]:
_field_dict[_key_{{{name}}}] = self.{{{name}}}[_key_{{{name}}}].to_dict()
_dict['{{{baseName}}}'] = _field_dict
{{/items.isEnumOrRef}}
{{/items.isPrimitiveType}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,18 @@ components:
enum:
- fish
- crab
MultiArrays:
type: object
properties:
tags:
type: array
items:
$ref: '#/components/schemas/Tag'
files:
type: array
description: Another array of objects in addition to tags (mypy check to not to reuse the same iterator)
items:
$ref: '#/components/schemas/File'
FreeFormObject:
type: object
description: A schema consisting only of additional properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def to_dict(self) -> Dict[str, Any]:
# override the default output from pydantic by calling `to_dict()` of each item in tags (list)
_items = []
if self.tags:
for _item in self.tags:
if _item:
_items.append(_item.to_dict())
for _item_tags in self.tags:
if _item_tags:
_items.append(_item_tags.to_dict())
_dict['tags'] = _items
return _dict

Expand Down
6 changes: 3 additions & 3 deletions samples/client/echo_api/python/openapi_client/models/pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def to_dict(self) -> Dict[str, Any]:
# override the default output from pydantic by calling `to_dict()` of each item in tags (list)
_items = []
if self.tags:
for _item in self.tags:
if _item:
_items.append(_item.to_dict())
for _item_tags in self.tags:
if _item_tags:
_items.append(_item_tags.to_dict())
_dict['tags'] = _items
return _dict

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ docs/Model200Response.md
docs/ModelApiResponse.md
docs/ModelField.md
docs/ModelReturn.md
docs/MultiArrays.md
docs/Name.md
docs/NullableClass.md
docs/NullableProperty.md
Expand Down Expand Up @@ -188,6 +189,7 @@ petstore_api/models/model200_response.py
petstore_api/models/model_api_response.py
petstore_api/models/model_field.py
petstore_api/models/model_return.py
petstore_api/models/multi_arrays.py
petstore_api/models/name.py
petstore_api/models/nullable_class.py
petstore_api/models/nullable_property.py
Expand Down
1 change: 1 addition & 0 deletions samples/openapi3/client/petstore/python-aiohttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ Class | Method | HTTP request | Description
- [ModelApiResponse](docs/ModelApiResponse.md)
- [ModelField](docs/ModelField.md)
- [ModelReturn](docs/ModelReturn.md)
- [MultiArrays](docs/MultiArrays.md)
- [Name](docs/Name.md)
- [NullableClass](docs/NullableClass.md)
- [NullableProperty](docs/NullableProperty.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# MultiArrays


## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**tags** | [**List[Tag]**](Tag.md) | | [optional]
**files** | [**List[File]**](File.md) | Another array of objects in addition to tags (mypy check to not to reuse the same iterator) | [optional]

## Example

```python
from petstore_api.models.multi_arrays import MultiArrays

# TODO update the JSON string below
json = "{}"
# create an instance of MultiArrays from a JSON string
multi_arrays_instance = MultiArrays.from_json(json)
# print the JSON string representation of the object
print(MultiArrays.to_json())

# convert the object into a dict
multi_arrays_dict = multi_arrays_instance.to_dict()
# create an instance of MultiArrays from a dict
multi_arrays_from_dict = MultiArrays.from_dict(multi_arrays_dict)
```
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
from petstore_api.models.model_api_response import ModelApiResponse
from petstore_api.models.model_field import ModelField
from petstore_api.models.model_return import ModelReturn
from petstore_api.models.multi_arrays import MultiArrays
from petstore_api.models.name import Name
from petstore_api.models.nullable_class import NullableClass
from petstore_api.models.nullable_property import NullableProperty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
from petstore_api.models.model_api_response import ModelApiResponse
from petstore_api.models.model_field import ModelField
from petstore_api.models.model_return import ModelReturn
from petstore_api.models.multi_arrays import MultiArrays
from petstore_api.models.name import Name
from petstore_api.models.nullable_class import NullableClass
from petstore_api.models.nullable_property import NullableProperty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ def to_dict(self) -> Dict[str, Any]:
# override the default output from pydantic by calling `to_dict()` of each item in another_property (list of list)
_items = []
if self.another_property:
for _item in self.another_property:
if _item:
for _item_another_property in self.another_property:
if _item_another_property:
_items.append(
[_inner_item.to_dict() for _inner_item in _item if _inner_item is not None]
[_inner_item.to_dict() for _inner_item in _item_another_property if _inner_item is not None]
)
_dict['another_property'] = _items
return _dict
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ def to_dict(self) -> Dict[str, Any]:
# override the default output from pydantic by calling `to_dict()` of each item in array_array_of_model (list of list)
_items = []
if self.array_array_of_model:
for _item in self.array_array_of_model:
if _item:
for _item_array_array_of_model in self.array_array_of_model:
if _item_array_array_of_model:
_items.append(
[_inner_item.to_dict() for _inner_item in _item if _inner_item is not None]
[_inner_item.to_dict() for _inner_item in _item_array_array_of_model if _inner_item is not None]
)
_dict['array_array_of_model'] = _items
return _dict
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def to_dict(self) -> Dict[str, Any]:
# override the default output from pydantic by calling `to_dict()` of each item in second_circular_all_of_ref (list)
_items = []
if self.second_circular_all_of_ref:
for _item in self.second_circular_all_of_ref:
if _item:
_items.append(_item.to_dict())
for _item_second_circular_all_of_ref in self.second_circular_all_of_ref:
if _item_second_circular_all_of_ref:
_items.append(_item_second_circular_all_of_ref.to_dict())
_dict['secondCircularAllOfRef'] = _items
return _dict

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ def to_dict(self) -> Dict[str, Any]:
# override the default output from pydantic by calling `to_dict()` of each item in files (list)
_items = []
if self.files:
for _item in self.files:
if _item:
_items.append(_item.to_dict())
for _item_files in self.files:
if _item_files:
_items.append(_item_files.to_dict())
_dict['files'] = _items
return _dict

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def to_dict(self) -> Dict[str, Any]:
# override the default output from pydantic by calling `to_dict()` of each value in some_data (dict)
_field_dict = {}
if self.some_data:
for _key in self.some_data:
if self.some_data[_key]:
_field_dict[_key] = self.some_data[_key].to_dict()
for _key_some_data in self.some_data:
if self.some_data[_key_some_data]:
_field_dict[_key_some_data] = self.some_data[_key_some_data].to_dict()
_dict['some_data'] = _field_dict
return _dict

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ def to_dict(self) -> Dict[str, Any]:
# override the default output from pydantic by calling `to_dict()` of each value in shop_id_to_org_online_lip_map (dict of array)
_field_dict_of_array = {}
if self.shop_id_to_org_online_lip_map:
for _key in self.shop_id_to_org_online_lip_map:
if self.shop_id_to_org_online_lip_map[_key] is not None:
_field_dict_of_array[_key] = [
_item.to_dict() for _item in self.shop_id_to_org_online_lip_map[_key]
for _key_shop_id_to_org_online_lip_map in self.shop_id_to_org_online_lip_map:
if self.shop_id_to_org_online_lip_map[_key_shop_id_to_org_online_lip_map] is not None:
_field_dict_of_array[_key_shop_id_to_org_online_lip_map] = [
_item.to_dict() for _item in self.shop_id_to_org_online_lip_map[_key_shop_id_to_org_online_lip_map]
]
_dict['shopIdToOrgOnlineLipMap'] = _field_dict_of_array
return _dict
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def to_dict(self) -> Dict[str, Any]:
# override the default output from pydantic by calling `to_dict()` of each value in map (dict)
_field_dict = {}
if self.map:
for _key in self.map:
if self.map[_key]:
_field_dict[_key] = self.map[_key].to_dict()
for _key_map in self.map:
if self.map[_key_map]:
_field_dict[_key_map] = self.map[_key_map].to_dict()
_dict['map'] = _field_dict
return _dict

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# coding: utf-8

"""
OpenAPI Petstore

This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\

The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)

Do not edit the class manually.
""" # noqa: E501


from __future__ import annotations
import pprint
import re # noqa: F401
import json

from pydantic import BaseModel, ConfigDict, Field
from typing import Any, ClassVar, Dict, List, Optional
from petstore_api.models.file import File
from petstore_api.models.tag import Tag
from typing import Optional, Set
from typing_extensions import Self

class MultiArrays(BaseModel):
"""
MultiArrays
""" # noqa: E501
tags: Optional[List[Tag]] = None
files: Optional[List[File]] = Field(default=None, description="Another array of objects in addition to tags (mypy check to not to reuse the same iterator)")
__properties: ClassVar[List[str]] = ["tags", "files"]

model_config = ConfigDict(
populate_by_name=True,
validate_assignment=True,
protected_namespaces=(),
)


def to_str(self) -> str:
"""Returns the string representation of the model using alias"""
return pprint.pformat(self.model_dump(by_alias=True))

def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
# TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
return json.dumps(self.to_dict())

@classmethod
def from_json(cls, json_str: str) -> Optional[Self]:
"""Create an instance of MultiArrays from a JSON string"""
return cls.from_dict(json.loads(json_str))

def to_dict(self) -> Dict[str, Any]:
"""Return the dictionary representation of the model using alias.

This has the following differences from calling pydantic's
`self.model_dump(by_alias=True)`:

* `None` is only added to the output dict for nullable fields that
were set at model initialization. Other fields with value `None`
are ignored.
"""
excluded_fields: Set[str] = set([
])

_dict = self.model_dump(
by_alias=True,
exclude=excluded_fields,
exclude_none=True,
)
# override the default output from pydantic by calling `to_dict()` of each item in tags (list)
_items = []
if self.tags:
for _item_tags in self.tags:
if _item_tags:
_items.append(_item_tags.to_dict())
_dict['tags'] = _items
# override the default output from pydantic by calling `to_dict()` of each item in files (list)
_items = []
if self.files:
for _item_files in self.files:
if _item_files:
_items.append(_item_files.to_dict())
_dict['files'] = _items
return _dict

@classmethod
def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
"""Create an instance of MultiArrays from a dict"""
if obj is None:
return None

if not isinstance(obj, dict):
return cls.model_validate(obj)

_obj = cls.model_validate({
"tags": [Tag.from_dict(_item) for _item in obj["tags"]] if obj.get("tags") is not None else None,
"files": [File.from_dict(_item) for _item in obj["files"]] if obj.get("files") is not None else None
})
return _obj


Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def to_dict(self) -> Dict[str, Any]:
# override the default output from pydantic by calling `to_dict()` of each value in optional_dict (dict)
_field_dict = {}
if self.optional_dict:
for _key in self.optional_dict:
if self.optional_dict[_key]:
_field_dict[_key] = self.optional_dict[_key].to_dict()
for _key_optional_dict in self.optional_dict:
if self.optional_dict[_key_optional_dict]:
_field_dict[_key_optional_dict] = self.optional_dict[_key_optional_dict].to_dict()
_dict['optionalDict'] = _field_dict
return _dict

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def to_dict(self) -> Dict[str, Any]:
# override the default output from pydantic by calling `to_dict()` of each value in optional_dict (dict)
_field_dict = {}
if self.optional_dict:
for _key in self.optional_dict:
if self.optional_dict[_key]:
_field_dict[_key] = self.optional_dict[_key].to_dict()
for _key_optional_dict in self.optional_dict:
if self.optional_dict[_key_optional_dict]:
_field_dict[_key_optional_dict] = self.optional_dict[_key_optional_dict].to_dict()
_dict['optionalDict'] = _field_dict
return _dict

Expand Down
Loading
Loading