@@ -44,7 +44,7 @@ def func():
44
44
)._to_string () == f"tail_recursive(func=" + repr (func ) + ").tail_call('first_arg', 2, [], first_kwarg='1', second_kwarg=2, third_kwarg={})"
45
45
46
46
47
- def test_nested_tail_call_mode_raises_exception_for_unknown_mode ():
47
+ def test_nested_tail_call_mode_raises_exception_for_unknown_feature_set ():
48
48
49
49
with pytest .raises (ValueError ) as excinfo :
50
50
@tail_recursive (feature_set = "not_a_feature_set" )
@@ -55,7 +55,7 @@ def _():
55
55
excinfo .value )
56
56
57
57
58
- def test_nested_tail_call_mode_converts_string_to_mode ():
58
+ def test_nested_tail_call_mode_converts_string_to_feature_set ():
59
59
60
60
@tail_recursive (feature_set = "full" )
61
61
def _ ():
@@ -380,3 +380,33 @@ def square_and_triangular_numbers(n):
380
380
381
381
assert square_and_triangular_numbers (3 ).square == 9
382
382
assert square_and_triangular_numbers (3 ).triangular == 6
383
+
384
+
385
+ def test_class_property ():
386
+ import functools
387
+
388
+ class MathStuff :
389
+
390
+ def __init__ (self , n ):
391
+ self .n = n
392
+
393
+ @tail_recursive (feature_set = "full" )
394
+ # Requires ``lru_cache`` because this version of fibonacci is highly inefficient.
395
+ @functools .lru_cache
396
+ def fibonacci (self , n ):
397
+ if n <= 1 :
398
+ return n
399
+ return self .fibonacci .tail_call (self , n - 1 ) + self .fibonacci .tail_call (self , n - 2 )
400
+
401
+ @property
402
+ @tail_recursive
403
+ def fib_of_n (self ):
404
+ return self .fibonacci .tail_call (self , self .n )
405
+
406
+ assert MathStuff (0 ).fib_of_n == non_recursive_fibonacci (0 ) == 0
407
+ assert MathStuff (1 ).fib_of_n == non_recursive_fibonacci (1 ) == 1
408
+ assert MathStuff (4 ).fib_of_n == non_recursive_fibonacci (4 ) == 3
409
+ assert MathStuff (7 ).fib_of_n == non_recursive_fibonacci (7 ) == 13
410
+
411
+ n = sys .getrecursionlimit () + 1
412
+ assert MathStuff (n ).fib_of_n == non_recursive_fibonacci (n )
0 commit comments