Skip to content

Commit 4495b83

Browse files
[Pygments] Complete stubs for Formatter (#15608)
1 parent e8d2bc6 commit 4495b83

File tree

13 files changed

+108
-104
lines changed

13 files changed

+108
-104
lines changed

stubs/Pygments/@tests/stubtest_allowlist.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ pygments.lexer.Lexer.name
1414
pygments.lexer.Lexer.url
1515
pygments.lexer.Lexer.version_added
1616

17+
# Class attributes that are set to None in the base class, but are
18+
# always overridden with a non-None value in subclasses.
19+
pygments.formatter.Formatter.name
20+
1721
# Individual lexers and styles submodules are not stubbed at this time.
1822
pygments\.lexers\.(?!get_|find_|load_|guess_).*
1923
pygments\.styles\.(?!get_).*
Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,58 @@
11
import types
2-
from _typeshed import Incomplete
3-
from typing import Any, Generic, TypeVar, overload
2+
from _typeshed import SupportsWrite
3+
from collections.abc import Iterable, Sequence
4+
from typing import Any, ClassVar, Generic, TypeVar, overload
5+
6+
from pygments.style import Style
7+
from pygments.token import _TokenType
48

59
_T = TypeVar("_T", str, bytes)
610

711
__all__ = ["Formatter"]
812

913
class Formatter(Generic[_T]):
10-
name: Incomplete
11-
aliases: Incomplete
12-
filenames: Incomplete
13-
unicodeoutput: bool
14-
style: Incomplete
15-
full: Incomplete
16-
title: Incomplete
17-
encoding: Incomplete
18-
options: Incomplete
14+
name: ClassVar[str] # Set to None, but always overridden with a non-None value in subclasses.
15+
aliases: ClassVar[Sequence[str]] # Not intended to be mutable
16+
filenames: ClassVar[Sequence[str]] # Not intended to be mutable
17+
unicodeoutput: ClassVar[bool]
18+
style: type[Style]
19+
full: bool
20+
title: str
21+
encoding: str | None
22+
options: dict[str, Any] # arbitrary values used by subclasses
1923
@overload
20-
def __init__(self: Formatter[str], *, encoding: None = None, outencoding: None = None, **options) -> None: ...
24+
def __init__(
25+
self: Formatter[str],
26+
*,
27+
style: type[Style] | str = "default",
28+
full: bool = False,
29+
title: str = "",
30+
encoding: None = None,
31+
outencoding: None = None,
32+
**options: Any, # arbitrary values used by subclasses
33+
) -> None: ...
2134
@overload
22-
def __init__(self: Formatter[bytes], *, encoding: str, outencoding: None = None, **options) -> None: ...
35+
def __init__(
36+
self: Formatter[bytes],
37+
*,
38+
style: type[Style] | str = "default",
39+
full: bool = False,
40+
title: str = "",
41+
encoding: str,
42+
outencoding: None = None,
43+
**options: Any, # arbitrary values used by subclasses
44+
) -> None: ...
2345
@overload
24-
def __init__(self: Formatter[bytes], *, encoding: None = None, outencoding: str, **options) -> None: ...
46+
def __init__(
47+
self: Formatter[bytes],
48+
*,
49+
style: type[Style] | str = "default",
50+
full: bool = False,
51+
title: str = "",
52+
encoding: None = None,
53+
outencoding: str,
54+
**options: Any, # arbitrary values used by subclasses
55+
) -> None: ...
2556
def __class_getitem__(cls, name: Any) -> types.GenericAlias: ...
26-
def get_style_defs(self, arg: str = ""): ...
27-
def format(self, tokensource, outfile): ...
57+
def get_style_defs(self, arg: str = "") -> str: ...
58+
def format(self, tokensource: Iterable[tuple[_TokenType, str]], outfile: SupportsWrite[_T]) -> None: ...
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
from _typeshed import Incomplete
1+
from _typeshed import SupportsWrite
2+
from collections.abc import Iterable
23
from typing import TypeVar
34

45
from pygments.formatter import Formatter
6+
from pygments.token import _TokenType
57

68
_T = TypeVar("_T", str, bytes)
79

810
__all__ = ["BBCodeFormatter"]
911

1012
class BBCodeFormatter(Formatter[_T]):
11-
name: str
12-
aliases: Incomplete
13-
filenames: Incomplete
14-
styles: Incomplete
15-
def format_unencoded(self, tokensource, outfile) -> None: ...
13+
styles: dict[_TokenType, tuple[str, str]]
14+
def format_unencoded(self, tokensource: Iterable[tuple[_TokenType, str]], outfile: SupportsWrite[str]) -> None: ...

stubs/Pygments/pygments/formatters/html.pyi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
from _typeshed import Incomplete
1+
from _typeshed import Incomplete, SupportsWrite
2+
from collections.abc import Iterable
23
from typing import TypeVar
34

45
from pygments.formatter import Formatter
6+
from pygments.token import _TokenType
57

68
_T = TypeVar("_T", str, bytes)
79

810
__all__ = ["HtmlFormatter"]
911

1012
class HtmlFormatter(Formatter[_T]):
11-
name: str
12-
aliases: Incomplete
13-
filenames: Incomplete
1413
title: Incomplete
1514
nowrap: Incomplete
1615
noclasses: Incomplete
@@ -41,4 +40,4 @@ class HtmlFormatter(Formatter[_T]):
4140
def get_linenos_style_defs(self): ...
4241
def get_css_prefix(self, arg): ...
4342
def wrap(self, source): ...
44-
def format_unencoded(self, tokensource, outfile) -> None: ...
43+
def format_unencoded(self, tokensource: Iterable[tuple[_TokenType, str]], outfile: SupportsWrite[str]) -> None: ...

stubs/Pygments/pygments/formatters/img.pyi

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from _typeshed import Incomplete
2-
from typing import TypeVar
1+
from _typeshed import Incomplete, SupportsWrite
2+
from collections.abc import Iterable
3+
from typing_extensions import Never
34

45
from pygments.formatter import Formatter
5-
6-
_T = TypeVar("_T", str, bytes)
6+
from pygments.token import _TokenType
77

88
__all__ = ["ImageFormatter", "GifImageFormatter", "JpgImageFormatter", "BmpImageFormatter"]
99

@@ -22,11 +22,7 @@ class FontManager:
2222
def get_font(self, bold, oblique): ...
2323
def get_style(self, style): ...
2424

25-
class ImageFormatter(Formatter[_T]):
26-
name: str
27-
aliases: Incomplete
28-
filenames: Incomplete
29-
unicodeoutput: bool
25+
class ImageFormatter(Formatter[bytes]):
3026
default_image_format: str
3127
encoding: str
3228
styles: Incomplete
@@ -49,23 +45,14 @@ class ImageFormatter(Formatter[_T]):
4945
hl_lines: Incomplete
5046
hl_color: Incomplete
5147
drawables: Incomplete
52-
def get_style_defs(self, arg: str = "") -> None: ...
53-
def format(self, tokensource, outfile) -> None: ...
48+
def get_style_defs(self, arg: str = "") -> Never: ...
49+
def format(self, tokensource: Iterable[tuple[_TokenType, str]], outfile: SupportsWrite[bytes]) -> None: ...
5450

55-
class GifImageFormatter(ImageFormatter[_T]):
56-
name: str
57-
aliases: Incomplete
58-
filenames: Incomplete
51+
class GifImageFormatter(ImageFormatter):
5952
default_image_format: str
6053

61-
class JpgImageFormatter(ImageFormatter[_T]):
62-
name: str
63-
aliases: Incomplete
64-
filenames: Incomplete
54+
class JpgImageFormatter(ImageFormatter):
6555
default_image_format: str
6656

67-
class BmpImageFormatter(ImageFormatter[_T]):
68-
name: str
69-
aliases: Incomplete
70-
filenames: Incomplete
57+
class BmpImageFormatter(ImageFormatter):
7158
default_image_format: str
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
from _typeshed import Incomplete
1+
from _typeshed import Incomplete, SupportsWrite
2+
from collections.abc import Iterable
23
from typing import TypeVar
34

45
from pygments.formatter import Formatter
6+
from pygments.token import _TokenType
57

68
_T = TypeVar("_T", str, bytes)
79

810
__all__ = ["IRCFormatter"]
911

1012
class IRCFormatter(Formatter[_T]):
11-
name: str
12-
aliases: Incomplete
13-
filenames: Incomplete
1413
darkbg: Incomplete
1514
colorscheme: Incomplete
1615
linenos: Incomplete
17-
def format_unencoded(self, tokensource, outfile) -> None: ...
16+
def format_unencoded(self, tokensource: Iterable[tuple[_TokenType, str]], outfile: SupportsWrite[str]) -> None: ...

stubs/Pygments/pygments/formatters/latex.pyi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
from _typeshed import Incomplete
1+
from _typeshed import Incomplete, SupportsWrite
2+
from collections.abc import Iterable
23
from typing import TypeVar
34

45
from pygments.formatter import Formatter
56
from pygments.lexer import Lexer
7+
from pygments.token import _TokenType
68

79
_T = TypeVar("_T", str, bytes)
810

911
__all__ = ["LatexFormatter"]
1012

1113
class LatexFormatter(Formatter[_T]):
12-
name: str
13-
aliases: Incomplete
14-
filenames: Incomplete
1514
docclass: Incomplete
1615
preamble: Incomplete
1716
linenos: Incomplete
@@ -27,7 +26,7 @@ class LatexFormatter(Formatter[_T]):
2726
right: Incomplete
2827
envname: Incomplete
2928
def get_style_defs(self, arg: str = ""): ...
30-
def format_unencoded(self, tokensource, outfile) -> None: ...
29+
def format_unencoded(self, tokensource: Iterable[tuple[_TokenType, str]], outfile: SupportsWrite[str]) -> None: ...
3130

3231
class LatexEmbeddedLexer(Lexer):
3332
left: Incomplete
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
1-
from _typeshed import Incomplete
1+
from _typeshed import Incomplete, SupportsWrite
2+
from collections.abc import Iterable
23
from typing import TypeVar
34

45
from pygments.formatter import Formatter
6+
from pygments.token import _TokenType
57

68
_T = TypeVar("_T", str, bytes)
79

810
__all__ = ["NullFormatter", "RawTokenFormatter", "TestcaseFormatter"]
911

1012
class NullFormatter(Formatter[_T]):
11-
name: str
12-
aliases: Incomplete
13-
filenames: Incomplete
14-
def format(self, tokensource, outfile) -> None: ...
13+
def format(self, tokensource: Iterable[tuple[_TokenType, str]], outfile: SupportsWrite[_T]) -> None: ...
1514

1615
class RawTokenFormatter(Formatter[bytes]):
17-
name: str
18-
aliases: Incomplete
19-
filenames: Incomplete
20-
unicodeoutput: bool
2116
encoding: str
2217
compress: Incomplete
2318
error_color: Incomplete
24-
def format(self, tokensource, outfile) -> None: ...
19+
def format(self, tokensource: Iterable[tuple[_TokenType, str]], outfile: SupportsWrite[bytes]) -> None: ...
2520

2621
class TestcaseFormatter(Formatter[_T]):
27-
name: str
28-
aliases: Incomplete
29-
def format(self, tokensource, outfile) -> None: ...
22+
def format(self, tokensource: Iterable[tuple[_TokenType, str]], outfile: SupportsWrite[_T]) -> None: ...
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
from _typeshed import Incomplete
1+
from _typeshed import Incomplete, SupportsWrite
2+
from collections.abc import Iterable
23
from typing import TypeVar
34

45
from pygments.formatter import Formatter
6+
from pygments.token import _TokenType
57

68
_T = TypeVar("_T", str, bytes)
79

810
__all__ = ["PangoMarkupFormatter"]
911

1012
class PangoMarkupFormatter(Formatter[_T]):
11-
name: str
12-
aliases: Incomplete
13-
filenames: Incomplete
1413
styles: Incomplete
15-
def format_unencoded(self, tokensource, outfile) -> None: ...
14+
def format_unencoded(self, tokensource: Iterable[tuple[_TokenType, str]], outfile: SupportsWrite[str]) -> None: ...
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
from _typeshed import Incomplete
1+
from _typeshed import Incomplete, SupportsWrite
2+
from collections.abc import Iterable
23
from typing import TypeVar
34

45
from pygments.formatter import Formatter
6+
from pygments.token import _TokenType
57

68
_T = TypeVar("_T", str, bytes)
79

810
__all__ = ["RtfFormatter"]
911

1012
class RtfFormatter(Formatter[_T]):
11-
name: str
12-
aliases: Incomplete
13-
filenames: Incomplete
1413
fontface: Incomplete
1514
fontsize: Incomplete
1615
@staticmethod
1716
def hex_to_rtf_color(hex_color: str) -> str: ...
18-
def format_unencoded(self, tokensource, outfile) -> None: ...
17+
def format_unencoded(self, tokensource: Iterable[tuple[_TokenType, str]], outfile: SupportsWrite[str]) -> None: ...

0 commit comments

Comments
 (0)