fix self inflicted deprecation warnings in QNode

This commit is contained in:
Terence D. Honles 2020-05-19 11:00:30 -07:00
parent 3db9d58dac
commit 22bff8566d

View File

@ -7,6 +7,11 @@ from mongoengine.queryset import transform
__all__ = ("Q", "QNode") __all__ = ("Q", "QNode")
def warn_empty_is_deprecated():
msg = "'empty' property is deprecated in favour of using 'not bool(filter)'"
warnings.warn(msg, DeprecationWarning, stacklevel=2)
class QNodeVisitor: class QNodeVisitor:
"""Base visitor class for visiting Q-object nodes in a query tree. """Base visitor class for visiting Q-object nodes in a query tree.
""" """
@ -98,19 +103,18 @@ class QNode:
object. object.
""" """
# If the other Q() is empty, ignore it and just use `self`. # If the other Q() is empty, ignore it and just use `self`.
if getattr(other, "empty", True): if not bool(other):
return self return self
# Or if this Q is empty, ignore it and just use `other`. # Or if this Q is empty, ignore it and just use `other`.
if self.empty: if not bool(self):
return other return other
return QCombination(operation, [self, other]) return QCombination(operation, [self, other])
@property @property
def empty(self): def empty(self):
msg = "'empty' property is deprecated in favour of using 'not bool(filter)'" warn_empty_is_deprecated()
warnings.warn(msg, DeprecationWarning)
return False return False
def __or__(self, other): def __or__(self, other):
@ -152,8 +156,7 @@ class QCombination(QNode):
@property @property
def empty(self): def empty(self):
msg = "'empty' property is deprecated in favour of using 'not bool(filter)'" warn_empty_is_deprecated()
warnings.warn(msg, DeprecationWarning)
return not bool(self.children) return not bool(self.children)
def __eq__(self, other): def __eq__(self, other):
@ -186,4 +189,5 @@ class Q(QNode):
@property @property
def empty(self): def empty(self):
warn_empty_is_deprecated()
return not bool(self.query) return not bool(self.query)