Skip to content

Fix boolean fenced code attributes without codehilite - #1718

Open
ARAYN7011 wants to merge 2 commits into
Python-Markdown:masterfrom
ARAYN7011:fix-fenced-code-bool-attrs-1713
Open

ARAYN7011 wants to merge 2 commits into
Python-Markdown:masterfrom
ARAYN7011:fix-fenced-code-bool-attrs-1713

Conversation

@ARAYN7011

Copy link
Copy Markdown

Description

Fixes #1713. When fenced_code and attr_list are enabled but codehilite is not, boolean options such as linenums=true were passed to _escape_attrib_html as bool objects, raising TypeError: cannot serialize True. Following the plan in the issue thread, values are now serialized as the serializer would: None -> key; True -> bare key for HTML boolean attributes with html output, otherwise key="key"; False -> omitted.

Test plan: added two tests in tests/test_syntax/extensions/test_fenced_code.py (fail before, pass after); full python -m unittest discover tests passes; flake8 clean on changed files.

AI Assistance Disclosure

  • No AI tools were used in preparing this PR.

  • AI tools were used and I have disclosed which ones, and have fully reviewed and verified their output.

    AI tools were use to assist with the following:

    • Code
    • Code Comments
    • Documentation
    • Changelog Entry
    • Commit Message
    • The Description Above
    • Other: tests (Claude Code)

Checklist

@waylan

waylan commented Nov 19, 2026

Copy link
Copy Markdown
Member

This needs to be compared to #1719. Both claim to fix #1713.

@waylan waylan added the needs-review Needs to be reviewed and/or approved. label Nov 19, 2026
@ARAYN7011

Copy link
Copy Markdown
Author

Thanks @waylan. For comparison: #1719 is essentially the same fix (a helper that serializes None/True/False like the serializer, using BOOLEAN_ATTRIBUTES for the html output format), so the output should be equivalent. This PR was opened first (2026-11-18 05:53 UTC) and sanitize_name is applied once inside the helper. Either is fine with me; I'm happy to adjust this one to match any preference, or close it if you'd rather take #1719.

@ARAYN7011
ARAYN7011 force-pushed the fix-fenced-code-bool-attrs-1713 branch from f229286 to 3143b71 Compare November 25, 2026 12:41
@waylan

waylan commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Both fixes are essentially the same and either would be fine. However, neither includes comprehensive tests which cover every possible scenario. That said, #1719 includes more comprehensive tests that this does.

@waylan waylan added requires-changes Awaiting updates after a review. and removed needs-review Needs to be reviewed and/or approved. labels Nov 25, 2026
ARAYN7011 and others added 2 commits November 25, 2026 20:01
Boolean options (e.g. linenums) set via attr_list were passed to the
escape helper as bool objects, raising TypeError when codehilite is not
enabled. Serialize them the way the serializer does.

Fixes Python-Markdown#1713.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@ARAYN7011
ARAYN7011 force-pushed the fix-fenced-code-bool-attrs-1713 branch from 3143b71 to 7e72636 Compare November 25, 2026 14:32
@ARAYN7011

Copy link
Copy Markdown
Author

Thanks @waylan. I rebased onto current master (changelog conflict) and added tests for: html output format, a valueless attribute, and mixed boolean/string/escaped values (7e72636). The full suite passes (1233 tests). If you'd rather take #1719, that's fine with me.

@waylan waylan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's a good start. However, that only tests output_format=xhtml. We would need the same set of tests with output_format=html as well.

),
self.dedent(
'''
<pre><code class="language-py" linenums="linenums" title="a &amp; b" nowrap="false"># Some python code

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is nowrap="false" being passed through here? That seems wrong to me. The test above (line 477) does not pass through the key with a false value, which is expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

requires-changes Awaiting updates after a review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fenced_code + attr_list: boolean options like linenums crash with "cannot serialize" when codehilite is not enabled

2 participants