[PYTHON] generate code based on pydantic v2 (#16685)

* [python] replace validator with field_validator

* [python] replace parse_obj with model_validate

* [python] replace dict with model_dump

* [python] replace construct with model_construct

* [python] replace __fields_set__ with model_fields_set

* [python] replace __fields_set__ in the test cases with model_fields_set

* [python] replace validate_arguments with validate_call

* [python] replace max_items, min_items with max_length, min_length

* [python] replace Config class with model_config

* [python] replace allow_population_by_field_name with populate_by_name

* [python] remove {{{classname}}}_ONE_OF_SCHEMAS

* [python] update test cases

* [python] update samples

* [python] fix typos in test cases
This commit is contained in:
ふぁ
2023-09-29 17:45:46 +09:00
committed by GitHub
parent 67b129fda9
commit e2f249ba35
177 changed files with 1774 additions and 1611 deletions

View File

@@ -30,14 +30,15 @@ class Bird(BaseModel):
color: Optional[StrictStr] = None
__properties = ["size", "color"]
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
model_config = {
"populate_by_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))
return pprint.pformat(self.model_dump(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
@@ -51,7 +52,7 @@ class Bird(BaseModel):
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
_dict = self.model_dump(by_alias=True,
exclude={
},
exclude_none=True)
@@ -64,9 +65,9 @@ class Bird(BaseModel):
return None
if not isinstance(obj, dict):
return Bird.parse_obj(obj)
return Bird.model_validate(obj)
_obj = Bird.parse_obj({
_obj = Bird.model_validate({
"size": obj.get("size"),
"color": obj.get("color")
})

View File

@@ -30,14 +30,15 @@ class Category(BaseModel):
name: Optional[StrictStr] = None
__properties = ["id", "name"]
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
model_config = {
"populate_by_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))
return pprint.pformat(self.model_dump(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
@@ -51,7 +52,7 @@ class Category(BaseModel):
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
_dict = self.model_dump(by_alias=True,
exclude={
},
exclude_none=True)
@@ -64,9 +65,9 @@ class Category(BaseModel):
return None
if not isinstance(obj, dict):
return Category.parse_obj(obj)
return Category.model_validate(obj)
_obj = Category.parse_obj({
_obj = Category.model_validate({
"id": obj.get("id"),
"name": obj.get("name")
})

View File

@@ -33,14 +33,15 @@ class DataQuery(Query):
var_date: Optional[datetime] = Field(default=None, description="A date", alias="date")
__properties = ["id", "outcomes", "suffix", "text", "date"]
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
model_config = {
"populate_by_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))
return pprint.pformat(self.model_dump(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
@@ -54,7 +55,7 @@ class DataQuery(Query):
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
_dict = self.model_dump(by_alias=True,
exclude={
},
exclude_none=True)
@@ -67,9 +68,9 @@ class DataQuery(Query):
return None
if not isinstance(obj, dict):
return DataQuery.parse_obj(obj)
return DataQuery.model_validate(obj)
_obj = DataQuery.parse_obj({
_obj = DataQuery.model_validate({
"id": obj.get("id"),
"outcomes": obj.get("outcomes"),
"suffix": obj.get("suffix"),

View File

@@ -20,7 +20,7 @@ import json
from typing import List, Optional
from pydantic import BaseModel, StrictInt, StrictStr, validator
from pydantic import BaseModel, StrictInt, StrictStr, field_validator
from openapi_client.models.string_enum_ref import StringEnumRef
class DefaultValue(BaseModel):
@@ -37,7 +37,7 @@ class DefaultValue(BaseModel):
string_nullable: Optional[StrictStr] = None
__properties = ["array_string_enum_ref_default", "array_string_enum_default", "array_string_default", "array_integer_default", "array_string", "array_string_nullable", "array_string_extension_nullable", "string_nullable"]
@validator('array_string_enum_default')
@field_validator('array_string_enum_default')
def array_string_enum_default_validate_enum(cls, value):
"""Validates the enum"""
if value is None:
@@ -48,14 +48,15 @@ class DefaultValue(BaseModel):
raise ValueError("each list item must be one of ('success', 'failure', 'unclassified')")
return value
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
model_config = {
"populate_by_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))
return pprint.pformat(self.model_dump(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
@@ -69,23 +70,23 @@ class DefaultValue(BaseModel):
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
_dict = self.model_dump(by_alias=True,
exclude={
},
exclude_none=True)
# set to None if array_string_nullable (nullable) is None
# and __fields_set__ contains the field
if self.array_string_nullable is None and "array_string_nullable" in self.__fields_set__:
# and model_fields_set contains the field
if self.array_string_nullable is None and "array_string_nullable" in self.model_fields_set:
_dict['array_string_nullable'] = None
# set to None if array_string_extension_nullable (nullable) is None
# and __fields_set__ contains the field
if self.array_string_extension_nullable is None and "array_string_extension_nullable" in self.__fields_set__:
# and model_fields_set contains the field
if self.array_string_extension_nullable is None and "array_string_extension_nullable" in self.model_fields_set:
_dict['array_string_extension_nullable'] = None
# set to None if string_nullable (nullable) is None
# and __fields_set__ contains the field
if self.string_nullable is None and "string_nullable" in self.__fields_set__:
# and model_fields_set contains the field
if self.string_nullable is None and "string_nullable" in self.model_fields_set:
_dict['string_nullable'] = None
return _dict
@@ -97,9 +98,9 @@ class DefaultValue(BaseModel):
return None
if not isinstance(obj, dict):
return DefaultValue.parse_obj(obj)
return DefaultValue.model_validate(obj)
_obj = DefaultValue.parse_obj({
_obj = DefaultValue.model_validate({
"array_string_enum_ref_default": obj.get("array_string_enum_ref_default"),
"array_string_enum_default": obj.get("array_string_enum_default"),
"array_string_default": obj.get("array_string_default"),

View File

@@ -32,14 +32,15 @@ class NumberPropertiesOnly(BaseModel):
double: Optional[Union[Annotated[float, Field(le=50.2, strict=True, ge=0.8)], Annotated[int, Field(le=50, strict=True, ge=1)]]] = None
__properties = ["number", "double"]
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
model_config = {
"populate_by_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))
return pprint.pformat(self.model_dump(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
@@ -53,7 +54,7 @@ class NumberPropertiesOnly(BaseModel):
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
_dict = self.model_dump(by_alias=True,
exclude={
},
exclude_none=True)
@@ -66,9 +67,9 @@ class NumberPropertiesOnly(BaseModel):
return None
if not isinstance(obj, dict):
return NumberPropertiesOnly.parse_obj(obj)
return NumberPropertiesOnly.model_validate(obj)
_obj = NumberPropertiesOnly.parse_obj({
_obj = NumberPropertiesOnly.model_validate({
"number": obj.get("number"),
"double": obj.get("double")
})

View File

@@ -20,7 +20,7 @@ import json
from typing import List, Optional
from pydantic import BaseModel, StrictInt, StrictStr, validator
from pydantic import BaseModel, StrictInt, StrictStr, field_validator
from pydantic import Field
from openapi_client.models.category import Category
from openapi_client.models.tag import Tag
@@ -37,7 +37,7 @@ class Pet(BaseModel):
status: Optional[StrictStr] = Field(default=None, description="pet status in the store")
__properties = ["id", "name", "category", "photoUrls", "tags", "status"]
@validator('status')
@field_validator('status')
def status_validate_enum(cls, value):
"""Validates the enum"""
if value is None:
@@ -47,14 +47,15 @@ class Pet(BaseModel):
raise ValueError("must be one of enum values ('available', 'pending', 'sold')")
return value
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
model_config = {
"populate_by_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))
return pprint.pformat(self.model_dump(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
@@ -68,7 +69,7 @@ class Pet(BaseModel):
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
_dict = self.model_dump(by_alias=True,
exclude={
},
exclude_none=True)
@@ -91,9 +92,9 @@ class Pet(BaseModel):
return None
if not isinstance(obj, dict):
return Pet.parse_obj(obj)
return Pet.model_validate(obj)
_obj = Pet.parse_obj({
_obj = Pet.model_validate({
"id": obj.get("id"),
"name": obj.get("name"),
"category": Category.from_dict(obj.get("category")) if obj.get("category") is not None else None,

View File

@@ -20,7 +20,7 @@ import json
from typing import List, Optional
from pydantic import BaseModel, StrictInt, StrictStr, validator
from pydantic import BaseModel, StrictInt, StrictStr, field_validator
from pydantic import Field
class Query(BaseModel):
@@ -31,7 +31,7 @@ class Query(BaseModel):
outcomes: Optional[List[StrictStr]] = None
__properties = ["id", "outcomes"]
@validator('outcomes')
@field_validator('outcomes')
def outcomes_validate_enum(cls, value):
"""Validates the enum"""
if value is None:
@@ -42,14 +42,15 @@ class Query(BaseModel):
raise ValueError("each list item must be one of ('SUCCESS', 'FAILURE', 'SKIPPED')")
return value
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
model_config = {
"populate_by_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))
return pprint.pformat(self.model_dump(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
@@ -63,7 +64,7 @@ class Query(BaseModel):
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
_dict = self.model_dump(by_alias=True,
exclude={
},
exclude_none=True)

View File

@@ -30,14 +30,15 @@ class Tag(BaseModel):
name: Optional[StrictStr] = None
__properties = ["id", "name"]
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
model_config = {
"populate_by_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))
return pprint.pformat(self.model_dump(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
@@ -51,7 +52,7 @@ class Tag(BaseModel):
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
_dict = self.model_dump(by_alias=True,
exclude={
},
exclude_none=True)
@@ -64,9 +65,9 @@ class Tag(BaseModel):
return None
if not isinstance(obj, dict):
return Tag.parse_obj(obj)
return Tag.model_validate(obj)
_obj = Tag.parse_obj({
_obj = Tag.model_validate({
"id": obj.get("id"),
"name": obj.get("name")
})

View File

@@ -32,14 +32,15 @@ class TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter(BaseMod
name: Optional[StrictStr] = None
__properties = ["size", "color", "id", "name"]
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
model_config = {
"populate_by_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))
return pprint.pformat(self.model_dump(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
@@ -53,7 +54,7 @@ class TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter(BaseMod
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
_dict = self.model_dump(by_alias=True,
exclude={
},
exclude_none=True)
@@ -66,9 +67,9 @@ class TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter(BaseMod
return None
if not isinstance(obj, dict):
return TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter.parse_obj(obj)
return TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter.model_validate(obj)
_obj = TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter.parse_obj({
_obj = TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter.model_validate({
"size": obj.get("size"),
"color": obj.get("color"),
"id": obj.get("id"),

View File

@@ -29,14 +29,15 @@ class TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter(BaseModel):
values: Optional[List[StrictStr]] = None
__properties = ["values"]
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
model_config = {
"populate_by_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))
return pprint.pformat(self.model_dump(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
@@ -50,7 +51,7 @@ class TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter(BaseModel):
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
_dict = self.model_dump(by_alias=True,
exclude={
},
exclude_none=True)
@@ -63,9 +64,9 @@ class TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter(BaseModel):
return None
if not isinstance(obj, dict):
return TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter.parse_obj(obj)
return TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter.model_validate(obj)
_obj = TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter.parse_obj({
_obj = TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter.model_validate({
"values": obj.get("values")
})
return _obj