Skip to content

Commit bef21a3

Browse files
committed
Rename members
1 parent 1d30525 commit bef21a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1500
-1319
lines changed

derive-impl/src/pyclass.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,13 @@ where
752752
let raw = item_meta.raw()?;
753753
let sig_doc = text_signature(func.sig(), &py_name);
754754

755+
// Add #[allow(non_snake_case)] for setter methods like set___name__
756+
let method_name = ident.to_string();
757+
if method_name.starts_with("set_") && method_name.contains("__") {
758+
let allow_attr: Attribute = parse_quote!(#[allow(non_snake_case)]);
759+
args.attrs.push(allow_attr);
760+
}
761+
755762
let doc = args.attrs.doc().map(|doc| format_doc(&sig_doc, &doc));
756763
args.context.method_items.add_item(MethodNurseryItem {
757764
py_name,
@@ -780,6 +787,13 @@ where
780787
let item_meta = GetSetItemMeta::from_attr(ident.clone(), &item_attr)?;
781788

782789
let (py_name, kind) = item_meta.getset_name()?;
790+
791+
// Add #[allow(non_snake_case)] for setter methods
792+
if matches!(kind, GetSetItemKind::Set) {
793+
let allow_attr: Attribute = parse_quote!(#[allow(non_snake_case)]);
794+
args.attrs.push(allow_attr);
795+
}
796+
783797
args.context
784798
.getset_items
785799
.add_item(py_name, args.cfgs.to_vec(), kind, ident.clone())?;
@@ -934,6 +948,12 @@ where
934948
_ => MemberKind::ObjectEx,
935949
};
936950

951+
// Add #[allow(non_snake_case)] for setter methods
952+
if matches!(member_item_kind, MemberItemKind::Set) {
953+
let allow_attr: Attribute = parse_quote!(#[allow(non_snake_case)]);
954+
args.attrs.push(allow_attr);
955+
}
956+
937957
args.context.member_items.add_item(
938958
py_name,
939959
member_item_kind,
@@ -1342,7 +1362,7 @@ impl ItemMeta for SlotItemMeta {
13421362
impl SlotItemMeta {
13431363
fn slot_name(&self) -> Result<Ident> {
13441364
let inner = self.inner();
1345-
let slot_name = if let Some((_, meta)) = inner.meta_map.get("name") {
1365+
let method_name = if let Some((_, meta)) = inner.meta_map.get("name") {
13461366
match meta {
13471367
Meta::Path(path) => path.get_ident().cloned(),
13481368
_ => None,
@@ -1356,12 +1376,25 @@ impl SlotItemMeta {
13561376
};
13571377
Some(name)
13581378
};
1359-
slot_name.ok_or_else(|| {
1379+
let method_name = method_name.ok_or_else(|| {
13601380
err_span!(
13611381
inner.meta_ident,
13621382
"#[pyslot] must be of the form #[pyslot] or #[pyslot(slot_name)]",
13631383
)
1364-
})
1384+
})?;
1385+
1386+
// Strip double underscores from slot names like __init__ -> init
1387+
let method_name_str = method_name.to_string();
1388+
let slot_name = if method_name_str.starts_with("__")
1389+
&& method_name_str.ends_with("__")
1390+
&& method_name_str.len() > 4
1391+
{
1392+
&method_name_str[2..method_name_str.len() - 2]
1393+
} else {
1394+
&method_name_str
1395+
};
1396+
1397+
Ok(proc_macro2::Ident::new(slot_name, slot_name.span()))
13651398
}
13661399
}
13671400

derive/src/lib.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,6 @@ pub fn derive_from_args(input: TokenStream) -> TokenStream {
6464
/// but so does any object that implements `PyValue`.
6565
/// Consider using `OptionalArg` for optional arguments.
6666
/// #### Arguments
67-
/// - `magic`: marks the method as a magic method: the method name is surrounded with double underscores.
68-
/// ```rust, ignore
69-
/// #[pyclass]
70-
/// impl MyStruct {
71-
/// // This will be called as the `__add__` method in Python.
72-
/// #[pymethod(magic)]
73-
/// fn add(&self, other: &Self) -> PyResult<i32> {
74-
/// ...
75-
/// }
76-
/// }
77-
/// ```
7867
/// - `name`: the name of the method in Python,
7968
/// by default it is the same as the Rust method, or surrounded by double underscores if magic is present.
8069
/// This overrides `magic` and the default name and cannot be used with `magic` to prevent ambiguity.

stdlib/src/array.rs

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ mod array {
907907
range: OptionalRangeArgs,
908908
vm: &VirtualMachine,
909909
) -> PyResult<usize> {
910-
let (start, stop) = range.saturate(self.len(), vm)?;
910+
let (start, stop) = range.saturate(self.__len__(), vm)?;
911911
self.read().index(x, start, stop, vm)
912912
}
913913

@@ -976,29 +976,29 @@ mod array {
976976
self.write().reverse()
977977
}
978978

979-
#[pymethod(magic)]
980-
fn copy(&self) -> PyArray {
979+
#[pymethod]
980+
fn __copy__(&self) -> PyArray {
981981
self.array.read().clone().into()
982982
}
983983

984-
#[pymethod(magic)]
985-
fn deepcopy(&self, _memo: PyObjectRef) -> PyArray {
986-
self.copy()
984+
#[pymethod]
985+
fn __deepcopy__(&self, _memo: PyObjectRef) -> PyArray {
986+
self.__copy__()
987987
}
988988

989-
fn _getitem(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult {
989+
fn getitem_inner(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult {
990990
match SequenceIndex::try_from_borrowed_object(vm, needle, "array")? {
991991
SequenceIndex::Int(i) => self.read().getitem_by_index(i, vm),
992992
SequenceIndex::Slice(slice) => self.read().getitem_by_slice(slice, vm),
993993
}
994994
}
995995

996-
#[pymethod(magic)]
997-
fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
998-
self._getitem(&needle, vm)
996+
#[pymethod]
997+
fn __getitem__(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
998+
self.getitem_inner(&needle, vm)
999999
}
10001000

1001-
fn _setitem(
1001+
fn setitem_inner(
10021002
zelf: &Py<Self>,
10031003
needle: &PyObject,
10041004
value: PyObjectRef,
@@ -1035,30 +1035,30 @@ mod array {
10351035
}
10361036
}
10371037

1038-
#[pymethod(magic)]
1039-
fn setitem(
1038+
#[pymethod]
1039+
fn __setitem__(
10401040
zelf: &Py<Self>,
10411041
needle: PyObjectRef,
10421042
value: PyObjectRef,
10431043
vm: &VirtualMachine,
10441044
) -> PyResult<()> {
1045-
Self::_setitem(zelf, &needle, value, vm)
1045+
Self::setitem_inner(zelf, &needle, value, vm)
10461046
}
10471047

1048-
fn _delitem(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
1048+
fn delitem_inner(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
10491049
match SequenceIndex::try_from_borrowed_object(vm, needle, "array")? {
10501050
SequenceIndex::Int(i) => self.try_resizable(vm)?.delitem_by_index(i, vm),
10511051
SequenceIndex::Slice(slice) => self.try_resizable(vm)?.delitem_by_slice(slice, vm),
10521052
}
10531053
}
10541054

1055-
#[pymethod(magic)]
1056-
fn delitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
1057-
self._delitem(&needle, vm)
1055+
#[pymethod]
1056+
fn __delitem__(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
1057+
self.delitem_inner(&needle, vm)
10581058
}
10591059

1060-
#[pymethod(magic)]
1061-
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
1060+
#[pymethod]
1061+
fn __add__(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
10621062
if let Some(other) = other.payload::<PyArray>() {
10631063
self.read()
10641064
.add(&other.read(), vm)
@@ -1071,8 +1071,8 @@ mod array {
10711071
}
10721072
}
10731073

1074-
#[pymethod(magic)]
1075-
fn iadd(
1074+
#[pymethod]
1075+
fn __iadd__(
10761076
zelf: PyRef<Self>,
10771077
other: PyObjectRef,
10781078
vm: &VirtualMachine,
@@ -1091,28 +1091,28 @@ mod array {
10911091
}
10921092

10931093
#[pymethod(name = "__rmul__")]
1094-
#[pymethod(magic)]
1095-
fn mul(&self, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
1094+
#[pymethod]
1095+
fn __mul__(&self, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
10961096
self.read()
10971097
.mul(value, vm)
10981098
.map(|x| Self::from(x).into_ref(&vm.ctx))
10991099
}
11001100

1101-
#[pymethod(magic)]
1102-
fn imul(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
1101+
#[pymethod]
1102+
fn __imul__(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
11031103
zelf.try_resizable(vm)?.imul(value, vm)?;
11041104
Ok(zelf)
11051105
}
11061106

1107-
#[pymethod(magic)]
1108-
pub(crate) fn len(&self) -> usize {
1107+
#[pymethod]
1108+
pub(crate) fn __len__(&self) -> usize {
11091109
self.read().len()
11101110
}
11111111

11121112
fn array_eq(&self, other: &Self, vm: &VirtualMachine) -> PyResult<bool> {
11131113
// we cannot use zelf.is(other) for shortcut because if we contenting a
11141114
// float value NaN we always return False even they are the same object.
1115-
if self.len() != other.len() {
1115+
if self.__len__() != other.__len__() {
11161116
return Ok(false);
11171117
}
11181118
let array_a = self.read();
@@ -1133,14 +1133,14 @@ mod array {
11331133
Ok(true)
11341134
}
11351135

1136-
#[pymethod(magic)]
1137-
fn reduce_ex(
1136+
#[pymethod]
1137+
fn __reduce_ex__(
11381138
zelf: &Py<Self>,
11391139
proto: usize,
11401140
vm: &VirtualMachine,
11411141
) -> PyResult<(PyObjectRef, PyTupleRef, Option<PyDictRef>)> {
11421142
if proto < 3 {
1143-
return Self::reduce(zelf, vm);
1143+
return Self::__reduce__(zelf, vm);
11441144
}
11451145
let array = zelf.read();
11461146
let cls = zelf.class().to_owned();
@@ -1157,8 +1157,8 @@ mod array {
11571157
))
11581158
}
11591159

1160-
#[pymethod(magic)]
1161-
fn reduce(
1160+
#[pymethod]
1161+
fn __reduce__(
11621162
zelf: &Py<Self>,
11631163
vm: &VirtualMachine,
11641164
) -> PyResult<(PyObjectRef, PyTupleRef, Option<PyDictRef>)> {
@@ -1179,8 +1179,8 @@ mod array {
11791179
))
11801180
}
11811181

1182-
#[pymethod(magic)]
1183-
fn contains(&self, value: PyObjectRef, vm: &VirtualMachine) -> bool {
1182+
#[pymethod]
1183+
fn __contains__(&self, value: PyObjectRef, vm: &VirtualMachine) -> bool {
11841184
let array = self.array.read();
11851185
for element in array
11861186
.iter(vm)
@@ -1272,7 +1272,7 @@ mod array {
12721272
let class = zelf.class();
12731273
let class_name = class.name();
12741274
if zelf.read().typecode() == 'u' {
1275-
if zelf.len() == 0 {
1275+
if zelf.__len__() == 0 {
12761276
return Ok(format!("{class_name}('u')"));
12771277
}
12781278
let to_unicode = zelf.tounicode(vm)?;
@@ -1303,16 +1303,18 @@ mod array {
13031303
impl AsMapping for PyArray {
13041304
fn as_mapping() -> &'static PyMappingMethods {
13051305
static AS_MAPPING: PyMappingMethods = PyMappingMethods {
1306-
length: atomic_func!(|mapping, _vm| Ok(PyArray::mapping_downcast(mapping).len())),
1306+
length: atomic_func!(|mapping, _vm| Ok(
1307+
PyArray::mapping_downcast(mapping).__len__()
1308+
)),
13071309
subscript: atomic_func!(|mapping, needle, vm| {
1308-
PyArray::mapping_downcast(mapping)._getitem(needle, vm)
1310+
PyArray::mapping_downcast(mapping).getitem_inner(needle, vm)
13091311
}),
13101312
ass_subscript: atomic_func!(|mapping, needle, value, vm| {
13111313
let zelf = PyArray::mapping_downcast(mapping);
13121314
if let Some(value) = value {
1313-
PyArray::_setitem(zelf, needle, value, vm)
1315+
PyArray::setitem_inner(zelf, needle, value, vm)
13141316
} else {
1315-
zelf._delitem(needle, vm)
1317+
zelf.delitem_inner(needle, vm)
13161318
}
13171319
}),
13181320
};
@@ -1323,13 +1325,15 @@ mod array {
13231325
impl AsSequence for PyArray {
13241326
fn as_sequence() -> &'static PySequenceMethods {
13251327
static AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
1326-
length: atomic_func!(|seq, _vm| Ok(PyArray::sequence_downcast(seq).len())),
1328+
length: atomic_func!(|seq, _vm| Ok(PyArray::sequence_downcast(seq).__len__())),
13271329
concat: atomic_func!(|seq, other, vm| {
13281330
let zelf = PyArray::sequence_downcast(seq);
1329-
PyArray::add(zelf, other.to_owned(), vm).map(|x| x.into())
1331+
PyArray::__add__(zelf, other.to_owned(), vm).map(|x| x.into())
13301332
}),
13311333
repeat: atomic_func!(|seq, n, vm| {
1332-
PyArray::sequence_downcast(seq).mul(n, vm).map(|x| x.into())
1334+
PyArray::sequence_downcast(seq)
1335+
.__mul__(n, vm)
1336+
.map(|x| x.into())
13331337
}),
13341338
item: atomic_func!(|seq, i, vm| {
13351339
PyArray::sequence_downcast(seq)
@@ -1346,15 +1350,15 @@ mod array {
13461350
}),
13471351
contains: atomic_func!(|seq, target, vm| {
13481352
let zelf = PyArray::sequence_downcast(seq);
1349-
Ok(zelf.contains(target.to_owned(), vm))
1353+
Ok(zelf.__contains__(target.to_owned(), vm))
13501354
}),
13511355
inplace_concat: atomic_func!(|seq, other, vm| {
13521356
let zelf = PyArray::sequence_downcast(seq).to_owned();
1353-
PyArray::iadd(zelf, other.to_owned(), vm).map(|x| x.into())
1357+
PyArray::__iadd__(zelf, other.to_owned(), vm).map(|x| x.into())
13541358
}),
13551359
inplace_repeat: atomic_func!(|seq, n, vm| {
13561360
let zelf = PyArray::sequence_downcast(seq).to_owned();
1357-
PyArray::imul(zelf, n, vm).map(|x| x.into())
1361+
PyArray::__imul__(zelf, n, vm).map(|x| x.into())
13581362
}),
13591363
};
13601364
&AS_SEQUENCE
@@ -1388,15 +1392,15 @@ mod array {
13881392

13891393
#[pyclass(with(IterNext, Iterable), flags(HAS_DICT))]
13901394
impl PyArrayIter {
1391-
#[pymethod(magic)]
1392-
fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
1395+
#[pymethod]
1396+
fn __setstate__(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
13931397
self.internal
13941398
.lock()
1395-
.set_state(state, |obj, pos| pos.min(obj.len()), vm)
1399+
.set_state(state, |obj, pos| pos.min(obj.__len__()), vm)
13961400
}
13971401

1398-
#[pymethod(magic)]
1399-
fn reduce(&self, vm: &VirtualMachine) -> PyTupleRef {
1402+
#[pymethod]
1403+
fn __reduce__(&self, vm: &VirtualMachine) -> PyTupleRef {
14001404
self.internal
14011405
.lock()
14021406
.builtins_iter_reduce(|x| x.clone().into(), vm)

0 commit comments

Comments
 (0)