From 33b7ca0aac8d11aafe2518022e3606c0456c7ea6 Mon Sep 17 00:00:00 2001 From: Sean Breckenridge Date: Fri, 26 Mar 2021 16:07:21 -0700 Subject: [PATCH] warn and return on empty results when ordering --- my/core/query.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/my/core/query.py b/my/core/query.py index aa9aa72..54d6874 100644 --- a/my/core/query.py +++ b/my/core/query.py @@ -320,10 +320,14 @@ Will attempt to call iter() on the value""") order_by_chosen: Optional[OrderFunc] = order_by # if the user just supplied a function themselves if order_by is None: - # https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.spy - [first_item], itrc = more_itertools.spy(itr) - # replace the 'itr' in the higher scope with itrc -- itr is consumed by more_itertools.spy - itr = itrc + itr = more_itertools.peekable(itr) + try: + first_item = itr.peek() + except StopIteration: + low("""While determining order_key, encountered empty iterable. +Your 'src' may have been empty of the 'where' clause filtered the iterable to nothing""") + # 'itr' is an empty iterable + return itr # try to use a key, if it was supplied # order_key doesn't use local state - it just tries to find the passed # attribute, or default to the 'default' value. As mentioned above,