Merge pull request #2330 from terencehonles/fix-empty-deprecation-warning-in-q-node

fix self inflicted deprecation warnings in QNode
This commit is contained in:
Bastien Gérard 2020-05-19 22:02:12 +02:00 committed by GitHub
commit 4275c2d7b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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)