Fix lexer selection priority in get_lexer()#1888
Open
Synvoya wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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+pythonhighlights as Python instead of YAML.Root cause
lexer_namesis built name-first ([subtype_name, subtype_suffix]), and the sibling MIME-type loopbreaks on the first match. But the lexer-name loop has nobreak:so it keeps overwriting
lexerand 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_lexerassertingapplication/yaml+python→YAML(fails before the fix, passes after).TestColors: 15 passed;flake8clean.