-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Infra: Add custom lexers for PEP 823 + 824 #5125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cdce8p
wants to merge
6
commits into
python:main
Choose a base branch
from
cdce8p:lexers-823+824
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
750b08d
Add custom lexers for PEP 823 + 824
cdce8p 2c1bf24
Register lexers automatically
cdce8p 03a8225
Fix ruff
cdce8p b52f984
Merge branch 'main'
cdce8p 35b35e0
Use custom lexers for 823 + 824
cdce8p 8fc770a
Add notes to file headers
cdce8p File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # This file is placed in the public domain or under the | ||
| # CC0-1.0-Universal license, whichever is more permissive. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import importlib | ||
| import pkgutil | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pygments.lexer import Lexer | ||
|
|
||
|
|
||
| def get_custom_lexers() -> list[type[Lexer]]: | ||
| lexers: list[type[Lexer]] = [] | ||
| for module_info in pkgutil.walk_packages(__path__, prefix=f"{__name__}."): | ||
| module = importlib.import_module(module_info.name) | ||
| if (register_func := getattr(module, "register", None)) is None: | ||
| continue | ||
| lexers.extend(register_func()) | ||
| return 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # This file is placed in the public domain or under the | ||
| # CC0-1.0-Universal license, whichever is more permissive. | ||
|
|
||
| """Custom lexer for PEP 823.""" | ||
|
|
||
| # This lexer is most useful while PEP 823 is being discussed. | ||
| # If it breaks in the future, it can be safely replaced by plain | ||
| # Python or text lexers. | ||
|
|
||
| from pygments.lexer import DelegatingLexer, inherit | ||
| from pygments.lexers.python import ( | ||
| PythonLexer, | ||
| PythonTracebackLexer, | ||
| _PythonConsoleLexerBase, | ||
| ) | ||
| from pygments.token import Operator, Other | ||
|
|
||
|
|
||
| class Py823Lexer(PythonLexer): | ||
| name = "py823" | ||
|
|
||
| tokens = { | ||
| "expr": [ | ||
| (r"maybe\b", Operator.Word), | ||
| (r"\?", Operator), | ||
| inherit, | ||
| ], | ||
| } | ||
|
|
||
|
|
||
| class Py823ConsoleLexer(DelegatingLexer): | ||
| name = "py823-console" | ||
|
|
||
| def __init__(self, **options): | ||
| pylexer = Py823Lexer | ||
| tblexer = PythonTracebackLexer | ||
|
|
||
| class _ReplaceInnerCode(DelegatingLexer): | ||
| def __init__(self, **options): | ||
| super().__init__( | ||
| pylexer, _PythonConsoleLexerBase, Other.Code, **options | ||
| ) | ||
|
|
||
| super().__init__(tblexer, _ReplaceInnerCode, Other.Traceback, **options) | ||
|
|
||
|
|
||
| def register(): | ||
| return [Py823Lexer, Py823ConsoleLexer] | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # This file is placed in the public domain or under the | ||
| # CC0-1.0-Universal license, whichever is more permissive. | ||
|
|
||
| """Custom lexer for PEP 824.""" | ||
|
|
||
| # This lexer is most useful while PEP 824 is being discussed. | ||
| # If it breaks in the future, it can be safely replaced by plain | ||
| # Python or text lexers. | ||
|
|
||
| from pygments.lexer import inherit | ||
| from pygments.lexers.python import PythonLexer | ||
| from pygments.token import Operator | ||
|
|
||
|
|
||
| class Py824Lexer(PythonLexer): | ||
| name = "py824" | ||
|
|
||
| tokens = { | ||
| "expr": [ | ||
| (r"\?\?(?=\s)", Operator.Word), | ||
| (r"otherwise\b", Operator.Word), | ||
| (r"\?", Operator), | ||
| inherit, | ||
| ], | ||
| } | ||
|
|
||
|
|
||
| def register(): | ||
| return [Py824Lexer] |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is likely to only break in the far future, when a new generation of maintainers, let's note explicitly (and in each file) that is can be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 8fc770a.
For 823 and 824 in particular the current plain Python lexer will crash outright since those add new operators. That's one of the reasons why I started looking into it in the first place actually. If the custom lexer needs to be removed in the future, it would be fine to just replace it with the default text lexer like it is now.