Skip to content

Commit 2d4d670

Browse files
committed
Attempt to reduce cognitive complexity
1 parent 0fadae5 commit 2d4d670

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/common/utils.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,34 @@ def apply_decorator_to_methods(
66
to all functions and coroutines within a class.
77
"""
88

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+
917
def class_decorator(cls):
18+
cls_name = cls.__name__
19+
1020
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
1222
if not callable(attr_value):
1323
continue
1424

15-
if attr_name.startswith(f"_{cls.__name__}__"):
25+
# Check for private methods
26+
if is_private_method(attr_name, cls_name):
1627
if not private_methods:
1728
continue
1829

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:
2032
continue
2133

2234
# Replace the original callable with the decorated version
2335
setattr(cls, attr_name, decorator(attr_value))
36+
2437
return cls
2538

2639
return class_decorator

0 commit comments

Comments
 (0)