Skip to content

Commit

Permalink
update samples
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Jan 31, 2024
1 parent fa454c3 commit cac4b5f
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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))

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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))

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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))

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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))

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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))

Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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))

Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit cac4b5f

Please sign in to comment.