@@ -605,12 +605,10 @@ impl PyFunction {
605605 // Check for callable __annotate__ and clone it before calling
606606 let annotate_fn = {
607607 let annotate = self . annotate . lock ( ) ;
608- if let Some ( ref func) = * annotate {
609- if func. is_callable ( ) {
610- Some ( func. clone ( ) )
611- } else {
612- None
613- }
608+ if let Some ( ref func) = * annotate
609+ && func. is_callable ( )
610+ {
611+ Some ( func. clone ( ) )
614612 } else {
615613 None
616614 }
@@ -641,26 +639,24 @@ impl PyFunction {
641639 }
642640
643641 #[ pygetset( setter) ]
644- fn set___annotations__ ( & self , value : PySetterValue , vm : & VirtualMachine ) -> PyResult < ( ) > {
645- match value {
646- PySetterValue :: Assign ( value) => {
647- if vm. is_none ( & value) {
648- * self . annotations . lock ( ) = None ;
649- } else {
650- let annotations =
651- value. downcast :: < crate :: builtins:: PyDict > ( ) . map_err ( |_| {
652- vm. new_type_error ( "__annotations__ must be set to a dict object" )
653- } ) ?;
654- * self . annotations . lock ( ) = Some ( annotations) ;
655- }
656- // Clear __annotate__ when __annotations__ is set
657- * self . annotate . lock ( ) = None ;
658- }
659- PySetterValue :: Delete => {
660- * self . annotations . lock ( ) = None ;
661- * self . annotate . lock ( ) = None ;
642+ fn set___annotations__ (
643+ & self ,
644+ value : PySetterValue < Option < PyObjectRef > > ,
645+ vm : & VirtualMachine ,
646+ ) -> PyResult < ( ) > {
647+ let annotations = match value {
648+ PySetterValue :: Assign ( Some ( value) ) => {
649+ let annotations = value. downcast :: < crate :: builtins:: PyDict > ( ) . map_err ( |_| {
650+ vm. new_type_error ( "__annotations__ must be set to a dict object" )
651+ } ) ?;
652+ Some ( annotations)
662653 }
663- }
654+ PySetterValue :: Assign ( None ) | PySetterValue :: Delete => None ,
655+ } ;
656+ * self . annotations . lock ( ) = annotations;
657+
658+ // Clear __annotate__ when __annotations__ is set
659+ * self . annotate . lock ( ) = None ;
664660 Ok ( ( ) )
665661 }
666662
@@ -673,23 +669,26 @@ impl PyFunction {
673669 }
674670
675671 #[ pygetset( setter) ]
676- fn set___annotate__ ( & self , value : PySetterValue , vm : & VirtualMachine ) -> PyResult < ( ) > {
677- match value {
678- PySetterValue :: Assign ( value) => {
679- if vm. is_none ( & value) {
680- * self . annotate . lock ( ) = Some ( value) ;
681- } else if value. is_callable ( ) {
682- * self . annotate . lock ( ) = Some ( value) ;
683- // Clear cached __annotations__ when __annotate__ is set
684- * self . annotations . lock ( ) = None ;
685- } else {
672+ fn set___annotate__ (
673+ & self ,
674+ value : PySetterValue < Option < PyObjectRef > > ,
675+ vm : & VirtualMachine ,
676+ ) -> PyResult < ( ) > {
677+ let annotate = match value {
678+ PySetterValue :: Assign ( Some ( value) ) => {
679+ if !value. is_callable ( ) {
686680 return Err ( vm. new_type_error ( "__annotate__ must be callable or None" ) ) ;
687681 }
682+ // Clear cached __annotations__ when __annotate__ is set
683+ * self . annotations . lock ( ) = None ;
684+ Some ( value)
688685 }
686+ PySetterValue :: Assign ( None ) => None ,
689687 PySetterValue :: Delete => {
690688 return Err ( vm. new_type_error ( "__annotate__ cannot be deleted" ) ) ;
691689 }
692- }
690+ } ;
691+ * self . annotate . lock ( ) = annotate;
693692 Ok ( ( ) )
694693 }
695694
0 commit comments