From cac4b5fd3a8b4ffccf2e99f727f55c7b886cfcd3 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 31 Jan 2024 11:11:33 +0800 Subject: [PATCH] update samples --- .../petstore_api/models/bathing.py | 19 +++++++------- .../petstore_api/models/feeding.py | 19 +++++++------- .../petstore_api/models/poop_cleaning.py | 19 +++++++------- .../petstore_api/models/task.py | 21 ++++++++-------- .../petstore_api/models/task_activity.py | 25 ++++++------------- .../python/petstore_api/models/bathing.py | 21 ++++++++-------- .../python/petstore_api/models/feeding.py | 21 ++++++++-------- .../petstore_api/models/poop_cleaning.py | 21 ++++++++-------- .../python/petstore_api/models/task.py | 23 ++++++++--------- .../petstore_api/models/task_activity.py | 25 ++++++------------- 10 files changed, 94 insertions(+), 120 deletions(-) diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/bathing.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/bathing.py index afe4c102a1c0..efc485131032 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/bathing.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/bathing.py @@ -17,13 +17,10 @@ import re # noqa: F401 import json - -from typing import Any, ClassVar, Dict, List from pydantic import BaseModel, StrictStr, field_validator -try: - from typing import Self -except ImportError: - from typing_extensions import Self +from typing import Any, ClassVar, Dict, List +from typing import Optional, Set +from typing_extensions import Self class Bathing(BaseModel): """ @@ -65,7 +62,7 @@ def to_json(self) -> str: return json.dumps(self.to_dict()) @classmethod - def from_json(cls, json_str: str) -> Self: + def from_json(cls, json_str: str) -> Optional[Self]: """Create an instance of Bathing from a JSON string""" return cls.from_dict(json.loads(json_str)) @@ -79,16 +76,18 @@ def to_dict(self) -> Dict[str, Any]: 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={ - }, + exclude=excluded_fields, exclude_none=True, ) return _dict @classmethod - def from_dict(cls, obj: Dict) -> Self: + def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]: """Create an instance of Bathing from a dict""" if obj is None: return None diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/feeding.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/feeding.py index da4a47a7c47f..4c5333d3b161 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/feeding.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/feeding.py @@ -17,13 +17,10 @@ import re # noqa: F401 import json - -from typing import Any, ClassVar, Dict, List from pydantic import BaseModel, StrictStr, field_validator -try: - from typing import Self -except ImportError: - from typing_extensions import Self +from typing import Any, ClassVar, Dict, List +from typing import Optional, Set +from typing_extensions import Self class Feeding(BaseModel): """ @@ -65,7 +62,7 @@ def to_json(self) -> str: return json.dumps(self.to_dict()) @classmethod - def from_json(cls, json_str: str) -> Self: + def from_json(cls, json_str: str) -> Optional[Self]: """Create an instance of Feeding from a JSON string""" return cls.from_dict(json.loads(json_str)) @@ -79,16 +76,18 @@ def to_dict(self) -> Dict[str, Any]: 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={ - }, + exclude=excluded_fields, exclude_none=True, ) return _dict @classmethod - def from_dict(cls, obj: Dict) -> Self: + def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]: """Create an instance of Feeding from a dict""" if obj is None: return None diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/poop_cleaning.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/poop_cleaning.py index 417d2b3a2cc1..789ef627f0ed 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/poop_cleaning.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/poop_cleaning.py @@ -17,13 +17,10 @@ import re # noqa: F401 import json - -from typing import Any, ClassVar, Dict, List from pydantic import BaseModel, StrictStr, field_validator -try: - from typing import Self -except ImportError: - from typing_extensions import Self +from typing import Any, ClassVar, Dict, List +from typing import Optional, Set +from typing_extensions import Self class PoopCleaning(BaseModel): """ @@ -65,7 +62,7 @@ def to_json(self) -> str: return json.dumps(self.to_dict()) @classmethod - def from_json(cls, json_str: str) -> Self: + def from_json(cls, json_str: str) -> Optional[Self]: """Create an instance of PoopCleaning from a JSON string""" return cls.from_dict(json.loads(json_str)) @@ -79,16 +76,18 @@ def to_dict(self) -> Dict[str, Any]: 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={ - }, + exclude=excluded_fields, exclude_none=True, ) return _dict @classmethod - def from_dict(cls, obj: Dict) -> Self: + def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]: """Create an instance of PoopCleaning from a dict""" if obj is None: return None diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/task.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/task.py index e4a159aa91e9..7fb9e638729f 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/task.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/task.py @@ -17,14 +17,11 @@ import re # noqa: F401 import json - -from typing import Any, ClassVar, Dict, List from pydantic import BaseModel, StrictStr +from typing import Any, ClassVar, Dict, List from petstore_api.models.task_activity import TaskActivity -try: - from typing import Self -except ImportError: - from typing_extensions import Self +from typing import Optional, Set +from typing_extensions import Self class Task(BaseModel): """ @@ -51,7 +48,7 @@ def to_json(self) -> str: return json.dumps(self.to_dict()) @classmethod - def from_json(cls, json_str: str) -> Self: + def from_json(cls, json_str: str) -> Optional[Self]: """Create an instance of Task from a JSON string""" return cls.from_dict(json.loads(json_str)) @@ -65,10 +62,12 @@ def to_dict(self) -> Dict[str, Any]: 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={ - }, + exclude=excluded_fields, exclude_none=True, ) # override the default output from pydantic by calling `to_dict()` of activity @@ -77,7 +76,7 @@ def to_dict(self) -> Dict[str, Any]: return _dict @classmethod - def from_dict(cls, obj: Dict) -> Self: + def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]: """Create an instance of Task from a dict""" if obj is None: return None @@ -87,7 +86,7 @@ def from_dict(cls, obj: Dict) -> Self: _obj = cls.model_validate({ "id": obj.get("id"), - "activity": TaskActivity.from_dict(obj.get("activity")) if obj.get("activity") is not None else None + "activity": TaskActivity.from_dict(obj["activity"]) if obj.get("activity") is not None else None }) return _obj diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/task_activity.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/task_activity.py index 95665d6d4c2e..b76c2604597c 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/task_activity.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/task_activity.py @@ -13,23 +13,16 @@ from __future__ import annotations -from inspect import getfullargspec import json import pprint -import re # noqa: F401 - -from typing import Any, List, Optional from pydantic import BaseModel, Field, StrictStr, ValidationError, field_validator +from typing import Any, List, Optional from petstore_api.models.bathing import Bathing from petstore_api.models.feeding import Feeding from petstore_api.models.poop_cleaning import PoopCleaning -from typing import Union, Any, List, TYPE_CHECKING, Optional, Dict -from typing_extensions import Literal from pydantic import StrictStr, Field -try: - from typing import Self -except ImportError: - from typing_extensions import Self +from typing import Union, List, Optional, Dict +from typing_extensions import Literal, Self TASKACTIVITY_ONE_OF_SCHEMAS = ["Bathing", "Feeding", "PoopCleaning"] @@ -44,7 +37,7 @@ class TaskActivity(BaseModel): # data type: Bathing oneof_schema_3_validator: Optional[Bathing] = None actual_instance: Optional[Union[Bathing, Feeding, PoopCleaning]] = None - one_of_schemas: List[str] = Literal["Bathing", "Feeding", "PoopCleaning"] + one_of_schemas: List[str] = Field(default=Literal["Bathing", "Feeding", "PoopCleaning"]) model_config = { "validate_assignment": True, @@ -92,7 +85,7 @@ def actual_instance_must_validate_oneof(cls, v): return v @classmethod - def from_dict(cls, obj: dict) -> Self: + def from_dict(cls, obj: Union[str, Dict[str, Any]]) -> Self: return cls.from_json(json.dumps(obj)) @classmethod @@ -135,19 +128,17 @@ def to_json(self) -> str: if self.actual_instance is None: return "null" - to_json = getattr(self.actual_instance, "to_json", None) - if callable(to_json): + if hasattr(self.actual_instance, "to_json") and callable(self.actual_instance.to_json): return self.actual_instance.to_json() else: return json.dumps(self.actual_instance) - def to_dict(self) -> Dict: + def to_dict(self) -> Optional[Union[Dict[str, Any], Bathing, Feeding, PoopCleaning]]: """Returns the dict representation of the actual instance""" if self.actual_instance is None: return None - to_dict = getattr(self.actual_instance, "to_dict", None) - if callable(to_dict): + if hasattr(self.actual_instance, "to_dict") and callable(self.actual_instance.to_dict): return self.actual_instance.to_dict() else: # primitive type diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/bathing.py b/samples/openapi3/client/petstore/python/petstore_api/models/bathing.py index 2ceda3da159f..7ea7084ae9db 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/models/bathing.py +++ b/samples/openapi3/client/petstore/python/petstore_api/models/bathing.py @@ -17,13 +17,10 @@ import re # noqa: F401 import json - -from typing import Any, ClassVar, Dict, List from pydantic import BaseModel, StrictStr, field_validator -try: - from typing import Self -except ImportError: - from typing_extensions import Self +from typing import Any, ClassVar, Dict, List +from typing import Optional, Set +from typing_extensions import Self class Bathing(BaseModel): """ @@ -66,7 +63,7 @@ def to_json(self) -> str: return json.dumps(self.to_dict()) @classmethod - def from_json(cls, json_str: str) -> Self: + def from_json(cls, json_str: str) -> Optional[Self]: """Create an instance of Bathing from a JSON string""" return cls.from_dict(json.loads(json_str)) @@ -81,11 +78,13 @@ def to_dict(self) -> Dict[str, Any]: are ignored. * Fields in `self.additional_properties` are added to the output dict. """ + excluded_fields: Set[str] = set([ + "additional_properties", + ]) + _dict = self.model_dump( by_alias=True, - exclude={ - "additional_properties", - }, + exclude=excluded_fields, exclude_none=True, ) # puts key-value pairs in additional_properties in the top level @@ -96,7 +95,7 @@ def to_dict(self) -> Dict[str, Any]: return _dict @classmethod - def from_dict(cls, obj: Dict) -> Self: + def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]: """Create an instance of Bathing from a dict""" if obj is None: return None diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/feeding.py b/samples/openapi3/client/petstore/python/petstore_api/models/feeding.py index 171e1ec16ef8..e09b83a21b09 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/models/feeding.py +++ b/samples/openapi3/client/petstore/python/petstore_api/models/feeding.py @@ -17,13 +17,10 @@ import re # noqa: F401 import json - -from typing import Any, ClassVar, Dict, List from pydantic import BaseModel, StrictStr, field_validator -try: - from typing import Self -except ImportError: - from typing_extensions import Self +from typing import Any, ClassVar, Dict, List +from typing import Optional, Set +from typing_extensions import Self class Feeding(BaseModel): """ @@ -66,7 +63,7 @@ def to_json(self) -> str: return json.dumps(self.to_dict()) @classmethod - def from_json(cls, json_str: str) -> Self: + def from_json(cls, json_str: str) -> Optional[Self]: """Create an instance of Feeding from a JSON string""" return cls.from_dict(json.loads(json_str)) @@ -81,11 +78,13 @@ def to_dict(self) -> Dict[str, Any]: are ignored. * Fields in `self.additional_properties` are added to the output dict. """ + excluded_fields: Set[str] = set([ + "additional_properties", + ]) + _dict = self.model_dump( by_alias=True, - exclude={ - "additional_properties", - }, + exclude=excluded_fields, exclude_none=True, ) # puts key-value pairs in additional_properties in the top level @@ -96,7 +95,7 @@ def to_dict(self) -> Dict[str, Any]: return _dict @classmethod - def from_dict(cls, obj: Dict) -> Self: + def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]: """Create an instance of Feeding from a dict""" if obj is None: return None diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/poop_cleaning.py b/samples/openapi3/client/petstore/python/petstore_api/models/poop_cleaning.py index 6b53f134398e..7d881efcd21b 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/models/poop_cleaning.py +++ b/samples/openapi3/client/petstore/python/petstore_api/models/poop_cleaning.py @@ -17,13 +17,10 @@ import re # noqa: F401 import json - -from typing import Any, ClassVar, Dict, List from pydantic import BaseModel, StrictStr, field_validator -try: - from typing import Self -except ImportError: - from typing_extensions import Self +from typing import Any, ClassVar, Dict, List +from typing import Optional, Set +from typing_extensions import Self class PoopCleaning(BaseModel): """ @@ -66,7 +63,7 @@ def to_json(self) -> str: return json.dumps(self.to_dict()) @classmethod - def from_json(cls, json_str: str) -> Self: + def from_json(cls, json_str: str) -> Optional[Self]: """Create an instance of PoopCleaning from a JSON string""" return cls.from_dict(json.loads(json_str)) @@ -81,11 +78,13 @@ def to_dict(self) -> Dict[str, Any]: are ignored. * Fields in `self.additional_properties` are added to the output dict. """ + excluded_fields: Set[str] = set([ + "additional_properties", + ]) + _dict = self.model_dump( by_alias=True, - exclude={ - "additional_properties", - }, + exclude=excluded_fields, exclude_none=True, ) # puts key-value pairs in additional_properties in the top level @@ -96,7 +95,7 @@ def to_dict(self) -> Dict[str, Any]: return _dict @classmethod - def from_dict(cls, obj: Dict) -> Self: + def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]: """Create an instance of PoopCleaning from a dict""" if obj is None: return None diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/task.py b/samples/openapi3/client/petstore/python/petstore_api/models/task.py index 8bb555f0d03e..2a58ebaed92e 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/models/task.py +++ b/samples/openapi3/client/petstore/python/petstore_api/models/task.py @@ -17,14 +17,11 @@ import re # noqa: F401 import json - -from typing import Any, ClassVar, Dict, List from pydantic import BaseModel, StrictStr +from typing import Any, ClassVar, Dict, List from petstore_api.models.task_activity import TaskActivity -try: - from typing import Self -except ImportError: - from typing_extensions import Self +from typing import Optional, Set +from typing_extensions import Self class Task(BaseModel): """ @@ -52,7 +49,7 @@ def to_json(self) -> str: return json.dumps(self.to_dict()) @classmethod - def from_json(cls, json_str: str) -> Self: + def from_json(cls, json_str: str) -> Optional[Self]: """Create an instance of Task from a JSON string""" return cls.from_dict(json.loads(json_str)) @@ -67,11 +64,13 @@ def to_dict(self) -> Dict[str, Any]: are ignored. * Fields in `self.additional_properties` are added to the output dict. """ + excluded_fields: Set[str] = set([ + "additional_properties", + ]) + _dict = self.model_dump( by_alias=True, - exclude={ - "additional_properties", - }, + exclude=excluded_fields, exclude_none=True, ) # override the default output from pydantic by calling `to_dict()` of activity @@ -85,7 +84,7 @@ def to_dict(self) -> Dict[str, Any]: return _dict @classmethod - def from_dict(cls, obj: Dict) -> Self: + def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]: """Create an instance of Task from a dict""" if obj is None: return None @@ -95,7 +94,7 @@ def from_dict(cls, obj: Dict) -> Self: _obj = cls.model_validate({ "id": obj.get("id"), - "activity": TaskActivity.from_dict(obj.get("activity")) if obj.get("activity") is not None else None + "activity": TaskActivity.from_dict(obj["activity"]) if obj.get("activity") is not None else None }) # store additional fields in additional_properties for _key in obj.keys(): diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/task_activity.py b/samples/openapi3/client/petstore/python/petstore_api/models/task_activity.py index 95665d6d4c2e..b76c2604597c 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/models/task_activity.py +++ b/samples/openapi3/client/petstore/python/petstore_api/models/task_activity.py @@ -13,23 +13,16 @@ from __future__ import annotations -from inspect import getfullargspec import json import pprint -import re # noqa: F401 - -from typing import Any, List, Optional from pydantic import BaseModel, Field, StrictStr, ValidationError, field_validator +from typing import Any, List, Optional from petstore_api.models.bathing import Bathing from petstore_api.models.feeding import Feeding from petstore_api.models.poop_cleaning import PoopCleaning -from typing import Union, Any, List, TYPE_CHECKING, Optional, Dict -from typing_extensions import Literal from pydantic import StrictStr, Field -try: - from typing import Self -except ImportError: - from typing_extensions import Self +from typing import Union, List, Optional, Dict +from typing_extensions import Literal, Self TASKACTIVITY_ONE_OF_SCHEMAS = ["Bathing", "Feeding", "PoopCleaning"] @@ -44,7 +37,7 @@ class TaskActivity(BaseModel): # data type: Bathing oneof_schema_3_validator: Optional[Bathing] = None actual_instance: Optional[Union[Bathing, Feeding, PoopCleaning]] = None - one_of_schemas: List[str] = Literal["Bathing", "Feeding", "PoopCleaning"] + one_of_schemas: List[str] = Field(default=Literal["Bathing", "Feeding", "PoopCleaning"]) model_config = { "validate_assignment": True, @@ -92,7 +85,7 @@ def actual_instance_must_validate_oneof(cls, v): return v @classmethod - def from_dict(cls, obj: dict) -> Self: + def from_dict(cls, obj: Union[str, Dict[str, Any]]) -> Self: return cls.from_json(json.dumps(obj)) @classmethod @@ -135,19 +128,17 @@ def to_json(self) -> str: if self.actual_instance is None: return "null" - to_json = getattr(self.actual_instance, "to_json", None) - if callable(to_json): + if hasattr(self.actual_instance, "to_json") and callable(self.actual_instance.to_json): return self.actual_instance.to_json() else: return json.dumps(self.actual_instance) - def to_dict(self) -> Dict: + def to_dict(self) -> Optional[Union[Dict[str, Any], Bathing, Feeding, PoopCleaning]]: """Returns the dict representation of the actual instance""" if self.actual_instance is None: return None - to_dict = getattr(self.actual_instance, "to_dict", None) - if callable(to_dict): + if hasattr(self.actual_instance, "to_dict") and callable(self.actual_instance.to_dict): return self.actual_instance.to_dict() else: # primitive type