Skip to content

Commit ba23058

Browse files
committed
refactor(query_list): Extract exceptions
1 parent a43136e commit ba23058

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/libtmux/_internal/query_list.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ def keygetter(
5959
elif hasattr(dct, sub_field):
6060
dct = getattr(dct, sub_field)
6161

62-
return dct
6362
except Exception as e:
6463
traceback.print_stack()
6564
print(f"Above error was {e}")
66-
return None
65+
return None
66+
67+
return dct
6768

6869

6970
def parse_lookup(obj: "Mapping[str, Any]", path: str, lookup: str) -> Optional[Any]:
@@ -244,6 +245,16 @@ def lookup_iregex(
244245
}
245246

246247

248+
class PKRequiredException(Exception):
249+
def __init__(self, *args: object):
250+
return super().__init__("items() require a pk_key exists")
251+
252+
253+
class OpNotFound(ValueError):
254+
def __init__(self, op: str, *args: object):
255+
return super().__init__(f"{op} not in LOOKUP_NAME_MAP")
256+
257+
247258
class QueryList(List[T]):
248259
"""Filter list of object/dictionaries. For small, local datasets.
249260
@@ -284,7 +295,7 @@ class QueryList(List[T]):
284295

285296
def items(self) -> List[T]:
286297
if self.pk_key is None:
287-
raise Exception("items() require a pk_key exists")
298+
raise PKRequiredException()
288299
return [(getattr(item, self.pk_key), item) for item in self]
289300

290301
def __eq__(
@@ -324,7 +335,7 @@ def filter_lookup(obj: Any) -> bool:
324335
lhs, op = path.rsplit("__", 1)
325336

326337
if op not in LOOKUP_NAME_MAP:
327-
raise ValueError(f"{op} not in LOOKUP_NAME_MAP")
338+
raise OpNotFound(op=op)
328339
except ValueError:
329340
lhs = path
330341
op = "exact"

0 commit comments

Comments
 (0)