diff --git a/core/data/tests/anonymous_struct_with_rename/output.py b/core/data/tests/anonymous_struct_with_rename/output.py index 51d381f3..b701b65a 100644 --- a/core/data/tests/anonymous_struct_with_rename/output.py +++ b/core/data/tests/anonymous_struct_with_rename/output.py @@ -3,7 +3,6 @@ """ from __future__ import annotations -from enum import Enum from pydantic import BaseModel, ConfigDict, Field from typing import List, Literal, Union @@ -37,11 +36,6 @@ class AnonymousStructWithRenameKebabCaseInner(BaseModel): something_else: bool = Field(alias="something-else") -class AnonymousStructWithRenameTypes(str, Enum): - LIST = "list" - LONG_FIELD_NAMES = "longFieldNames" - KEBAB_CASE = "kebabCase" - class AnonymousStructWithRenameList(BaseModel): AnonymousStructWithRenameTypes: Literal["list"] = "list" content: AnonymousStructWithRenameListInner diff --git a/core/data/tests/can_apply_prefix_correctly/output.py b/core/data/tests/can_apply_prefix_correctly/output.py index d850291f..1bf22790 100644 --- a/core/data/tests/can_apply_prefix_correctly/output.py +++ b/core/data/tests/can_apply_prefix_correctly/output.py @@ -3,7 +3,6 @@ """ from __future__ import annotations -from enum import Enum from pydantic import BaseModel from typing import Dict, List, Literal, Union @@ -12,14 +11,6 @@ class ItemDetailsFieldValue(BaseModel): hello: str -class AdvancedColorsTypes(str, Enum): - STRING = "String" - NUMBER = "Number" - NUMBER_ARRAY = "NumberArray" - REALLY_COOL_TYPE = "ReallyCoolType" - ARRAY_REALLY_COOL_TYPE = "ArrayReallyCoolType" - DICTIONARY_REALLY_COOL_TYPE = "DictionaryReallyCoolType" - class AdvancedColorsString(BaseModel): AdvancedColorsTypes: Literal["String"] = "String" c: str diff --git a/core/data/tests/can_generate_algebraic_enum/output.py b/core/data/tests/can_generate_algebraic_enum/output.py index 8b128828..37f4db6d 100644 --- a/core/data/tests/can_generate_algebraic_enum/output.py +++ b/core/data/tests/can_generate_algebraic_enum/output.py @@ -3,7 +3,6 @@ """ from __future__ import annotations -from enum import Enum from pydantic import BaseModel from typing import List, Literal, Union @@ -14,13 +13,9 @@ class ItemDetailsFieldValue(BaseModel): """ pass -class AdvancedColorsTypes(str, Enum): - STRING = "String" - NUMBER = "Number" - UNSIGNED_NUMBER = "UnsignedNumber" - NUMBER_ARRAY = "NumberArray" - REALLY_COOL_TYPE = "ReallyCoolType" - +""" +This is a case comment +""" class AdvancedColorsString(BaseModel): AdvancedColorsTypes: Literal["String"] = "String" content: str @@ -37,17 +32,20 @@ class AdvancedColorsNumberArray(BaseModel): AdvancedColorsTypes: Literal["NumberArray"] = "NumberArray" content: List[int] +""" +Comment on the last element +""" class AdvancedColorsReallyCoolType(BaseModel): AdvancedColorsTypes: Literal["ReallyCoolType"] = "ReallyCoolType" content: ItemDetailsFieldValue +""" +Enum comment +""" AdvancedColors = Union[AdvancedColorsString, AdvancedColorsNumber, AdvancedColorsUnsignedNumber, AdvancedColorsNumberArray, AdvancedColorsReallyCoolType] -class AdvancedColors2Types(str, Enum): - STRING = "string" - NUMBER = "number" - NUMBER_ARRAY = "number-array" - REALLY_COOL_TYPE = "really-cool-type" - +""" +This is a case comment +""" class AdvancedColors2String(BaseModel): AdvancedColors2Types: Literal["string"] = "string" content: str @@ -60,6 +58,9 @@ class AdvancedColors2NumberArray(BaseModel): AdvancedColors2Types: Literal["number-array"] = "number-array" content: List[int] +""" +Comment on the last element +""" class AdvancedColors2ReallyCoolType(BaseModel): AdvancedColors2Types: Literal["really-cool-type"] = "really-cool-type" content: ItemDetailsFieldValue diff --git a/core/data/tests/can_generate_algebraic_enum_with_skipped_variants/output.py b/core/data/tests/can_generate_algebraic_enum_with_skipped_variants/output.py index 82757ee0..ba4c3cc7 100644 --- a/core/data/tests/can_generate_algebraic_enum_with_skipped_variants/output.py +++ b/core/data/tests/can_generate_algebraic_enum_with_skipped_variants/output.py @@ -3,15 +3,10 @@ """ from __future__ import annotations -from enum import Enum from pydantic import BaseModel from typing import Literal, Union -class SomeEnumTypes(str, Enum): - A = "A" - C = "C" - class SomeEnumA(BaseModel): SomeEnumTypes: Literal["A"] = "A" diff --git a/core/data/tests/can_generate_anonymous_struct_with_skipped_fields/output.py b/core/data/tests/can_generate_anonymous_struct_with_skipped_fields/output.py index 02d86787..0ea777c9 100644 --- a/core/data/tests/can_generate_anonymous_struct_with_skipped_fields/output.py +++ b/core/data/tests/can_generate_anonymous_struct_with_skipped_fields/output.py @@ -3,7 +3,6 @@ """ from __future__ import annotations -from enum import Enum from pydantic import BaseModel from typing import Literal, Union @@ -28,16 +27,21 @@ class AutofilledBySomethingElseInner(BaseModel): """ -class AutofilledByTypes(str, Enum): - US = "Us" - SOMETHING_ELSE = "SomethingElse" - +""" +This field was autofilled by us +""" class AutofilledByUs(BaseModel): AutofilledByTypes: Literal["Us"] = "Us" content: AutofilledByUsInner +""" +Something else autofilled this field +""" class AutofilledBySomethingElse(BaseModel): AutofilledByTypes: Literal["SomethingElse"] = "SomethingElse" content: AutofilledBySomethingElseInner +""" +Enum keeping track of who autofilled a field +""" AutofilledBy = Union[AutofilledByUs, AutofilledBySomethingElse] diff --git a/core/data/tests/can_generate_empty_algebraic_enum/output.py b/core/data/tests/can_generate_empty_algebraic_enum/output.py index 6eb48283..ae8a2c45 100644 --- a/core/data/tests/can_generate_empty_algebraic_enum/output.py +++ b/core/data/tests/can_generate_empty_algebraic_enum/output.py @@ -3,7 +3,6 @@ """ from __future__ import annotations -from enum import Enum from pydantic import BaseModel from typing import Literal, Union @@ -11,10 +10,6 @@ class AddressDetails(BaseModel): pass -class AddressTypes(str, Enum): - FIXED_ADDRESS = "FixedAddress" - NO_FIXED_ADDRESS = "NoFixedAddress" - class AddressFixedAddress(BaseModel): AddressTypes: Literal["FixedAddress"] = "FixedAddress" content: AddressDetails diff --git a/core/data/tests/can_handle_anonymous_struct/output.py b/core/data/tests/can_handle_anonymous_struct/output.py index a8c358fe..069a7e34 100644 --- a/core/data/tests/can_handle_anonymous_struct/output.py +++ b/core/data/tests/can_handle_anonymous_struct/output.py @@ -3,7 +3,6 @@ """ from __future__ import annotations -from enum import Enum from pydantic import BaseModel from typing import Literal, Union @@ -32,18 +31,23 @@ class AutofilledBySomethingElseInner(BaseModel): """ -class AutofilledByTypes(str, Enum): - US = "Us" - SOMETHING_ELSE = "SomethingElse" - +""" +This field was autofilled by us +""" class AutofilledByUs(BaseModel): AutofilledByTypes: Literal["Us"] = "Us" content: AutofilledByUsInner +""" +Something else autofilled this field +""" class AutofilledBySomethingElse(BaseModel): AutofilledByTypes: Literal["SomethingElse"] = "SomethingElse" content: AutofilledBySomethingElseInner +""" +Enum keeping track of who autofilled a field +""" AutofilledBy = Union[AutofilledByUs, AutofilledBySomethingElse] class EnumWithManyVariantsAnonVariantInner(BaseModel): """ @@ -60,14 +64,6 @@ class EnumWithManyVariantsAnotherAnonVariantInner(BaseModel): thing: int -class EnumWithManyVariantsTypes(str, Enum): - UNIT_VARIANT = "UnitVariant" - TUPLE_VARIANT_STRING = "TupleVariantString" - ANON_VARIANT = "AnonVariant" - TUPLE_VARIANT_INT = "TupleVariantInt" - ANOTHER_UNIT_VARIANT = "AnotherUnitVariant" - ANOTHER_ANON_VARIANT = "AnotherAnonVariant" - class EnumWithManyVariantsUnitVariant(BaseModel): EnumWithManyVariantsTypes: Literal["UnitVariant"] = "UnitVariant" @@ -90,4 +86,7 @@ class EnumWithManyVariantsAnotherAnonVariant(BaseModel): EnumWithManyVariantsTypes: Literal["AnotherAnonVariant"] = "AnotherAnonVariant" content: EnumWithManyVariantsAnotherAnonVariantInner +""" +This is a comment (yareek sameek wuz here) +""" EnumWithManyVariants = Union[EnumWithManyVariantsUnitVariant, EnumWithManyVariantsTupleVariantString, EnumWithManyVariantsAnonVariant, EnumWithManyVariantsTupleVariantInt, EnumWithManyVariantsAnotherUnitVariant, EnumWithManyVariantsAnotherAnonVariant] diff --git a/core/data/tests/can_handle_unit_type/output.py b/core/data/tests/can_handle_unit_type/output.py index 0dd13314..a6ffd367 100644 --- a/core/data/tests/can_handle_unit_type/output.py +++ b/core/data/tests/can_handle_unit_type/output.py @@ -3,7 +3,6 @@ """ from __future__ import annotations -from enum import Enum from pydantic import BaseModel, ConfigDict, Field from typing import Literal @@ -17,11 +16,11 @@ class StructHasVoidType(BaseModel): this_is_a_unit: None = Field(alias="thisIsAUnit") -class EnumHasVoidTypeTypes(str, Enum): - HAS_A_UNIT = "hasAUnit" - class EnumHasVoidTypeHasAUnit(BaseModel): EnumHasVoidTypeTypes: Literal["hasAUnit"] = "hasAUnit" content: None +""" +This enum has a variant associated with unit data +""" EnumHasVoidType = EnumHasVoidTypeHasAUnit diff --git a/core/data/tests/excluded_by_target_os/output.py b/core/data/tests/excluded_by_target_os/output.py index f9ad75a8..37cc7e57 100644 --- a/core/data/tests/excluded_by_target_os/output.py +++ b/core/data/tests/excluded_by_target_os/output.py @@ -49,13 +49,6 @@ class TestEnumVariant9Inner(BaseModel): field_2: str = Field(alias="field2") -class TestEnumTypes(str, Enum): - VARIANT_1 = "Variant1" - VARIANT_5 = "Variant5" - VARIANT_7 = "Variant7" - VARIANT_8 = "Variant8" - VARIANT_9 = "Variant9" - class TestEnumVariant1(BaseModel): TestEnumTypes: Literal["Variant1"] = "Variant1" diff --git a/core/data/tests/recursive_enum_decorator/output.py b/core/data/tests/recursive_enum_decorator/output.py index 191f78c9..b04799f9 100644 --- a/core/data/tests/recursive_enum_decorator/output.py +++ b/core/data/tests/recursive_enum_decorator/output.py @@ -3,7 +3,6 @@ """ from __future__ import annotations -from enum import Enum from pydantic import BaseModel from typing import Literal, Union @@ -22,11 +21,6 @@ class MoreOptionsBuiltInner(BaseModel): top: MoreOptions -class MoreOptionsTypes(str, Enum): - NEWS = "news" - EXACTLY = "exactly" - BUILT = "built" - class MoreOptionsNews(BaseModel): MoreOptionsTypes: Literal["news"] = "news" content: bool @@ -40,11 +34,6 @@ class MoreOptionsBuilt(BaseModel): content: MoreOptionsBuiltInner MoreOptions = Union[MoreOptionsNews, MoreOptionsExactly, MoreOptionsBuilt] -class OptionsTypes(str, Enum): - RED = "red" - BANANA = "banana" - VERMONT = "vermont" - class OptionsRed(BaseModel): OptionsTypes: Literal["red"] = "red" content: bool diff --git a/core/data/tests/serialize_anonymous_field_as/output.py b/core/data/tests/serialize_anonymous_field_as/output.py index 5aebe3e0..87c06a9d 100644 --- a/core/data/tests/serialize_anonymous_field_as/output.py +++ b/core/data/tests/serialize_anonymous_field_as/output.py @@ -3,15 +3,13 @@ """ from __future__ import annotations -from enum import Enum from pydantic import BaseModel from typing import Literal, Union -class SomeEnumTypes(str, Enum): - CONTEXT = "Context" - OTHER = "Other" - +""" +The associated String contains some opaque context +""" class SomeEnumContext(BaseModel): SomeEnumTypes: Literal["Context"] = "Context" content: str diff --git a/core/data/tests/smart_pointers/output.py b/core/data/tests/smart_pointers/output.py index 1cb4ad06..6b290653 100644 --- a/core/data/tests/smart_pointers/output.py +++ b/core/data/tests/smart_pointers/output.py @@ -3,7 +3,6 @@ """ from __future__ import annotations -from enum import Enum from pydantic import BaseModel from typing import List, Literal, Union @@ -56,11 +55,6 @@ class RcyColors(BaseModel): green: str -class BoxyColorsTypes(str, Enum): - RED = "Red" - BLUE = "Blue" - GREEN = "Green" - class BoxyColorsRed(BaseModel): BoxyColorsTypes: Literal["Red"] = "Red" @@ -71,4 +65,7 @@ class BoxyColorsGreen(BaseModel): BoxyColorsTypes: Literal["Green"] = "Green" content: str +""" +This is a comment. +""" BoxyColors = Union[BoxyColorsRed, BoxyColorsBlue, BoxyColorsGreen] diff --git a/core/data/tests/test_algebraic_enum_case_name_support/output.py b/core/data/tests/test_algebraic_enum_case_name_support/output.py index a6a14b97..f9362543 100644 --- a/core/data/tests/test_algebraic_enum_case_name_support/output.py +++ b/core/data/tests/test_algebraic_enum_case_name_support/output.py @@ -3,7 +3,6 @@ """ from __future__ import annotations -from enum import Enum from pydantic import BaseModel from typing import List, Literal, Union @@ -11,12 +10,6 @@ class ItemDetailsFieldValue(BaseModel): pass -class AdvancedColorsTypes(str, Enum): - STRING = "string" - NUMBER = "number" - NUMBER_ARRAY = "number-array" - REALLY_COOL_TYPE = "reallyCoolType" - class AdvancedColorsString(BaseModel): AdvancedColorsTypes: Literal["string"] = "string" content: str diff --git a/core/src/language/python.rs b/core/src/language/python.rs index b451b44f..f0f52ecc 100644 --- a/core/src/language/python.rs +++ b/core/src/language/python.rs @@ -271,11 +271,11 @@ impl Language for Python { // Generate named types for any anonymous struct variants of this enum self.write_types_for_anonymous_structs(w, e, &make_anonymous_struct_name)?; - self.add_import("enum".to_string(), "Enum".to_string()); match e { // Write all the unit variants out (there can only be unit variants in // this case) RustEnum::Unit(shared) => { + self.add_import("enum".to_string(), "Enum".to_string()); writeln!(w, "class {}(str, Enum):", shared.id.renamed)?; if shared.variants.is_empty() { writeln!(w, " pass")?; @@ -505,9 +505,7 @@ impl Python { .cloned() .for_each(|v| self.add_type_var(v)); self.add_import("pydantic".to_string(), "BaseModel".to_string()); - // write "types" class: a union of all the enum variants4 let enum_type_class_name = format!("{}Types", shared.id.renamed); - writeln!(w, "class {enum_type_class_name}(str, Enum):")?; let all_enum_variants_name = shared .variants .iter() @@ -518,19 +516,6 @@ impl Python { }) .map(|name| (name.to_case(Case::Snake).to_uppercase(), name)) .collect::>(); - writeln!( - w, - "{}", - all_enum_variants_name - .iter() - .map(|(type_key_name, type_string)| format!( - " {type_key_name} = \"{type_string}\"" - )) - .collect::>() - .join("\n") - )?; - writeln!(w)?; - // all the types and class names for the enum variants in tuple // (type_name, class_name) let mut union_members = Vec::new(); @@ -540,7 +525,8 @@ impl Python { let variant_class_name = format!("{enum_name}{}", &variant.shared().id.original); union_members.push(variant_class_name.clone()); match variant { - RustEnumVariant::Unit(..) => { + RustEnumVariant::Unit(variant_shared) => { + self.write_comments(w, true, &variant_shared.comments, 0)?; self.write_variant_class( &variant_class_name, &enum_type_class_name, @@ -552,7 +538,12 @@ impl Python { )?; writeln!(w)?; } - RustEnumVariant::Tuple { ty, .. } => { + RustEnumVariant::Tuple { + ty, + shared: variant_shared, + } => { + self.write_comments(w, true, &variant_shared.comments, 0)?; + let tuple_name = self .format_type(ty, shared.generic_types.as_slice()) .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?; @@ -571,6 +562,8 @@ impl Python { shared: variant_shared, .. } => { + self.write_comments(w, true, &variant_shared.comments, 0)?; + // writing is taken care of by write_types_for_anonymous_structs in write_enum let variant_class_inner_name = make_struct_name(&variant_shared.id.original); @@ -587,6 +580,7 @@ impl Python { } } } + self.write_comments(w, true, &shared.comments, 0)?; if union_members.len() == 1 { writeln!(w, "{enum_name} = {}", union_members[0])?; } else {