@@ -19,8 +19,9 @@ use crate::method::{FnArg, FnSpec, PyArg, RegularArg};
19
19
use crate :: pyfunction:: ConstructorAttribute ;
20
20
use crate :: pyimpl:: { gen_py_const, get_cfg_attributes, PyClassMethodsType } ;
21
21
use crate :: pymethod:: {
22
- impl_py_getter_def, impl_py_setter_def, MethodAndMethodDef , MethodAndSlotDef , PropertyType ,
23
- SlotDef , __GETITEM__, __HASH__, __INT__, __LEN__, __REPR__, __RICHCMP__, __STR__,
22
+ impl_py_class_attribute, impl_py_getter_def, impl_py_setter_def, MethodAndMethodDef ,
23
+ MethodAndSlotDef , PropertyType , SlotDef , __GETITEM__, __HASH__, __INT__, __LEN__, __REPR__,
24
+ __RICHCMP__, __STR__,
24
25
} ;
25
26
use crate :: pyversions:: is_abi3_before;
26
27
use crate :: utils:: { self , apply_renaming_rule, Ctx , LitCStr , PythonDoc } ;
@@ -1185,34 +1186,30 @@ fn impl_complex_enum_variant_cls(
1185
1186
}
1186
1187
1187
1188
fn impl_complex_enum_variant_match_args (
1188
- ctx : & Ctx ,
1189
+ ctx @ Ctx { pyo3_path , .. } : & Ctx ,
1189
1190
variant_cls_type : & syn:: Type ,
1190
1191
field_names : & mut Vec < Ident > ,
1191
- ) -> ( MethodAndMethodDef , syn:: ImplItemConst ) {
1192
+ ) -> syn :: Result < ( MethodAndMethodDef , syn:: ImplItemFn ) > {
1192
1193
let ident = format_ident ! ( "__match_args__" ) ;
1193
- let match_args_const_impl: syn:: ImplItemConst = {
1194
- let args_tp = field_names. iter ( ) . map ( |_| {
1195
- quote ! { & ' static str }
1196
- } ) ;
1194
+ let mut match_args_impl: syn:: ImplItemFn = {
1197
1195
parse_quote ! {
1198
- #[ allow( non_upper_case_globals) ]
1199
- const #ident: ( #( #args_tp, ) * ) = (
1200
- #( stringify!( #field_names) , ) *
1201
- ) ;
1196
+ #[ classattr]
1197
+ fn #ident( py: #pyo3_path:: Python <' _>) -> #pyo3_path:: PyResult <#pyo3_path:: Bound <' _, #pyo3_path:: types:: PyTuple >> {
1198
+ #pyo3_path:: types:: PyTuple :: new:: <& str , _>( py, [
1199
+ #( stringify!( #field_names) , ) *
1200
+ ] )
1201
+ }
1202
1202
}
1203
1203
} ;
1204
1204
1205
- let spec = ConstSpec {
1206
- rust_ident : ident,
1207
- attributes : ConstAttributes {
1208
- is_class_attr : true ,
1209
- name : None ,
1210
- } ,
1211
- } ;
1212
-
1213
- let variant_match_args = gen_py_const ( variant_cls_type, & spec, ctx) ;
1205
+ let spec = FnSpec :: parse (
1206
+ & mut match_args_impl. sig ,
1207
+ & mut match_args_impl. attrs ,
1208
+ Default :: default ( ) ,
1209
+ ) ?;
1210
+ let variant_match_args = impl_py_class_attribute ( variant_cls_type, & spec, ctx) ?;
1214
1211
1215
- ( variant_match_args, match_args_const_impl )
1212
+ Ok ( ( variant_match_args, match_args_impl ) )
1216
1213
}
1217
1214
1218
1215
fn impl_complex_enum_struct_variant_cls (
@@ -1260,14 +1257,15 @@ fn impl_complex_enum_struct_variant_cls(
1260
1257
}
1261
1258
1262
1259
let ( variant_match_args, match_args_const_impl) =
1263
- impl_complex_enum_variant_match_args ( ctx, & variant_cls_type, & mut field_names) ;
1260
+ impl_complex_enum_variant_match_args ( ctx, & variant_cls_type, & mut field_names) ? ;
1264
1261
1265
1262
field_getters. push ( variant_match_args) ;
1266
1263
1267
1264
let cls_impl = quote ! {
1268
1265
#[ doc( hidden) ]
1269
1266
#[ allow( non_snake_case) ]
1270
1267
impl #variant_cls {
1268
+ #[ allow( clippy:: too_many_arguments) ]
1271
1269
fn __pymethod_constructor__( py: #pyo3_path:: Python <' _>, #( #fields_with_types, ) * ) -> #pyo3_path:: PyClassInitializer <#variant_cls> {
1272
1270
let base_value = #enum_name:: #variant_ident { #( #field_names, ) * } ;
1273
1271
<#pyo3_path:: PyClassInitializer <#enum_name> as :: std:: convert:: From <#enum_name>>:: from( base_value) . add_subclass( #variant_cls)
@@ -1434,14 +1432,15 @@ fn impl_complex_enum_tuple_variant_cls(
1434
1432
slots. push ( variant_getitem) ;
1435
1433
1436
1434
let ( variant_match_args, match_args_method_impl) =
1437
- impl_complex_enum_variant_match_args ( ctx, & variant_cls_type, & mut field_names) ;
1435
+ impl_complex_enum_variant_match_args ( ctx, & variant_cls_type, & mut field_names) ? ;
1438
1436
1439
1437
field_getters. push ( variant_match_args) ;
1440
1438
1441
1439
let cls_impl = quote ! {
1442
1440
#[ doc( hidden) ]
1443
1441
#[ allow( non_snake_case) ]
1444
1442
impl #variant_cls {
1443
+ #[ allow( clippy:: too_many_arguments) ]
1445
1444
fn __pymethod_constructor__( py: #pyo3_path:: Python <' _>, #( #field_names : #field_types, ) * ) -> #pyo3_path:: PyClassInitializer <#variant_cls> {
1446
1445
let base_value = #enum_name:: #variant_ident ( #( #field_names, ) * ) ;
1447
1446
<#pyo3_path:: PyClassInitializer <#enum_name> as :: std:: convert:: From <#enum_name>>:: from( base_value) . add_subclass( #variant_cls)
0 commit comments