1+ from typing import Callable , Any
12import sys
23
34import pytest
78from .utility import sync
89
910
10- def method_counter (size ):
11+ class Counter :
12+ kind : object
13+ count : Any
14+
15+
16+ def method_counter (size : "int | None" ) -> "type[Counter]" :
1117 class Counter :
18+ kind = None
19+
1220 def __init__ (self ):
1321 self ._count = 0
1422
@@ -20,9 +28,10 @@ async def count(self):
2028 return Counter
2129
2230
23- def classmethod_counter (size ) :
31+ def classmethod_counter (size : "int | None" ) -> "type[Counter]" :
2432 class Counter :
2533 _count = 0
34+ kind = classmethod
2635
2736 def __init__ (self ):
2837 type(self )._count = 0
@@ -36,32 +45,40 @@ async def count(cls):
3645 return Counter
3746
3847
39- def staticmethod_counter (size ) :
48+ def staticmethod_counter (size : "int | None" ) -> "type[Counter]" :
4049 # I'm sorry for writing this test – please don't do this at home!
41- _count = 0
50+ count : int = 0
4251
4352 class Counter :
53+ kind = staticmethod
54+
4455 def __init__ (self ):
45- nonlocal _count
46- _count = 0
56+ nonlocal count
57+ count = 0
4758
4859 @staticmethod
4960 @a .lru_cache (maxsize = size )
5061 async def count ():
51- nonlocal _count
52- _count += 1
53- return _count
62+ nonlocal count
63+ count += 1
64+ return count
5465
5566 return Counter
5667
5768
58- counter_factories = [method_counter , classmethod_counter , staticmethod_counter ]
69+ counter_factories : "list[Callable[[int | None], type[Counter]]]" = [
70+ method_counter ,
71+ classmethod_counter ,
72+ staticmethod_counter ,
73+ ]
5974
6075
6176@pytest .mark .parametrize ("size" , [0 , 3 , 10 , None ])
6277@pytest .mark .parametrize ("counter_factory" , counter_factories )
6378@sync
64- async def test_method_plain (size , counter_factory ):
79+ async def test_method_plain (
80+ size : "int | None" , counter_factory : "Callable[[int | None], type[Counter]]"
81+ ):
6582 """Test caching without resetting"""
6683
6784 counter_type = counter_factory (size )
@@ -76,7 +93,9 @@ async def test_method_plain(size, counter_factory):
7693@pytest .mark .parametrize ("size" , [0 , 3 , 10 , None ])
7794@pytest .mark .parametrize ("counter_factory" , counter_factories )
7895@sync
79- async def test_method_clear (size , counter_factory ):
96+ async def test_method_clear (
97+ size : "int | None" , counter_factory : "Callable[[int | None], type[Counter]]"
98+ ):
8099 """Test caching with resetting everything"""
81100 counter_type = counter_factory (size )
82101 for _instance in range (4 ):
@@ -91,14 +110,16 @@ async def test_method_clear(size, counter_factory):
91110@pytest .mark .parametrize ("size" , [0 , 3 , 10 , None ])
92111@pytest .mark .parametrize ("counter_factory" , counter_factories )
93112@sync
94- async def test_method_discard (size , counter_factory ):
113+ async def test_method_discard (
114+ size : "int | None" , counter_factory : "Callable[[int | None], type[Counter]]"
115+ ):
95116 """Test caching with resetting specific item"""
96117 counter_type = counter_factory (size )
97- if (
98- sys .version_info < (3 , 9 )
99- and type ( counter_type .__dict__ [ "count" ]) is classmethod
118+ if not (
119+ ( 3 , 9 ) <= sys .version_info [: 2 ] <= (3 , 12 )
120+ or counter_type .kind is not classmethod
100121 ):
101- pytest .skip ("classmethod does not respect descriptors up to 3.8 " )
122+ pytest .skip ("classmethod only respects descriptors between 3.9 and 3.12 " )
102123 for _instance in range (4 ):
103124 instance = counter_type ()
104125 for reset in range (5 ):
@@ -111,7 +132,9 @@ async def test_method_discard(size, counter_factory):
111132@pytest .mark .parametrize ("size" , [0 , 3 , 10 , None ])
112133@pytest .mark .parametrize ("counter_factory" , counter_factories )
113134@sync
114- async def test_method_metadata (size , counter_factory ):
135+ async def test_method_metadata (
136+ size : "int | None" , counter_factory : "Callable[[int | None], type[Counter]]"
137+ ):
115138 """Test cache metadata on methods"""
116139 tp = counter_factory (size )
117140 for instance in range (4 ):
@@ -133,7 +156,7 @@ async def test_method_metadata(size, counter_factory):
133156
134157
135158@pytest .mark .parametrize ("size" , [None , 0 , 10 , 128 ])
136- def test_wrapper_attributes (size ):
159+ def test_wrapper_attributes (size : "int | None" ):
137160 class Bar :
138161 @a .lru_cache
139162 async def method (self , int_arg : int ):
0 commit comments