@@ -6,34 +6,21 @@ def apply_decorator_to_methods(
6
6
to all functions and coroutines within a class.
7
7
"""
8
8
9
- def is_private_method (attr_name , cls_name ):
10
- """Check if the attribute is a private method."""
11
- return attr_name .startswith (f"_{ cls_name } __" )
12
-
13
- def is_protected_method (attr_name , cls_name ):
14
- """Check if the attribute is a protected method."""
15
- return attr_name .startswith ("_" ) and not attr_name .startswith (f"_{ cls_name } __" )
16
-
17
9
def class_decorator (cls ):
18
- cls_name = cls .__name__
19
-
20
10
for attr_name , attr_value in cls .__dict__ .items ():
21
- # Skip attributes that are not callable
11
+ # Check if the attribute is a callable (method or coroutine)
22
12
if not callable (attr_value ):
23
13
continue
24
14
25
- # Check for private methods
26
- if is_private_method (attr_name , cls_name ):
15
+ if attr_name .startswith (f"_{ cls .__name__ } __" ):
27
16
if not private_methods :
28
17
continue
29
18
30
- # Check for protected methods
31
- elif is_protected_method (attr_name , cls_name ) and not protected_methods :
19
+ elif attr_name .startswith ("_" ) and not protected_methods :
32
20
continue
33
21
34
22
# Replace the original callable with the decorated version
35
23
setattr (cls , attr_name , decorator (attr_value ))
36
-
37
24
return cls
38
25
39
26
return class_decorator
0 commit comments