Skip to content

Commit

Permalink
python-pydantic-v1: Keep trailing commas for enum validation tuples (O…
Browse files Browse the repository at this point in the history
…penAPITools#19985)

* python-pydantic-v1: Keep trailing commas for tuples when enum has just single member

* Update samples

* Add test for single member enums

* Refactor test name
  • Loading branch information
chiragjn authored Nov 2, 2024
1 parent 66c7b2f commit 67af02c
Show file tree
Hide file tree
Showing 35 changed files with 188 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
{{/required}}
{{#isArray}}
for i in value:
if i not in ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}):
if i not in ({{#allowableValues}}{{#enumVars}}{{{value}}},{{^-last}} {{/-last}}{{/enumVars}}{{/allowableValues}}):
raise ValueError("each list item must be one of ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})")
{{/isArray}}
{{^isArray}}
if value not in ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}):
if value not in ({{#allowableValues}}{{#enumVars}}{{{value}}},{{^-last}} {{/-last}}{{/enumVars}}{{/allowableValues}}):
raise ValueError("must be one of enum values ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})")
{{/isArray}}
return value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
{{/required}}
{{#isArray}}
for i in value:
if i not in ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}):
if i not in ({{#allowableValues}}{{#enumVars}}{{{value}}},{{^-last}} {{/-last}}{{/enumVars}}{{/allowableValues}}):
raise ValueError("each list item must be one of ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})")
{{/isArray}}
{{^isArray}}
if value not in ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}):
if value not in ({{#allowableValues}}{{#enumVars}}{{{value}}},{{^-last}} {{/-last}}{{/enumVars}}{{/allowableValues}}):
raise ValueError("must be one of enum values ({{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}})")
{{/isArray}}
return value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2124,6 +2124,15 @@ components:
- FOOVar
- BarVar
- bazVar
enum_string_single_member:
type: string
enum:
- 'abc'
enum_integer_single_member:
type: integer
format: int32
enum:
- 100
outerEnum:
$ref: '#/components/schemas/OuterEnum'
outerEnumInteger:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def array_string_enum_default_validate_enum(cls, value):
return value

for i in value:
if i not in ('success', 'failure', 'unclassified'):
if i not in ('success', 'failure', 'unclassified',):
raise ValueError("each list item must be one of ('success', 'failure', 'unclassified')")
return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def status_validate_enum(cls, value):
if value is None:
return value

if value not in ('available', 'pending', 'sold'):
if value not in ('available', 'pending', 'sold',):
raise ValueError("must be one of enum values ('available', 'pending', 'sold')")
return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def outcomes_validate_enum(cls, value):
return value

for i in value:
if i not in ('SUCCESS', 'FAILURE', 'SKIPPED'):
if i not in ('SUCCESS', 'FAILURE', 'SKIPPED',):
raise ValueError("each list item must be one of ('SUCCESS', 'FAILURE', 'SKIPPED')")
return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Name | Type | Description | Notes
**enum_number** | **float** | | [optional]
**enum_number_vendor_ext** | **int** | | [optional]
**enum_string_vendor_ext** | **str** | | [optional]
**enum_string_single_member** | **str** | | [optional]
**enum_integer_single_member** | **int** | | [optional]
**outer_enum** | [**OuterEnum**](OuterEnum.md) | | [optional]
**outer_enum_integer** | [**OuterEnumInteger**](OuterEnumInteger.md) | | [optional]
**outer_enum_default_value** | [**OuterEnumDefaultValue**](OuterEnumDefaultValue.md) | | [optional] [default to OuterEnumDefaultValue.PLACED]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ class EnumTest(BaseModel):
enum_number: Optional[float] = None
enum_number_vendor_ext: Optional[StrictInt] = None
enum_string_vendor_ext: Optional[StrictStr] = None
enum_string_single_member: Optional[StrictStr] = None
enum_integer_single_member: Optional[StrictInt] = None
outer_enum: Optional[OuterEnum] = Field(default=None, alias="outerEnum")
outer_enum_integer: Optional[OuterEnumInteger] = Field(default=None, alias="outerEnumInteger")
outer_enum_default_value: Optional[OuterEnumDefaultValue] = Field(default=OuterEnumDefaultValue.PLACED, alias="outerEnumDefaultValue")
outer_enum_integer_default_value: Optional[OuterEnumIntegerDefaultValue] = Field(default=OuterEnumIntegerDefaultValue.NUMBER_0, alias="outerEnumIntegerDefaultValue")
__properties: ClassVar[List[str]] = ["enum_string", "enum_string_required", "enum_integer_default", "enum_integer", "enum_number", "enum_number_vendor_ext", "enum_string_vendor_ext", "outerEnum", "outerEnumInteger", "outerEnumDefaultValue", "outerEnumIntegerDefaultValue"]
__properties: ClassVar[List[str]] = ["enum_string", "enum_string_required", "enum_integer_default", "enum_integer", "enum_number", "enum_number_vendor_ext", "enum_string_vendor_ext", "enum_string_single_member", "enum_integer_single_member", "outerEnum", "outerEnumInteger", "outerEnumDefaultValue", "outerEnumIntegerDefaultValue"]

@field_validator('enum_string')
def enum_string_validate_enum(cls, value):
Expand Down Expand Up @@ -110,6 +112,26 @@ def enum_string_vendor_ext_validate_enum(cls, value):
raise ValueError("must be one of enum values ('FOO', 'Bar', 'baz')")
return value

@field_validator('enum_string_single_member')
def enum_string_single_member_validate_enum(cls, value):
"""Validates the enum"""
if value is None:
return value

if value not in set(['abc']):
raise ValueError("must be one of enum values ('abc')")
return value

@field_validator('enum_integer_single_member')
def enum_integer_single_member_validate_enum(cls, value):
"""Validates the enum"""
if value is None:
return value

if value not in set([100]):
raise ValueError("must be one of enum values (100)")
return value

model_config = ConfigDict(
populate_by_name=True,
validate_assignment=True,
Expand Down Expand Up @@ -173,6 +195,8 @@ def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
"enum_number": obj.get("enum_number"),
"enum_number_vendor_ext": obj.get("enum_number_vendor_ext"),
"enum_string_vendor_ext": obj.get("enum_string_vendor_ext"),
"enum_string_single_member": obj.get("enum_string_single_member"),
"enum_integer_single_member": obj.get("enum_integer_single_member"),
"outerEnum": obj.get("outerEnum"),
"outerEnumInteger": obj.get("outerEnumInteger"),
"outerEnumDefaultValue": obj.get("outerEnumDefaultValue") if obj.get("outerEnumDefaultValue") is not None else OuterEnumDefaultValue.PLACED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Name | Type | Description | Notes
**enum_number** | **float** | | [optional]
**enum_number_vendor_ext** | **int** | | [optional]
**enum_string_vendor_ext** | **str** | | [optional]
**enum_string_single_member** | **str** | | [optional]
**enum_integer_single_member** | **int** | | [optional]
**outer_enum** | [**OuterEnum**](OuterEnum.md) | | [optional]
**outer_enum_integer** | [**OuterEnumInteger**](OuterEnumInteger.md) | | [optional]
**outer_enum_default_value** | [**OuterEnumDefaultValue**](OuterEnumDefaultValue.md) | | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ class Bathing(BaseModel):
@validator('task_name')
def task_name_validate_enum(cls, value):
"""Validates the enum"""
if value not in ('cleaning_deep'):
if value not in ('cleaning_deep',):
raise ValueError("must be one of enum values ('cleaning_deep')")
return value

@validator('function_name')
def function_name_validate_enum(cls, value):
"""Validates the enum"""
if value not in ('care_nourish'):
if value not in ('care_nourish',):
raise ValueError("must be one of enum values ('care_nourish')")
return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def just_symbol_validate_enum(cls, value):
if value is None:
return value

if value not in ('>=', '$'):
if value not in ('>=', '$',):
raise ValueError("must be one of enum values ('>=', '$')")
return value

Expand All @@ -46,7 +46,7 @@ def array_enum_validate_enum(cls, value):
return value

for i in value:
if i not in ('fish', 'crab'):
if i not in ('fish', 'crab',):
raise ValueError("each list item must be one of ('fish', 'crab')")
return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,28 @@ class EnumTest(BaseModel):
enum_number: Optional[float] = None
enum_number_vendor_ext: Optional[StrictInt] = None
enum_string_vendor_ext: Optional[StrictStr] = None
enum_string_single_member: Optional[StrictStr] = None
enum_integer_single_member: Optional[StrictInt] = None
outer_enum: Optional[OuterEnum] = Field(default=None, alias="outerEnum")
outer_enum_integer: Optional[OuterEnumInteger] = Field(default=None, alias="outerEnumInteger")
outer_enum_default_value: Optional[OuterEnumDefaultValue] = Field(default=None, alias="outerEnumDefaultValue")
outer_enum_integer_default_value: Optional[OuterEnumIntegerDefaultValue] = Field(default=None, alias="outerEnumIntegerDefaultValue")
__properties = ["enum_string", "enum_string_required", "enum_integer_default", "enum_integer", "enum_number", "enum_number_vendor_ext", "enum_string_vendor_ext", "outerEnum", "outerEnumInteger", "outerEnumDefaultValue", "outerEnumIntegerDefaultValue"]
__properties = ["enum_string", "enum_string_required", "enum_integer_default", "enum_integer", "enum_number", "enum_number_vendor_ext", "enum_string_vendor_ext", "enum_string_single_member", "enum_integer_single_member", "outerEnum", "outerEnumInteger", "outerEnumDefaultValue", "outerEnumIntegerDefaultValue"]

@validator('enum_string')
def enum_string_validate_enum(cls, value):
"""Validates the enum"""
if value is None:
return value

if value not in ('UPPER', 'lower', ''):
if value not in ('UPPER', 'lower', '',):
raise ValueError("must be one of enum values ('UPPER', 'lower', '')")
return value

@validator('enum_string_required')
def enum_string_required_validate_enum(cls, value):
"""Validates the enum"""
if value not in ('UPPER', 'lower', ''):
if value not in ('UPPER', 'lower', '',):
raise ValueError("must be one of enum values ('UPPER', 'lower', '')")
return value

Expand All @@ -65,7 +67,7 @@ def enum_integer_default_validate_enum(cls, value):
if value is None:
return value

if value not in (1, 5, 14):
if value not in (1, 5, 14,):
raise ValueError("must be one of enum values (1, 5, 14)")
return value

Expand All @@ -75,7 +77,7 @@ def enum_integer_validate_enum(cls, value):
if value is None:
return value

if value not in (1, -1):
if value not in (1, -1,):
raise ValueError("must be one of enum values (1, -1)")
return value

Expand All @@ -85,7 +87,7 @@ def enum_number_validate_enum(cls, value):
if value is None:
return value

if value not in (1.1, -1.2):
if value not in (1.1, -1.2,):
raise ValueError("must be one of enum values (1.1, -1.2)")
return value

Expand All @@ -95,7 +97,7 @@ def enum_number_vendor_ext_validate_enum(cls, value):
if value is None:
return value

if value not in (42, 18, 56):
if value not in (42, 18, 56,):
raise ValueError("must be one of enum values (42, 18, 56)")
return value

Expand All @@ -105,10 +107,30 @@ def enum_string_vendor_ext_validate_enum(cls, value):
if value is None:
return value

if value not in ('FOO', 'Bar', 'baz'):
if value not in ('FOO', 'Bar', 'baz',):
raise ValueError("must be one of enum values ('FOO', 'Bar', 'baz')")
return value

@validator('enum_string_single_member')
def enum_string_single_member_validate_enum(cls, value):
"""Validates the enum"""
if value is None:
return value

if value not in ('abc',):
raise ValueError("must be one of enum values ('abc')")
return value

@validator('enum_integer_single_member')
def enum_integer_single_member_validate_enum(cls, value):
"""Validates the enum"""
if value is None:
return value

if value not in (100,):
raise ValueError("must be one of enum values (100)")
return value

class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
Expand Down Expand Up @@ -157,6 +179,8 @@ def from_dict(cls, obj: dict) -> EnumTest:
"enum_number": obj.get("enum_number"),
"enum_number_vendor_ext": obj.get("enum_number_vendor_ext"),
"enum_string_vendor_ext": obj.get("enum_string_vendor_ext"),
"enum_string_single_member": obj.get("enum_string_single_member"),
"enum_integer_single_member": obj.get("enum_integer_single_member"),
"outer_enum": obj.get("outerEnum"),
"outer_enum_integer": obj.get("outerEnumInteger"),
"outer_enum_default_value": obj.get("outerEnumDefaultValue"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ class Feeding(BaseModel):
@validator('task_name')
def task_name_validate_enum(cls, value):
"""Validates the enum"""
if value not in ('cleaning'):
if value not in ('cleaning',):
raise ValueError("must be one of enum values ('cleaning')")
return value

@validator('function_name')
def function_name_validate_enum(cls, value):
"""Validates the enum"""
if value not in ('care_nourish'):
if value not in ('care_nourish',):
raise ValueError("must be one of enum values ('care_nourish')")
return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def map_of_enum_string_validate_enum(cls, value):
if value is None:
return value

if value not in ('UPPER', 'lower'):
if value not in ('UPPER', 'lower',):
raise ValueError("must be one of enum values ('UPPER', 'lower')")
return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def status_validate_enum(cls, value):
if value is None:
return value

if value not in ('placed', 'approved', 'delivered'):
if value not in ('placed', 'approved', 'delivered',):
raise ValueError("must be one of enum values ('placed', 'approved', 'delivered')")
return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def status_validate_enum(cls, value):
if value is None:
return value

if value not in ('available', 'pending', 'sold'):
if value not in ('available', 'pending', 'sold',):
raise ValueError("must be one of enum values ('available', 'pending', 'sold')")
return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ class PoopCleaning(BaseModel):
@validator('task_name')
def task_name_validate_enum(cls, value):
"""Validates the enum"""
if value not in ('cleaning'):
if value not in ('cleaning',):
raise ValueError("must be one of enum values ('cleaning')")
return value

@validator('function_name')
def function_name_validate_enum(cls, value):
"""Validates the enum"""
if value not in ('care'):
if value not in ('care',):
raise ValueError("must be one of enum values ('care')")
return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def var_schema_validate_enum(cls, value):
if value is None:
return value

if value not in ('available', 'pending', 'sold'):
if value not in ('available', 'pending', 'sold',):
raise ValueError("must be one of enum values ('available', 'pending', 'sold')")
return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_inline_defined_enum_with_default_validate_enum(cls, value):
if value is None:
return value

if value not in ('A', 'B', 'C'):
if value not in ('A', 'B', 'C',):
raise ValueError("must be one of enum values ('A', 'B', 'C')")
return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Name | Type | Description | Notes
**enum_number** | **float** | | [optional]
**enum_number_vendor_ext** | **int** | | [optional]
**enum_string_vendor_ext** | **str** | | [optional]
**enum_string_single_member** | **str** | | [optional]
**enum_integer_single_member** | **int** | | [optional]
**outer_enum** | [**OuterEnum**](OuterEnum.md) | | [optional]
**outer_enum_integer** | [**OuterEnumInteger**](OuterEnumInteger.md) | | [optional]
**outer_enum_default_value** | [**OuterEnumDefaultValue**](OuterEnumDefaultValue.md) | | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ class Bathing(BaseModel):
@validator('task_name')
def task_name_validate_enum(cls, value):
"""Validates the enum"""
if value not in ('cleaning_deep'):
if value not in ('cleaning_deep',):
raise ValueError("must be one of enum values ('cleaning_deep')")
return value

@validator('function_name')
def function_name_validate_enum(cls, value):
"""Validates the enum"""
if value not in ('care_nourish'):
if value not in ('care_nourish',):
raise ValueError("must be one of enum values ('care_nourish')")
return value

Expand Down
Loading

0 comments on commit 67af02c

Please sign in to comment.