Skip to content

Commit

Permalink
update: removed checks for shorthand and renaming for enums and simpl…
Browse files Browse the repository at this point in the history
…ified back to inline check for structs
  • Loading branch information
MG committed Jul 16, 2024
1 parent 1597175 commit 5709b85
Showing 1 changed file with 9 additions and 30 deletions.
39 changes: 9 additions & 30 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,33 +382,6 @@ fn get_class_python_name<'a>(cls: &'a syn::Ident, args: &'a PyClassArgs) -> Cow<
.unwrap_or_else(|| Cow::Owned(cls.unraw()))
}

// Enum used to provide handling of different types in the same validation logic in validate_renaming_mixed_with_str
enum VariantType<'a> {
Fields(&'a Vec<(&'a syn::Field, FieldPyO3Options)>),
UnitVariant(&'a Vec<PyClassEnumUnitVariant<'a>>),
EnumVariant(&'a Vec<PyClassEnumVariant<'a>>),
}

fn validate_renaming_mixed_with_str(
args: &PyClassArgs,
variant: VariantType<'_>,
) -> syn::Result<()> {
if let Some(str) = &args.options.str {
if str.value.is_some() {
// check if any renaming is present
let variant_check = match variant {
VariantType::Fields(v) => v.iter().all(|x| x.1.name.is_none()),
VariantType::UnitVariant(v) => v.iter().all(|x| x.options.name.is_none()),
VariantType::EnumVariant(v) => v.iter().all(|x| x.get_options().name.is_none()),
};
let renaming_conflict =
variant_check & args.options.name.is_none() & args.options.rename_all.is_none();
ensure_spanned!(renaming_conflict, str.value.span() => "The optional string format shorthand argument to `str` is incompatible with any renaming via `name` or `rename_all`. You should remove the string format shorthand argument and implement the `Display` trait or implement `__str__` directly.");
}
}
Ok(())
}

fn impl_class(
cls: &syn::Ident,
args: &PyClassArgs,
Expand All @@ -420,7 +393,15 @@ fn impl_class(
let Ctx { pyo3_path, .. } = ctx;
let pytypeinfo_impl = impl_pytypeinfo(cls, args, ctx);

validate_renaming_mixed_with_str(args, VariantType::Fields(&field_options))?;
if let Some(str) = &args.options.str {
if str.value.is_some() {
// check if any renaming is present
let renaming_conflict = field_options.iter().all(|x| x.1.name.is_none())
& args.options.name.is_none()
& args.options.rename_all.is_none();
ensure_spanned!(renaming_conflict, str.value.span() => "The optional string format shorthand argument to `str` is incompatible with any renaming via `name` or `rename_all`. You should remove the string format shorthand argument and implement the `Display` trait or implement `__str__` directly.");
}
}

let (default_str, default_str_slot) =
implement_pyclass_str(&args.options, &syn::parse_quote!(#cls), ctx);
Expand Down Expand Up @@ -907,7 +888,6 @@ fn impl_simple_enum(
(repr_impl, repr_slot)
};

validate_renaming_mixed_with_str(args, VariantType::UnitVariant(&variants))?;
let (default_str, default_str_slot) = implement_pyclass_str(&args.options, &ty, ctx);

let repr_type = &simple_enum.repr_type;
Expand Down Expand Up @@ -998,7 +978,6 @@ fn impl_complex_enum(
let (default_richcmp, default_richcmp_slot) = pyclass_richcmp(&args.options, &ty, ctx)?;
let (default_hash, default_hash_slot) = pyclass_hash(&args.options, &ty, ctx)?;

validate_renaming_mixed_with_str(&args, VariantType::EnumVariant(&variants))?;
let (default_str, default_str_slot) = implement_pyclass_str(&args.options, &ty, ctx);

let mut default_slots = vec![];
Expand Down

0 comments on commit 5709b85

Please sign in to comment.