Skip to content

feat: q to bool - #2227

Merged
waketzheng merged 4 commits into
tortoise:developfrom
VladislavYar:q-to-bool
Aug 24, 2026
Merged

feat: q to bool#2227
waketzheng merged 4 commits into
tortoise:developfrom
VladislavYar:q-to-bool

Conversation

@VladislavYar

Copy link
Copy Markdown
Contributor

Description

Added __bool__ support to the Q class, allowing Q objects to be evaluated in a boolean context.

A Q object is considered truthy if it contains any filters or any non-empty children. An empty Q() with no filters and no children evaluates to False.

Motivation and Context

Previously, all Q instances were truthy by default (standard Python object behavior), making it impossible to distinguish between an empty Q() and a meaningful one. This change enables natural boolean checks like if q: to guard against applying empty query conditions.

How Has This Been Tested?

Added test_q_to_bool covering 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:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed-hq

codspeed-hq Bot commented Jun 24, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing VladislavYar:q-to-bool (ccf7d79) with develop (c5e5c4b)

Open in CodSpeed

Comment thread tests/test_q.py
Comment thread tortoise/expressions.py Outdated

@waketzheng waketzheng left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to also update the changelog.

waketzheng
waketzheng previously approved these changes Jul 21, 2026
@waketzheng

Copy link
Copy Markdown
Contributor

@VladislavYar, this PR is helpful. Could you please fix the merge conflict in the CHANGELOG?

@VladislavYar

Copy link
Copy Markdown
Contributor Author

@waketzheng done

Added new feature to make Q objects falsy when empty.
@waketzheng
waketzheng merged commit a324edc into tortoise:develop Aug 24, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants