11use std:: fmt:: Display ;
22
33use crate :: attributes:: { TextSignatureAttribute , TextSignatureAttributeValue } ;
4+ use crate :: deprecations:: { Deprecation , Deprecations } ;
45use crate :: params:: impl_arg_params;
56use crate :: pyfunction:: { FunctionSignature , PyFunctionArgPyO3Attributes } ;
67use crate :: pyfunction:: { PyFunctionOptions , SignatureAttribute } ;
@@ -228,6 +229,7 @@ pub struct FnSpec<'a> {
228229 pub convention : CallingConvention ,
229230 pub text_signature : Option < TextSignatureAttribute > ,
230231 pub unsafety : Option < syn:: Token ![ unsafe ] > ,
232+ pub deprecations : Deprecations ,
231233}
232234
233235pub fn get_return_info ( output : & syn:: ReturnType ) -> syn:: Type {
@@ -275,8 +277,9 @@ impl<'a> FnSpec<'a> {
275277 } = options;
276278
277279 let mut python_name = name. map ( |name| name. value . 0 ) ;
280+ let mut deprecations = Deprecations :: new ( ) ;
278281
279- let fn_type = Self :: parse_fn_type ( sig, meth_attrs, & mut python_name) ?;
282+ let fn_type = Self :: parse_fn_type ( sig, meth_attrs, & mut python_name, & mut deprecations ) ?;
280283 ensure_signatures_on_valid_method ( & fn_type, signature. as_ref ( ) , text_signature. as_ref ( ) ) ?;
281284
282285 let name = & sig. ident ;
@@ -315,6 +318,7 @@ impl<'a> FnSpec<'a> {
315318 output : ty,
316319 text_signature,
317320 unsafety : sig. unsafety ,
321+ deprecations,
318322 } )
319323 }
320324
@@ -326,8 +330,9 @@ impl<'a> FnSpec<'a> {
326330 sig : & syn:: Signature ,
327331 meth_attrs : & mut Vec < syn:: Attribute > ,
328332 python_name : & mut Option < syn:: Ident > ,
333+ deprecations : & mut Deprecations ,
329334 ) -> Result < FnType > {
330- let mut method_attributes = parse_method_attributes ( meth_attrs) ?;
335+ let mut method_attributes = parse_method_attributes ( meth_attrs, deprecations ) ?;
331336
332337 let name = & sig. ident ;
333338 let parse_receiver = |msg : & ' static str | {
@@ -648,7 +653,10 @@ impl MethodTypeAttribute {
648653 /// If the attribute does not match one of the attribute names, returns `Ok(None)`.
649654 ///
650655 /// Otherwise will either return a parse error or the attribute.
651- fn parse_if_matching_attribute ( attr : & syn:: Attribute ) -> Result < Option < Self > > {
656+ fn parse_if_matching_attribute (
657+ attr : & syn:: Attribute ,
658+ deprecations : & mut Deprecations ,
659+ ) -> Result < Option < Self > > {
652660 fn ensure_no_arguments ( meta : & syn:: Meta , ident : & str ) -> syn:: Result < ( ) > {
653661 match meta {
654662 syn:: Meta :: Path ( _) => Ok ( ( ) ) ,
@@ -693,9 +701,10 @@ impl MethodTypeAttribute {
693701 ensure_no_arguments ( meta, "new" ) ?;
694702 Ok ( Some ( MethodTypeAttribute :: New ( path. span ( ) ) ) )
695703 } else if path. is_ident ( "__new__" ) {
696- // TODO deprecate this form?
704+ let span = path. span ( ) ;
705+ deprecations. push ( Deprecation :: PyMethodsNewDeprecatedForm , span) ;
697706 ensure_no_arguments ( meta, "__new__" ) ?;
698- Ok ( Some ( MethodTypeAttribute :: New ( path . span ( ) ) ) )
707+ Ok ( Some ( MethodTypeAttribute :: New ( span) ) )
699708 } else if path. is_ident ( "classmethod" ) {
700709 ensure_no_arguments ( meta, "classmethod" ) ?;
701710 Ok ( Some ( MethodTypeAttribute :: ClassMethod ( path. span ( ) ) ) )
@@ -730,12 +739,15 @@ impl Display for MethodTypeAttribute {
730739 }
731740}
732741
733- fn parse_method_attributes ( attrs : & mut Vec < syn:: Attribute > ) -> Result < Vec < MethodTypeAttribute > > {
742+ fn parse_method_attributes (
743+ attrs : & mut Vec < syn:: Attribute > ,
744+ deprecations : & mut Deprecations ,
745+ ) -> Result < Vec < MethodTypeAttribute > > {
734746 let mut new_attrs = Vec :: new ( ) ;
735747 let mut found_attrs = Vec :: new ( ) ;
736748
737749 for attr in attrs. drain ( ..) {
738- match MethodTypeAttribute :: parse_if_matching_attribute ( & attr) ? {
750+ match MethodTypeAttribute :: parse_if_matching_attribute ( & attr, deprecations ) ? {
739751 Some ( attr) => found_attrs. push ( attr) ,
740752 None => new_attrs. push ( attr) ,
741753 }
0 commit comments