[Pygments] Cleanup metaclass workarounds for mypy<1.1 (#15607)

This commit is contained in:
Brian Schubert
2026-04-05 11:45:24 -04:00
committed by GitHub
parent c6b7c0b144
commit e8d2bc6708
5 changed files with 39 additions and 50 deletions
+2 -19
View File
@@ -2,34 +2,17 @@
pygments.lexer.LexerMeta.__new__
pygments.style.StyleMeta.__new__
# Defined in lexer classes, intended to be used as static method, but doesn't use @staticmethod
pygments.lexer.Lexer(Meta)?.analyse_text
# Inheriting from tuple is weird
pygments.token._TokenType.__init__
# Cannot import in stubtest (SystemExit: 2)
pygments.__main__
# Class attributes that are typed in the metaclass instead. See comment in stubs.
# Class attributes that are set to None in the base class, but are
# always overridden with a non-None value in subclasses.
pygments.lexer.Lexer.name
pygments.lexer.Lexer.aliases
pygments.lexer.Lexer.filenames
pygments.lexer.Lexer.alias_filenames
pygments.lexer.Lexer.mimetypes
pygments.lexer.Lexer.priority
pygments.lexer.Lexer.url
pygments.lexer.Lexer.version_added
pygments.style.Style.background_color
pygments.style.Style.highlight_color
pygments.style.Style.line_number_color
pygments.style.Style.line_number_background_color
pygments.style.Style.line_number_special_color
pygments.style.Style.line_number_special_background_color
pygments.style.Style.styles
pygments.style.Style.name
pygments.style.Style.aliases
pygments.style.Style.web_style_gallery_exclude
# Individual lexers and styles submodules are not stubbed at this time.
pygments\.lexers\.(?!get_|find_|load_|guess_).*
@@ -0,0 +1,10 @@
from typing_extensions import assert_type
from pygments.style import Style, _StyleDict
from pygments.token import _TokenType
def test_style_class_iterable(style_class: type[Style]) -> None:
for t, d in style_class:
assert_type(t, _TokenType)
assert_type(d, _StyleDict)
+10 -10
View File
@@ -26,18 +26,16 @@ line_re: Final[Pattern[str]]
class LexerMeta(type):
def __new__(cls, name, bases, d): ...
def analyse_text(self, text: str) -> float: ... # actually defined in class Lexer
# ClassVars of Lexer, but same situation as with StyleMeta and Style
name: str # Set to None in Lexer, but always overridden with a non-None value in subclasses.
aliases: Sequence[str] # not intended mutable
filenames: Sequence[str]
alias_filenames: Sequence[str]
mimetypes: Sequence[str]
priority: float
url: str # Set to None in Lexer, but always overridden with a non-None value in subclasses.
version_added: str # Set to None in Lexer, but always overridden with a non-None value in subclasses.
class Lexer(metaclass=LexerMeta):
name: ClassVar[str] # Set to None, but always overridden with a non-None value in subclasses.
aliases: ClassVar[Sequence[str]] # not intended to be mutable
filenames: ClassVar[Sequence[str]]
alias_filenames: ClassVar[Sequence[str]]
mimetypes: ClassVar[Sequence[str]]
priority: ClassVar[float]
url: ClassVar[str] # Set to None, but always overridden with a non-None value in subclasses.
version_added: ClassVar[str] # Set to None, but always overridden with a non-None value in subclasses.
options: Incomplete
stripnl: Incomplete
stripall: Incomplete
@@ -47,6 +45,8 @@ class Lexer(metaclass=LexerMeta):
filters: Incomplete
def __init__(self, **options) -> None: ...
def add_filter(self, filter_, **options) -> None: ...
@staticmethod # @staticmethod added by special handling in metaclass
def analyse_text(text: str) -> float: ...
def get_tokens(self, text: str, unfiltered: bool = False) -> Iterator[tuple[_TokenType, str]]: ...
def get_tokens_unprocessed(self, text: str) -> Iterator[tuple[int, _TokenType, str]]: ...
+4 -4
View File
@@ -1,14 +1,14 @@
from _typeshed import FileDescriptorOrPath, StrPath
from collections.abc import Iterator
from pygments.lexer import Lexer, LexerMeta
from pygments.lexer import Lexer
def get_all_lexers(plugins: bool = True) -> Iterator[tuple[str, tuple[str, ...], tuple[str, ...], tuple[str, ...]]]: ...
def find_lexer_class(name: str) -> LexerMeta | None: ...
def find_lexer_class_by_name(_alias: str) -> LexerMeta: ...
def find_lexer_class(name: str) -> type[Lexer] | None: ...
def find_lexer_class_by_name(_alias: str) -> type[Lexer]: ...
def get_lexer_by_name(_alias: str, **options) -> Lexer: ...
def load_lexer_from_file(filename: FileDescriptorOrPath, lexername: str = "CustomLexer", **options) -> Lexer: ...
def find_lexer_class_for_filename(_fn: StrPath, code: str | bytes | None = None) -> LexerMeta | None: ...
def find_lexer_class_for_filename(_fn: StrPath, code: str | bytes | None = None) -> type[Lexer] | None: ...
def get_lexer_for_filename(_fn: StrPath, code: str | bytes | None = None, **options) -> Lexer: ...
def get_lexer_for_mimetype(_mime: str, **options) -> Lexer: ...
def guess_lexer_for_filename(_fn: StrPath, _text: str, **options) -> Lexer: ...
+13 -17
View File
@@ -1,5 +1,5 @@
from collections.abc import Iterator, Mapping, Set as AbstractSet
from typing import TypedDict, type_check_only
from collections.abc import Iterator, Mapping, Sequence, Set as AbstractSet
from typing import ClassVar, TypedDict, type_check_only
from pygments.token import _TokenType
@@ -26,19 +26,15 @@ class StyleMeta(type):
def list_styles(cls) -> list[tuple[_TokenType, _StyleDict]]: ...
def __iter__(cls) -> Iterator[tuple[_TokenType, _StyleDict]]: ...
def __len__(cls) -> int: ...
# These are a bit tricky.
# Technically should be ClassVar in class Style.
# But then we can't use StyleMeta to denote a style class.
# We need that because Type[Style] is not iterable, for example.
background_color: str
highlight_color: str
line_number_color: str
line_number_background_color: str
line_number_special_color: str
line_number_special_background_color: str
styles: Mapping[_TokenType, str] # not intended to be mutable
name: str
aliases: list[str]
web_style_gallery_exclude: bool
class Style(metaclass=StyleMeta): ...
class Style(metaclass=StyleMeta):
background_color: ClassVar[str]
highlight_color: ClassVar[str]
line_number_color: ClassVar[str]
line_number_background_color: ClassVar[str]
line_number_special_color: ClassVar[str]
line_number_special_background_color: ClassVar[str]
styles: ClassVar[Mapping[_TokenType, str]] # not intended to be mutable
name: ClassVar[str]
aliases: ClassVar[Sequence[str]] # not intended to be mutable
web_style_gallery_exclude: ClassVar[bool]