feat: q to bool - #2227
Merged
Merged
Conversation
waketzheng
reviewed
Jun 24, 2026
waketzheng
reviewed
Jun 24, 2026
waketzheng
reviewed
Jun 24, 2026
waketzheng
left a comment
Contributor
There was a problem hiding this comment.
It would be better to also update the changelog.
waketzheng
previously approved these changes
Jul 21, 2026
Contributor
|
@VladislavYar, this PR is helpful. Could you please fix the merge conflict in the CHANGELOG? |
Contributor
Author
|
@waketzheng done |
Added new feature to make Q objects falsy when empty.
waketzheng
approved these changes
Aug 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Added
__bool__support to theQclass, allowingQobjects to be evaluated in a boolean context.A
Qobject is considered truthy if it contains any filters or any non-empty children. An emptyQ()with no filters and no children evaluates toFalse.Motivation and Context
Previously, all
Qinstances were truthy by default (standard Python object behavior), making it impossible to distinguish between an emptyQ()and a meaningful one. This change enables natural boolean checks likeif q:to guard against applying empty query conditions.How Has This Been Tested?
Added
test_q_to_boolcovering four cases:Q(row="data")- truthy (has filters)Q()- falsy (no filters, no children)Q(Q(row="data"), Q(row="data"))- truthy (has non-empty children)Q(Q(), Q())- falsy (all children are empty)Checklist: