Skip to content

Commit 1999aa9

Browse files
authored
delete some unneeded complexity from PyMethodDefType (#5581)
* delete some unneeded complexity from `PyMethodDefType` * `geitem` -> `getitem`
1 parent bee3fda commit 1999aa9

File tree

6 files changed

+32
-55
lines changed

6 files changed

+32
-55
lines changed

guide/src/python-typing-hints.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ To overcome this limitation, implementers can pass the `generic` parameter to `p
237237
#### Advanced Users
238238

239239
`#[pyclass(generic)]` implements a very simple runtime behavior that accepts any generic argument.
240-
Advanced users can opt to manually implement [`__class_geitem__`](https://docs.python.org/3/reference/datamodel.html#emulating-generic-types) for the generic class to have more control.
240+
Advanced users can opt to manually implement [`__class_getitem__`](https://docs.python.org/3/reference/datamodel.html#emulating-generic-types) for the generic class to have more control.
241241

242242
```rust ignore
243243
impl MyClass {

pyo3-macros-backend/src/method.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,11 @@ impl<'a> FnSpec<'a> {
903903
pub fn get_methoddef(&self, wrapper: impl ToTokens, doc: &PythonDoc, ctx: &Ctx) -> TokenStream {
904904
let Ctx { pyo3_path, .. } = ctx;
905905
let python_name = self.null_terminated_python_name();
906+
let flags = match self.tp {
907+
FnType::FnClass(_) => quote! { .flags(#pyo3_path::ffi::METH_CLASS) },
908+
FnType::FnStatic => quote! { .flags(#pyo3_path::ffi::METH_STATIC) },
909+
_ => quote! {},
910+
};
906911
match self.convention {
907912
CallingConvention::Noargs => quote! {
908913
#pyo3_path::impl_::pymethods::PyMethodDef::noargs(
@@ -924,7 +929,7 @@ impl<'a> FnSpec<'a> {
924929
trampoline
925930
},
926931
#doc,
927-
)
932+
) #flags
928933
},
929934
CallingConvention::Fastcall => quote! {
930935
#pyo3_path::impl_::pymethods::PyMethodDef::fastcall_cfunction_with_keywords(
@@ -948,7 +953,7 @@ impl<'a> FnSpec<'a> {
948953
trampoline
949954
},
950955
#doc,
951-
)
956+
) #flags
952957
},
953958
CallingConvention::Varargs => quote! {
954959
#pyo3_path::impl_::pymethods::PyMethodDef::cfunction_with_keywords(
@@ -970,7 +975,7 @@ impl<'a> FnSpec<'a> {
970975
trampoline
971976
},
972977
#doc,
973-
)
978+
) #flags
974979
},
975980
CallingConvention::TpNew => unreachable!("tp_new cannot get a methoddef"),
976981
}

pyo3-macros-backend/src/pyclass.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,11 @@ fn impl_class(
476476
ctx,
477477
)?;
478478

479-
let (default_class_geitem, default_class_geitem_method) =
480-
pyclass_class_geitem(&args.options, &syn::parse_quote!(#cls), ctx)?;
479+
let (default_class_getitem, default_class_getitem_method) =
480+
pyclass_class_getitem(&args.options, &syn::parse_quote!(#cls), ctx)?;
481481

482-
if let Some(default_class_geitem_method) = default_class_geitem_method {
483-
default_methods.push(default_class_geitem_method);
482+
if let Some(default_class_getitem_method) = default_class_getitem_method {
483+
default_methods.push(default_class_getitem_method);
484484
}
485485

486486
let (default_str, default_str_slot) =
@@ -514,7 +514,7 @@ fn impl_class(
514514
#default_richcmp
515515
#default_hash
516516
#default_str
517-
#default_class_geitem
517+
#default_class_getitem
518518
}
519519
})
520520
}
@@ -2233,7 +2233,7 @@ fn pyclass_hash(
22332233
}
22342234
}
22352235

2236-
fn pyclass_class_geitem(
2236+
fn pyclass_class_getitem(
22372237
options: &PyClassPyO3Options,
22382238
cls: &syn::Type,
22392239
ctx: &Ctx,
@@ -2242,7 +2242,7 @@ fn pyclass_class_geitem(
22422242
match options.generic {
22432243
Some(_) => {
22442244
let ident = format_ident!("__class_getitem__");
2245-
let mut class_geitem_impl: syn::ImplItemFn = {
2245+
let mut class_getitem_impl: syn::ImplItemFn = {
22462246
parse_quote! {
22472247
#[classmethod]
22482248
fn #ident<'py>(
@@ -2255,19 +2255,18 @@ fn pyclass_class_geitem(
22552255
};
22562256

22572257
let spec = FnSpec::parse(
2258-
&mut class_geitem_impl.sig,
2259-
&mut class_geitem_impl.attrs,
2258+
&mut class_getitem_impl.sig,
2259+
&mut class_getitem_impl.attrs,
22602260
Default::default(),
22612261
)?;
22622262

2263-
let class_geitem_method = crate::pymethod::impl_py_method_def(
2263+
let class_getitem_method = crate::pymethod::impl_py_method_def(
22642264
cls,
22652265
&spec,
2266-
&spec.get_doc(&class_geitem_impl.attrs, ctx)?,
2267-
Some(quote!(#pyo3_path::ffi::METH_CLASS)),
2266+
&spec.get_doc(&class_getitem_impl.attrs, ctx)?,
22682267
ctx,
22692268
)?;
2270-
Ok((Some(class_geitem_impl), Some(class_geitem_method)))
2269+
Ok((Some(class_getitem_impl), Some(class_getitem_method)))
22712270
}
22722271
None => Ok((None, None)),
22732272
}

pyo3-macros-backend/src/pymethod.rs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ pub fn gen_py_method(
222222
ctx: &Ctx,
223223
) -> Result<GeneratedPyMethod> {
224224
let spec = &method.spec;
225-
let Ctx { pyo3_path, .. } = ctx;
226225

227226
if spec.asyncness.is_some() {
228227
ensure_spanned!(
@@ -260,27 +259,14 @@ pub fn gen_py_method(
260259
}
261260
}
262261
// ordinary functions (with some specialties)
263-
(_, FnType::Fn(_)) => GeneratedPyMethod::Method(impl_py_method_def(
264-
cls,
265-
spec,
266-
&spec.get_doc(meth_attrs, ctx)?,
267-
None,
268-
ctx,
269-
)?),
270-
(_, FnType::FnClass(_)) => GeneratedPyMethod::Method(impl_py_method_def(
271-
cls,
272-
spec,
273-
&spec.get_doc(meth_attrs, ctx)?,
274-
Some(quote!(#pyo3_path::ffi::METH_CLASS)),
275-
ctx,
276-
)?),
277-
(_, FnType::FnStatic) => GeneratedPyMethod::Method(impl_py_method_def(
278-
cls,
279-
spec,
280-
&spec.get_doc(meth_attrs, ctx)?,
281-
Some(quote!(#pyo3_path::ffi::METH_STATIC)),
282-
ctx,
283-
)?),
262+
(_, FnType::Fn(_)) | (_, FnType::FnClass(_)) | (_, FnType::FnStatic) => {
263+
GeneratedPyMethod::Method(impl_py_method_def(
264+
cls,
265+
spec,
266+
&spec.get_doc(meth_attrs, ctx)?,
267+
ctx,
268+
)?)
269+
}
284270
// special prototypes
285271
(_, FnType::FnNew) | (_, FnType::FnNewClass(_)) => {
286272
GeneratedPyMethod::Proto(impl_py_method_def_new(cls, spec, ctx)?)
@@ -351,21 +337,14 @@ pub fn impl_py_method_def(
351337
cls: &syn::Type,
352338
spec: &FnSpec<'_>,
353339
doc: &PythonDoc,
354-
flags: Option<TokenStream>,
355340
ctx: &Ctx,
356341
) -> Result<MethodAndMethodDef> {
357342
let Ctx { pyo3_path, .. } = ctx;
358343
let wrapper_ident = format_ident!("__pymethod_{}__", spec.python_name);
359344
let associated_method = spec.get_wrapper_function(&wrapper_ident, Some(cls), ctx)?;
360-
let add_flags = flags.map(|flags| quote!(.flags(#flags)));
361-
let methoddef_type = match spec.tp {
362-
FnType::FnStatic => quote!(Static),
363-
FnType::FnClass(_) => quote!(Class),
364-
_ => quote!(Method),
365-
};
366345
let methoddef = spec.get_methoddef(quote! { #cls::#wrapper_ident }, doc, ctx);
367346
let method_def = quote! {
368-
#pyo3_path::impl_::pymethods::PyMethodDefType::#methoddef_type(#methoddef #add_flags)
347+
#pyo3_path::impl_::pymethods::PyMethodDefType::Method(#methoddef)
369348
};
370349
Ok(MethodAndMethodDef {
371350
associated_method,

src/impl_/pymethods.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ impl IPowModulo {
5959
/// It is used by the `#[pymethods]` attribute.
6060
#[derive(Copy, Clone)]
6161
pub enum PyMethodDefType {
62-
/// Represents class method
63-
Class(PyMethodDef),
64-
/// Represents static method
65-
Static(PyMethodDef),
66-
/// Represents normal method
62+
/// Represents a class method (might be `classmethod` or `staticmethod`, depends on `ml_flags`)
6763
Method(PyMethodDef),
6864
/// Represents class attribute, used by `#[attribute]`
6965
ClassAttribute(PyClassAttributeDef),

src/pyclass/create_type_object.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ impl PyTypeBuilder {
192192
.entry(setter.name)
193193
.or_default()
194194
.add_setter(setter),
195-
PyMethodDefType::Method(def)
196-
| PyMethodDefType::Class(def)
197-
| PyMethodDefType::Static(def) => self.method_defs.push(def.into_raw()),
195+
PyMethodDefType::Method(def) => self.method_defs.push(def.into_raw()),
198196
// These class attributes are added after the type gets created by LazyStaticType
199197
PyMethodDefType::ClassAttribute(_) => {}
200198
PyMethodDefType::StructMember(def) => self.member_defs.push(*def),

0 commit comments

Comments
 (0)