Skip to content

Commit b169297

Browse files
committed
[Pygments] Cleanup metaclass workarounds for mypy<1.1
1 parent 97d1089 commit b169297

File tree

5 files changed

+39
-50
lines changed

5 files changed

+39
-50
lines changed

stubs/Pygments/@tests/stubtest_allowlist.txt

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,17 @@
22
pygments.lexer.LexerMeta.__new__
33
pygments.style.StyleMeta.__new__
44

5-
# Defined in lexer classes, intended to be used as static method, but doesn't use @staticmethod
6-
pygments.lexer.Lexer(Meta)?.analyse_text
7-
85
# Inheriting from tuple is weird
96
pygments.token._TokenType.__init__
107

118
# Cannot import in stubtest (SystemExit: 2)
129
pygments.__main__
1310

14-
# Class attributes that are typed in the metaclass instead. See comment in stubs.
11+
# Class attributes that are set to None in the base class, but are
12+
# always overridden with a non-None value in subclasses.
1513
pygments.lexer.Lexer.name
16-
pygments.lexer.Lexer.aliases
17-
pygments.lexer.Lexer.filenames
18-
pygments.lexer.Lexer.alias_filenames
19-
pygments.lexer.Lexer.mimetypes
20-
pygments.lexer.Lexer.priority
2114
pygments.lexer.Lexer.url
2215
pygments.lexer.Lexer.version_added
23-
pygments.style.Style.background_color
24-
pygments.style.Style.highlight_color
25-
pygments.style.Style.line_number_color
26-
pygments.style.Style.line_number_background_color
27-
pygments.style.Style.line_number_special_color
28-
pygments.style.Style.line_number_special_background_color
29-
pygments.style.Style.styles
30-
pygments.style.Style.name
31-
pygments.style.Style.aliases
32-
pygments.style.Style.web_style_gallery_exclude
3316

3417
# Individual lexers and styles submodules are not stubbed at this time.
3518
pygments\.lexers\.(?!get_|find_|load_|guess_).*
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from typing_extensions import assert_type
2+
3+
from pygments.style import Style, _StyleDict
4+
from pygments.token import _TokenType
5+
6+
7+
def test_style_class_iterable(style_class: type[Style]) -> None:
8+
for t, d in style_class:
9+
assert_type(t, _TokenType)
10+
assert_type(d, _StyleDict)

stubs/Pygments/pygments/lexer.pyi

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,16 @@ line_re: Final[Pattern[str]]
2626

2727
class LexerMeta(type):
2828
def __new__(cls, name, bases, d): ...
29-
def analyse_text(self, text: str) -> float: ... # actually defined in class Lexer
30-
# ClassVars of Lexer, but same situation as with StyleMeta and Style
31-
name: str # Set to None in Lexer, but always overridden with a non-None value in subclasses.
32-
aliases: Sequence[str] # not intended mutable
33-
filenames: Sequence[str]
34-
alias_filenames: Sequence[str]
35-
mimetypes: Sequence[str]
36-
priority: float
37-
url: str # Set to None in Lexer, but always overridden with a non-None value in subclasses.
38-
version_added: str # Set to None in Lexer, but always overridden with a non-None value in subclasses.
3929

4030
class Lexer(metaclass=LexerMeta):
31+
name: ClassVar[str] # Set to None, but always overridden with a non-None value in subclasses.
32+
aliases: ClassVar[Sequence[str]] # not intended to be mutable
33+
filenames: ClassVar[Sequence[str]]
34+
alias_filenames: ClassVar[Sequence[str]]
35+
mimetypes: ClassVar[Sequence[str]]
36+
priority: ClassVar[float]
37+
url: ClassVar[str] # Set to None, but always overridden with a non-None value in subclasses.
38+
version_added: ClassVar[str] # Set to None, but always overridden with a non-None value in subclasses.
4139
options: Incomplete
4240
stripnl: Incomplete
4341
stripall: Incomplete
@@ -47,6 +45,8 @@ class Lexer(metaclass=LexerMeta):
4745
filters: Incomplete
4846
def __init__(self, **options) -> None: ...
4947
def add_filter(self, filter_, **options) -> None: ...
48+
@staticmethod
49+
def analyse_text(text: str) -> float: ...
5050
def get_tokens(self, text: str, unfiltered: bool = False) -> Iterator[tuple[_TokenType, str]]: ...
5151
def get_tokens_unprocessed(self, text: str) -> Iterator[tuple[int, _TokenType, str]]: ...
5252

