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