@@ -2267,15 +2267,27 @@ def __get__(self, instance, owner=None):
22672267                return  self 
22682268            cache  =  instance .__dict__ 
22692269            try :
2270+                 # If cache is ready, return it 
22702271                val  =  cache [self .attrname ]
22712272            except  KeyError :
2272-                 source  =  self .func (instance )
2273-                 if  isinstance (source , Function ):
2274-                     # Avoid creating a new Function object if the source is already one 
2275-                     val  =  source 
2276-                     val .reset (* args , ** kwargs )
2277-                 else :
2273+                 # If cache is not ready, create it 
2274+                 try :
2275+                     # Handle methods which return Function instances 
2276+                     val  =  self .func (instance ).reset (* args , ** kwargs )
2277+                 except  AttributeError :
2278+                     # Handle methods which return a valid source 
2279+                     source  =  self .func (instance )
2280+                     val  =  Function (source , * args , ** kwargs )
2281+                 except  TypeError :
2282+                     # Handle methods which are the source themselves 
2283+                     source  =  lambda  * _ : self .func (instance , * _ )
22782284                    val  =  Function (source , * args , ** kwargs )
2285+                 except  Exception :
2286+                     raise  Exception (
2287+                         "Could not create Function object from method " 
2288+                         f"{ self .func .__name__ }  
2289+                     )
2290+ 
22792291                val .__doc__  =  self .__doc__ 
22802292                cache [self .attrname ] =  val 
22812293            return  val 
0 commit comments