From cc7dff0c1072b36263e12db8fec11aba767c8535 Mon Sep 17 00:00:00 2001 From: SmartLamScott Date: Thu, 14 Aug 2025 08:23:01 -0500 Subject: [PATCH 1/2] only catch AttributeError in is_rule_valid, not general Exceptions --- src/object_filtering/object_filtering.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/object_filtering/object_filtering.py b/src/object_filtering/object_filtering.py index d3b0441..936fa87 100644 --- a/src/object_filtering/object_filtering.py +++ b/src/object_filtering/object_filtering.py @@ -218,7 +218,7 @@ def is_rule_valid(rule: dict, obj: Any = None) -> bool: raise FilterError("rule operator is not a valid operator.") try: # check if method exists method = getattr(obj, rule["criterion"]) - except: + except AttributeError: raise FilterError(f"method {rule['criterion']} does not exist in obj.") # check if method is decorated with @filter_criterion if not isinstance(obj, ObjectWrapper): @@ -685,11 +685,7 @@ def method(*args, **kwargs): raise AttributeError(f"Not all objects have the attribute '{name}'") else: if hasattr(self._obj, name): - attr = getattr(self._obj, name) # If the attribute is a method, return it directly - if callable(attr): - return attr - else: - return attr + return getattr(self._obj, name) else: raise AttributeError(f"'{type(self._obj).__name__}' object has no attribute '{name}'") From e23c0f471357d766d622cb8abc4241e4fd50e920 Mon Sep 17 00:00:00 2001 From: SmartLamScott Date: Thu, 14 Aug 2025 08:26:31 -0500 Subject: [PATCH 2/2] avoid try/except with hasattr --- src/object_filtering/object_filtering.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/object_filtering/object_filtering.py b/src/object_filtering/object_filtering.py index 936fa87..c3d6373 100644 --- a/src/object_filtering/object_filtering.py +++ b/src/object_filtering/object_filtering.py @@ -216,10 +216,9 @@ def is_rule_valid(rule: dict, obj: Any = None) -> bool: # value checks if rule["operator"].upper() not in VALID_OPERATORS: raise FilterError("rule operator is not a valid operator.") - try: # check if method exists - method = getattr(obj, rule["criterion"]) - except AttributeError: + if not hasattr(obj, rule["criterion"]): raise FilterError(f"method {rule['criterion']} does not exist in obj.") + method = getattr(obj, rule["criterion"]) # check if method is decorated with @filter_criterion if not isinstance(obj, ObjectWrapper): if callable(method) and not hasattr(method, "_is_whitelisted"):