stubs/Pygments/pygments/lexers/__init__.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from _typeshed import FileDescriptorOrPath, StrPath
22
from collections.abc import Iterator
33

4-
from pygments.lexer import Lexer, LexerMeta
4+
from pygments.lexer import Lexer
55

66
def get_all_lexers(plugins: bool = True) -> Iterator[tuple[str, tuple[str, ...], tuple[str, ...], tuple[str, ...]]]: ...
7-
def find_lexer_class(name: str) -> LexerMeta | None: ...
8-
def find_lexer_class_by_name(_alias: str) -> LexerMeta: ...
7+
def find_lexer_class(name: str) -> type[Lexer] | None: ...
8+
def find_lexer_class_by_name(_alias: str) -> type[Lexer]: ...
99
def get_lexer_by_name(_alias: str, **options) -> Lexer: ...
1010
def load_lexer_from_file(filename: FileDescriptorOrPath, lexername: str = "CustomLexer", **options) -> Lexer: ...
11-
def find_lexer_class_for_filename(_fn: StrPath, code: str | bytes | None = None) -> LexerMeta | None: ...
11+
def find_lexer_class_for_filename(_fn: StrPath, code: str | bytes | None = None) -> type[Lexer] | None: ...
1212
def get_lexer_for_filename(_fn: StrPath, code: str | bytes | None = None, **options) -> Lexer: ...
1313
def get_lexer_for_mimetype(_mime: str, **options) -> Lexer: ...
1414
def guess_lexer_for_filename(_fn: StrPath, _text: str, **options) -> Lexer: ...

stubs/Pygments/pygments/style.pyi

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from collections.abc import Iterator, Mapping, Set as AbstractSet
2-
from typing import TypedDict, type_check_only
1+
from collections.abc import Iterator, Mapping, Sequence, Set as AbstractSet
2+
from typing import ClassVar, TypedDict, type_check_only
33

44
from pygments.token import _TokenType
55

@@ -26,19 +26,15 @@ class StyleMeta(type):
2626
def list_styles(cls) -> list[tuple[_TokenType, _StyleDict]]: ...
2727
def __iter__(cls) -> Iterator[tuple[_TokenType, _StyleDict]]: ...
2828
def __len__(cls) -> int: ...
29-
# These are a bit tricky.
30-
# Technically should be ClassVar in class Style.
31-
# But then we can't use StyleMeta to denote a style class.
32-
# We need that because Type[Style] is not iterable, for example.
33-
background_color: str
34-
highlight_color: str
35-
line_number_color: str
36-
line_number_background_color: str
37-
line_number_special_color: str
38-
line_number_special_background_color: str
39-
styles: Mapping[_TokenType, str] # not intended to be mutable
40-
name: str
41-
aliases: list[str]
42-
web_style_gallery_exclude: bool
4329

44-
class Style(metaclass=StyleMeta): ...
30+
class Style(metaclass=StyleMeta):
31+
background_color: ClassVar[str]
32+
highlight_color: ClassVar[str]
33+
line_number_color: ClassVar[str]
34+
line_number_background_color: ClassVar[str]
35+
line_number_special_color: ClassVar[str]
36+
line_number_special_background_color: ClassVar[str]
37+
styles: ClassVar[Mapping[_TokenType, str]] # not intended to be mutable
38+
name: ClassVar[str]
39+
aliases: ClassVar[Sequence[str]] # not intended to be mutable
40+
web_style_gallery_exclude: ClassVar[bool]

0 commit comments

Comments
 (0)