Skip to content

Commit

Permalink
refactor(query_list): Ruff fixes
Browse files Browse the repository at this point in the history
src/libvcs/_internal/query_list.py:87:9: TRY300 Consider moving this statement to an `else` block
src/libvcs/_internal/query_list.py:119:9: SIM102 Use a single `if` statement instead of nested `if` statements
src/libvcs/_internal/query_list.py:451:19: TRY002 Create your own exception
src/libvcs/_internal/query_list.py:451:19: TRY003 Avoid specifying long messages outside the exception class
src/libvcs/_internal/query_list.py:489:25: TRY301 Abstract `raise` to an inner function
src/libvcs/_internal/query_list.py:489:31: TRY003 Avoid specifying long messages outside the exception class
  • Loading branch information
tony committed Aug 20, 2023
1 parent 1c46479 commit 8c6bfb8
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/libvcs/_internal/query_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ def keygetter(
dct = dct[sub_field]
elif hasattr(dct, sub_field):
dct = getattr(dct, sub_field)
return dct
except Exception as e:
traceback.print_stack()
print(f"Above error was {e}")
return None
return None
return dct


def parse_lookup(obj: Mapping[str, Any], path: str, lookup: str) -> Optional[Any]:
Expand Down Expand Up @@ -117,7 +117,8 @@ def parse_lookup(obj: Mapping[str, Any], path: str, lookup: str) -> Optional[Any
"""
try:
if isinstance(path, str) and isinstance(lookup, str) and path.endswith(lookup):
if field_name := path.rsplit(lookup)[0]:
field_name = path.rsplit(lookup)[0]
if field_name is not None:
return keygetter(obj, field_name)
except Exception:
traceback.print_stack()
Expand Down Expand Up @@ -294,6 +295,16 @@ def lookup_iregex(
}


class PKRequiredException(Exception):
def __init__(self, *args: object):
return super().__init__("items() require a pk_key exists")


class OpNotFound(ValueError):
def __init__(self, op: str, *args: object):
return super().__init__(f"{op} not in LOOKUP_NAME_MAP")


class QueryList(list[T]):
"""Filter list of object/dictionaries. For small, local datasets.
Expand Down Expand Up @@ -448,7 +459,7 @@ class QueryList(list[T]):

def items(self) -> list[T]:
if self.pk_key is None:
raise Exception("items() require a pk_key exists")
raise PKRequiredException()
return [(getattr(item, self.pk_key), item) for item in self]

def __eq__(
Expand Down Expand Up @@ -486,7 +497,7 @@ def filter_lookup(obj: Any) -> bool:
lhs, op = path.rsplit("__", 1)

if op not in LOOKUP_NAME_MAP:
raise ValueError(f"{op} not in LOOKUP_NAME_MAP")
raise OpNotFound(op=op)
except ValueError:
lhs = path
op = "exact"
Expand Down

0 comments on commit 8c6bfb8

Please sign in to comment.