Skip to content

Fix lexer selection priority in get_lexer()#1888

Open
Synvoya wants to merge 1 commit into
httpie:masterfrom
Synvoya:fix/get-lexer-name-priority
Open

Fix lexer selection priority in get_lexer()#1888
Synvoya wants to merge 1 commit into
httpie:masterfrom
Synvoya:fix/get-lexer-name-priority

Conversation

@Synvoya

@Synvoya Synvoya commented Jul 1, 2026

Copy link
Copy Markdown

Summary

get_lexer() returns the wrong lexer for a structured-syntax MIME type with a + suffix when both the subtype name and the suffix resolve to a Pygments lexer — e.g. application/yaml+python highlights as Python instead of YAML.

Root cause

lexer_names is built name-first ([subtype_name, subtype_suffix]), and the sibling MIME-type loop breaks on the first match. But the lexer-name loop has no break:

for name in lexer_names:
    try:
        lexer = pygments.lexers.get_lexer_by_name(name)   # no break -> last wins
    except ClassNotFound:
        pass

so it keeps overwriting lexer and returns the last match (the suffix) instead of the first (the name).

Fix

Add the missing break, mirroring the MIME-type loop, so the first resolving name wins as intended.

Impact

Limited but real: the common structured-suffix types (+json, +xml, +yaml) are unaffected because their name part isn't a Pygments lexer, so first- and last-match coincided. The bug only surfaces when both the name and the suffix are distinct valid lexers.

Tests

Added a parametrized case to TestColors.test_get_lexer asserting application/yaml+pythonYAML (fails before the fix, passes after). TestColors: 15 passed; flake8 clean.

When a response's structured-syntax MIME type has a `+` suffix (e.g.
`application/yaml+python`) and both the subtype name and the suffix resolve to
a Pygments lexer, the subtype name should win: it is added to `lexer_names`
first and tried first, mirroring the sibling MIME-type loop above.

The lexer-name loop had no `break`, so it kept overwriting `lexer` and returned
the last match (the suffix) instead of the first. Add the missing `break`.

Realistic structured-suffix types (`+json`, `+xml`, `+yaml`) were unaffected
because their name part is not a lexer, so last- and first-match coincided; the
bug only surfaces when both parts are distinct valid lexers.
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.

1 participant