[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
This commit is contained in:
VelorumS
2024-07-29 10:34:27 +02:00
committed by GitHub
parent 755b2efee4
commit f082a35d2e
58 changed files with 902 additions and 101 deletions

View File

@@ -66,6 +66,7 @@ docs/MapTest.md
docs/MixedPropertiesAndAdditionalPropertiesClass.md
docs/Model200Response.md
docs/ModelReturn.md
docs/MultiArrays.md
docs/Name.md
docs/NullableClass.md
docs/NullableProperty.md
@@ -188,6 +189,7 @@ petstore_api/models/map_test.py
petstore_api/models/mixed_properties_and_additional_properties_class.py
petstore_api/models/model200_response.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

View File

@@ -209,6 +209,7 @@ Class | Method | HTTP request | Description
- [MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md)
- [Model200Response](docs/Model200Response.md)
- [ModelReturn](docs/ModelReturn.md)
- [MultiArrays](docs/MultiArrays.md)
- [Name](docs/Name.md)
- [NullableClass](docs/NullableClass.md)
- [NullableProperty](docs/NullableProperty.md)

View File

@@ -0,0 +1,29 @@
# 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)

View File

@@ -97,6 +97,7 @@ from petstore_api.models.map_test import MapTest
from petstore_api.models.mixed_properties_and_additional_properties_class import MixedPropertiesAndAdditionalPropertiesClass
from petstore_api.models.model200_response import Model200Response
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

View File

@@ -72,6 +72,7 @@ from petstore_api.models.map_test import MapTest
from petstore_api.models.mixed_properties_and_additional_properties_class import MixedPropertiesAndAdditionalPropertiesClass
from petstore_api.models.model200_response import Model200Response
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

View File

@@ -0,0 +1,89 @@
# 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 typing import List, Optional
from pydantic import BaseModel, Field, conlist
from petstore_api.models.file import File
from petstore_api.models.tag import Tag
class MultiArrays(BaseModel):
"""
MultiArrays
"""
tags: Optional[conlist(Tag)] = None
files: Optional[conlist(File)] = Field(default=None, description="Another array of objects in addition to tags (mypy check to not to reuse the same iterator)")
__properties = ["tags", "files"]
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
def to_str(self) -> str:
"""Returns the string representation of the model using alias"""
return pprint.pformat(self.dict(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
return json.dumps(self.to_dict())
@classmethod
def from_json(cls, json_str: str) -> MultiArrays:
"""Create an instance of MultiArrays from a JSON string"""
return cls.from_dict(json.loads(json_str))
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
exclude={
},
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 in self.tags:
if _item:
_items.append(_item.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 in self.files:
if _item:
_items.append(_item.to_dict())
_dict['files'] = _items
return _dict
@classmethod
def from_dict(cls, obj: dict) -> MultiArrays:
"""Create an instance of MultiArrays from a dict"""
if obj is None:
return None
if not isinstance(obj, dict):
return MultiArrays.parse_obj(obj)
_obj = MultiArrays.parse_obj({
"tags": [Tag.from_dict(_item) for _item in obj.get("tags")] if obj.get("tags") is not None else None,
"files": [File.from_dict(_item) for _item in obj.get("files")] if obj.get("files") is not None else None
})
return _obj

View File

@@ -0,0 +1,60 @@
# 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
import unittest
import datetime
from petstore_api.models.multi_arrays import MultiArrays # noqa: E501
class TestMultiArrays(unittest.TestCase):
"""MultiArrays unit test stubs"""
def setUp(self):
pass
def tearDown(self):
pass
def make_instance(self, include_optional) -> MultiArrays:
"""Test MultiArrays
include_option is a boolean, when False only required
params are included, when True both required and
optional params are included """
# uncomment below to create an instance of `MultiArrays`
"""
model = MultiArrays() # noqa: E501
if include_optional:
return MultiArrays(
tags = [
petstore_api.models.tag.Tag(
id = 56,
name = '', )
],
files = [
petstore_api.models.file.File(
source_uri = '', )
]
)
else:
return MultiArrays(
)
"""
def testMultiArrays(self):
"""Test MultiArrays"""
# inst_req_only = self.make_instance(include_optional=False)
# inst_req_and_optional = self.make_instance(include_optional=True)
if __name__ == '__main__':
unittest.main()