Skip to content

Commit

Permalink
Remove config dict and allow unit enums to be serializble
Browse files Browse the repository at this point in the history
  • Loading branch information
MOmarMiraj committed Nov 7, 2024
1 parent 9810e7b commit 92f6922
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 16 deletions.
3 changes: 1 addition & 2 deletions core/data/tests/can_generate_bare_string_enum/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from pydantic import ConfigDict


class Colors(Enum):
model_config = ConfigDict(use_enum_values=True)
class Colors(str, Enum):
RED = "Red"
BLUE = "Blue"
GREEN = "Green"
3 changes: 1 addition & 2 deletions core/data/tests/can_generate_simple_enum/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from pydantic import ConfigDict


class Colors(Enum):
model_config = ConfigDict(use_enum_values=True)
class Colors(str, Enum):
RED = "Red"
BLUE = "Blue"
GREEN = "Green"
3 changes: 1 addition & 2 deletions core/data/tests/can_handle_quote_in_serde_rename/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
from pydantic import ConfigDict


class Colors(Enum):
model_config = ConfigDict(use_enum_values=True)
class Colors(str, Enum):
GREEN = "Green\""
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from pydantic import ConfigDict


class Colors(Enum):
model_config = ConfigDict(use_enum_values=True)
class Colors(str, Enum):
RED = "red"
BLUE = "blue"
GREEN = "green-like"
2 changes: 1 addition & 1 deletion core/data/tests/excluded_by_target_os/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class NestedNotTarget1(BaseModel):
class OtherExcluded(BaseModel):
pass

class SomeEnum(Enum):
class SomeEnum(str, Enum):
pass
class TestEnumVariant7(BaseModel):
"""
Expand Down
3 changes: 1 addition & 2 deletions core/data/tests/test_simple_enum_case_name_support/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from pydantic import ConfigDict


class Colors(Enum):
model_config = ConfigDict(use_enum_values=True)
class Colors(str, Enum):
RED = "red"
BLUE = "blue-ish"
GREEN = "Green"
9 changes: 4 additions & 5 deletions core/src/language/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl Language for Python {

self.write_comments(w, true, &rs.comments, 1)?;

handle_model_config(w, self, rs);
handle_model_config(w, self, &rs.fields);

rs.fields
.iter()
Expand All @@ -275,11 +275,10 @@ impl Language for Python {
// Write all the unit variants out (there can only be unit variants in
// this case)
RustEnum::Unit(shared) => {
writeln!(w, "class {}(Enum):", shared.id.renamed)?;
writeln!(w, "class {}(str, Enum):", shared.id.renamed)?;
if shared.variants.is_empty() {
writeln!(w, " pass")?;
} else {
writeln!(w, " model_config = ConfigDict(use_enum_values=True)")?;
shared.variants.iter().try_for_each(|v| {
writeln!(
w,
Expand Down Expand Up @@ -765,8 +764,8 @@ fn python_property_aware_rename(name: &str) -> String {
}

// If at least one field from within a class is changed when the serde rename is used (a.k.a the field has 2 words) then we must use aliasing and we must also use a config dict at the top level of the class.
fn handle_model_config(w: &mut dyn Write, python_module: &mut Python, rs: &RustStruct) {
let visibly_renamed_field = rs.fields.iter().find(|f| {
fn handle_model_config(w: &mut dyn Write, python_module: &mut Python, fields: &[RustField]) {
let visibly_renamed_field = fields.iter().find(|f| {
let python_field_name = python_property_aware_rename(&f.id.original);
python_field_name != f.id.renamed
});
Expand Down

0 comments on commit 92f6922

Please sign in to comment.