Skip to content

Ensure removing Abbreviations does not raise an error. - #1634

Merged
waylan merged 2 commits into
Python-Markdown:masterfrom
waylan:abbr
Sep 10, 2026
Merged

waylan merged 2 commits into
Python-Markdown:masterfrom
waylan:abbr

Conversation

@waylan

@waylan waylan commented Sep 10, 2026

Copy link
Copy Markdown
Member

In the documentation it is noted that an abbreviation defined in the global glossary can be removed for a specific document by assigning it to a blank value. However, if an abbreviation was not defined in a glossary, attempting to remove it would result in an error during rendering. The error is now avoided.

Description

AI Assistance Disclosure

  • No AI tools were used in preparing this PR.
  • If AI tools were used, I have disclosed which ones, and fully reviewed and verified their output.

Checklist

  • This PR follows the contribution guidelines.
  • The code follows the Code Style Guide.
  • The commit message follows the Commit Message Style Guide.
  • I have added or updated relevant docs, including release notes if applicable which follow the [Documentation Style Guide](Documentation Style Guide).
  • I have added or updated relevant tests.
  • I have not requested, and will not request, an automated AI review for this PR.

In the documentation it is noted that an abbreviation defined in the global
glossary can be removed for a specific document by assigning it to a blank
value. However, if an abbreviation was not defined in a glossary, attempting
to remove it would result in an error during rendering. The error is now avoided.
@waylan

waylan commented Sep 10, 2026 •

Copy link
Copy Markdown
Member Author

Prior to the code change, the new test would fail.

ERROR: test_abbr_override_not_in_glossary (test_syntax.extensions.test_abbr.TestAbbr.test_abbr_override_not_in_glossary)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\code\md\tests\test_syntax\extensions\test_abbr.py", line 246, in test_abbr_override_not_in_glossary
    self.assertMarkdownRenders(
  File "C:\code\md\markdown\test_tools.py", line 76, in assertMarkdownRenders
    output = md.convert(source)
             ^^^^^^^^^^^^^^^^^^
  File "C:\code\md\markdown\core.py", line 363, in convert
    root = self.parser.parseDocument(self.lines).getroot()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\code\md\markdown\blockparser.py", line 117, in parseDocument
    self.parseChunk(self.root, '\n'.join(lines))
  File "C:\code\md\markdown\blockparser.py", line 136, in parseChunk
    self.parseBlocks(parent, text.split('\n\n'))
  File "C:\code\md\markdown\blockparser.py", line 158, in parseBlocks
    if processor.run(parent, blocks) is not False:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\code\md\markdown\extensions\abbr.py", line 165, in run
    self.abbrs.pop(abbr)
KeyError: 'HTML'

Or here is a console session:

>>> import markdown
>>> markdown.markdown('HTML', extensions=['abbr'], extension_configs={'abbr': {'glossary': {'HTML': 'Hyper Text Markup Language'}}})
'<p><abbr title="Hyper Text Markup Language">HTML</abbr></p>'
>>> markdown.markdown('HTML\n*[HTML]: ""', extensions=['abbr'], extension_configs={'abbr': {'glossary': {'HTML': 'Hyper Text Markup Language'}}})
'<p>HTML</p>'
>>> markdown.markdown('HTML\n*[HTML]: ""', extensions=['abbr'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\code\md\markdown\core.py", line 502, in markdown
    return md.convert(text)
           ^^^^^^^^^^^^^^^^
  File "C:\code\md\markdown\core.py", line 363, in convert
    root = self.parser.parseDocument(self.lines).getroot()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\code\md\markdown\blockparser.py", line 117, in parseDocument
    self.parseChunk(self.root, '\n'.join(lines))
  File "C:\code\md\markdown\blockparser.py", line 136, in parseChunk
    self.parseBlocks(parent, text.split('\n\n'))
  File "C:\code\md\markdown\blockparser.py", line 158, in parseBlocks
    if processor.run(parent, blocks) is not False:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\code\md\markdown\extensions\abbr.py", line 165, in run
    self.abbrs.pop(abbr)
KeyError: 'HTML'

@waylan
waylan merged commit 175fb5a into Python-Markdown:master Sep 10, 2026
14 checks passed
@waylan
waylan deleted the abbr branch September 10, 2026 15:10
@waylan waylan added bug Bug report. extension Related to one or more of the included extensions. labels Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Bug report. extension Related to one or more of the included extensions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant