Skip to content

Add doc[bool] conditional no-op toggle for with statements - #15

Merged
Tronic merged 2 commits into
mainfrom
nullbuilder
Jul 9, 2026
Merged

Tronic merged 2 commits into
mainfrom
nullbuilder

Conversation

@Tronic

@Tronic Tronic commented Jul 9, 2026

Copy link
Copy Markdown
Member

The Python with statement cannot be made conditional due to syntax. This PR adds another overload for the [] operator with bool argument to act as such toggle. Using doc[True] does nothing and processing after it continues normally. doc[False] returns a NullBuilder that does absolutely nothing with anything done to it, in effect discarding further operations on the same statement and with statements with it.

with doc[is_enabled].strong:
    doc("always added, sometimes strong")

Copilot AI review requested due to automatic review settings July 9, 2026 00:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a conditional “no-op” toggle for the [] operator so doc[True] behaves normally while doc[False] returns a builder-like object that absorbs chained calls and with blocks, enabling conditional tags without conditional syntax around with.

Changes:

  • Added NullBuilder (a no-op context manager/builder) to swallow chained operations when a condition is False.
  • Extended Builder.__getitem__ to accept bool and return NullBuilder for False (or self for True).
  • Added documentation and tests covering conditional tags, chaining behavior, and NullBuilder behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
tests/test_html5tagger.py Adds tests for doc[bool] conditional behavior and NullBuilder semantics.
README.md Documents the new “Conditional elements” feature with an example.
html5tagger/nullbuilder.py Introduces NullBuilder implementation used for the no-op path.
html5tagger/builder.py Implements bool handling in __getitem__ and wires in NullBuilder.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread html5tagger/nullbuilder.py
@Tronic
Tronic merged commit 97d2bbb into main Jul 9, 2026
7 checks passed
@Tronic
Tronic deleted the nullbuilder branch July 9, 2026 01:02
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