Skip to content

Commit

Permalink
rename keyword to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed May 6, 2024
1 parent 62c7fd0 commit 6cdd401
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 19 deletions.
1 change: 1 addition & 0 deletions pyo3-macros-backend/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod kw {
syn::custom_keyword!(annotation);
syn::custom_keyword!(attribute);
syn::custom_keyword!(cancel_handle);
syn::custom_keyword!(constructor);
syn::custom_keyword!(dict);
syn::custom_keyword!(extends);
syn::custom_keyword!(freelist);
Expand Down
29 changes: 16 additions & 13 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::attributes::{
use crate::deprecations::Deprecations;
use crate::konst::{ConstAttributes, ConstSpec};
use crate::method::{FnArg, FnSpec, PyArg, RegularArg};
use crate::pyfunction::SignatureAttribute;
use crate::pyfunction::ConstructorAttribute;
use crate::pyimpl::{gen_py_const, PyClassMethodsType};
use crate::pymethod::{
impl_py_getter_def, impl_py_setter_def, MethodAndMethodDef, MethodAndSlotDef, PropertyType,
Expand Down Expand Up @@ -623,21 +623,21 @@ struct PyClassEnumVariantNamedField<'a> {
/// `#[pyo3()]` options for pyclass enum variants
struct EnumVariantPyO3Options {
name: Option<NameAttribute>,
signature: Option<SignatureAttribute>,
constructor: Option<ConstructorAttribute>,
}

enum EnumVariantPyO3Option {
Name(NameAttribute),
Signature(SignatureAttribute),
Constructor(ConstructorAttribute),
}

impl Parse for EnumVariantPyO3Option {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(attributes::kw::name) {
input.parse().map(EnumVariantPyO3Option::Name)
} else if lookahead.peek(attributes::kw::signature) {
input.parse().map(EnumVariantPyO3Option::Signature)
} else if lookahead.peek(attributes::kw::constructor) {
input.parse().map(EnumVariantPyO3Option::Constructor)
} else {
Err(lookahead.error())
}
Expand All @@ -648,7 +648,7 @@ impl EnumVariantPyO3Options {
fn take_pyo3_options(attrs: &mut Vec<syn::Attribute>) -> Result<Self> {
let mut options = EnumVariantPyO3Options {
name: None,
signature: None,
constructor: None,
};

for option in take_pyo3_options(attrs)? {
Expand All @@ -660,12 +660,12 @@ impl EnumVariantPyO3Options {
);
options.name = Some(name);
}
EnumVariantPyO3Option::Signature(signature) => {
EnumVariantPyO3Option::Constructor(constructor) => {
ensure_spanned!(
options.signature.is_none(),
signature.span() => "`signature` may only be specified once"
options.constructor.is_none(),
constructor.span() => "`constructor` may only be specified once"
);
options.signature = Some(signature);
options.constructor = Some(constructor);
}
}
}
Expand Down Expand Up @@ -706,7 +706,7 @@ fn impl_simple_enum(

let (default_repr, default_repr_slot) = {
let variants_repr = variants.iter().map(|variant| {
ensure_spanned!(variant.options.signature.is_none(), variant.options.signature.span() => "`signature` can't be used on a simple enum variant");
ensure_spanned!(variant.options.constructor.is_none(), variant.options.constructor.span() => "`constructor` can't be used on a simple enum variant");
let variant_name = variant.ident;
// Assuming all variants are unit variants because they are the only type we support.
let repr = format!(
Expand Down Expand Up @@ -1179,8 +1179,11 @@ fn complex_enum_struct_variant_new<'a>(
args
};

let signature = if let Some(signature) = variant.options.signature {
crate::pyfunction::FunctionSignature::from_arguments_and_attribute(args, signature)?
let signature = if let Some(constructor) = variant.options.constructor {
crate::pyfunction::FunctionSignature::from_arguments_and_attribute(
args,
constructor.into_signature(),
)?
} else {
crate::pyfunction::FunctionSignature::from_arguments(args)?
};
Expand Down
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use syn::{

mod signature;

pub use self::signature::{FunctionSignature, SignatureAttribute};
pub use self::signature::{ConstructorAttribute, FunctionSignature, SignatureAttribute};

#[derive(Clone, Debug)]
pub struct PyFunctionArgPyO3Attributes {
Expand Down
10 changes: 10 additions & 0 deletions pyo3-macros-backend/src/pyfunction/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ impl ToTokens for SignatureItemPosargsSep {
}

pub type SignatureAttribute = KeywordAttribute<kw::signature, Signature>;
pub type ConstructorAttribute = KeywordAttribute<kw::constructor, Signature>;

impl ConstructorAttribute {
pub fn into_signature(self) -> SignatureAttribute {
SignatureAttribute {
kw: kw::signature(self.kw.span),
value: self.value,
}
}
}

#[derive(Default)]
pub struct PythonSignature {
Expand Down
2 changes: 1 addition & 1 deletion pytests/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub enum ComplexEnum {
b: f64,
c: bool,
},
#[pyo3(signature = (a = 42, b = None))]
#[pyo3(constructor = (a = 42, b = None))]
VariantWithDefault {
a: i32,
b: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/invalid_pyclass_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ enum NoTupleVariants {

#[pyclass]
enum SimpleNoSignature {
#[pyo3(signature = (a, b))]
#[pyo3(constructor = (a, b))]
A,
B,
}
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/invalid_pyclass_enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ error: Tuple variant `TupleVariant` is not yet supported in a complex enum
27 | TupleVariant(i32),
| ^^^^^^^^^^^^

error: `signature` can't be used on a simple enum variant
error: `constructor` can't be used on a simple enum variant
--> tests/ui/invalid_pyclass_enum.rs:32:12
|
32 | #[pyo3(signature = (a, b))]
| ^^^^^^^^^
32 | #[pyo3(constructor = (a, b))]
| ^^^^^^^^^^^

0 comments on commit 6cdd401

Please sign in to comment.