55This is an internal API not covered by versioning policy.
66"""
77
8+ import logging
89import re
910import traceback
1011import typing as t
1112from collections .abc import Iterable , Mapping , Sequence
1213
14+ logger = logging .getLogger (__name__ )
15+
1316T = t .TypeVar ("T" )
1417no_arg = object ()
1518
@@ -86,7 +89,7 @@ def keygetter(
8689 dct = getattr (dct , sub_field )
8790 except Exception as e :
8891 traceback .print_stack ()
89- print (f"Above error was { e } " )
92+ logger . debug (f"The above error was { e } " )
9093 return None
9194 return dct
9295
@@ -120,8 +123,9 @@ def parse_lookup(obj: Mapping[str, t.Any], path: str, lookup: str) -> t.Optional
120123 field_name = path .rsplit (lookup )[0 ]
121124 if field_name is not None :
122125 return keygetter (obj , field_name )
123- except Exception :
126+ except Exception as e :
124127 traceback .print_stack ()
128+ logger .debug (f"The above error was { e } " )
125129 return None
126130
127131
@@ -304,12 +308,12 @@ def lookup_iregex(
304308
305309
306310class PKRequiredException (Exception ):
307- def __init__ (self , * args : object ):
311+ def __init__ (self , * args : object ) -> None :
308312 return super ().__init__ ("items() require a pk_key exists" )
309313
310314
311315class OpNotFound (ValueError ):
312- def __init__ (self , op : str , * args : object ):
316+ def __init__ (self , op : str , * args : object ) -> None :
313317 return super ().__init__ (f"{ op } not in LOOKUP_NAME_MAP" )
314318
315319
@@ -470,7 +474,7 @@ def __init__(self, items: t.Optional["Iterable[T]"] = None) -> None:
470474
471475 def items (self ) -> list [tuple [str , T ]]:
472476 if self .pk_key is None :
473- raise PKRequiredException ()
477+ raise PKRequiredException
474478 return [(getattr (item , self .pk_key ), item ) for item in self ]
475479
476480 def __eq__ (
@@ -490,9 +494,8 @@ def __eq__(
490494 for key in a_keys :
491495 if abs (a [key ] - b [key ]) > 1 :
492496 return False
493- else :
494- if a != b :
495- return False
497+ elif a != b :
498+ return False
496499
497500 return True
498501 return False
@@ -529,8 +532,7 @@ def filter_lookup(obj: t.Any) -> bool:
529532 def val_match (obj : t .Union [str , list [t .Any ], T ]) -> bool :
530533 if isinstance (matcher , list ):
531534 return obj in matcher
532- else :
533- return bool (obj == matcher )
535+ return bool (obj == matcher )
534536
535537 _filter = val_match
536538 else :
@@ -546,9 +548,9 @@ def get(
546548 ) -> t .Optional [T ]:
547549 objs = self .filter (matcher = matcher , ** kwargs )
548550 if len (objs ) > 1 :
549- raise MultipleObjectsReturned ()
550- elif len (objs ) == 0 :
551+ raise MultipleObjectsReturned
552+ if len (objs ) == 0 :
551553 if default == no_arg :
552- raise ObjectDoesNotExist ()
554+ raise ObjectDoesNotExist
553555 return default
554556 return objs [0 ]
0 commit comments