Skip to content

Commit 933fec0

Browse files
DreamsorcererAkuli
andauthored
Pygments: make Formatter generic and improve format/highlight (#6819)
Co-authored-by: Akuli <[email protected]>
1 parent a40d79a commit 933fec0

15 files changed

+78
-50
lines changed

stubs/Pygments/pygments/__init__.pyi

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
from typing import Any
1+
from _typeshed import SupportsWrite
2+
from typing import TypeVar, overload
3+
4+
from pygments.formatter import Formatter
5+
6+
_T = TypeVar("_T", str, bytes)
27

38
def lex(code, lexer): ...
4-
def format(tokens, formatter, outfile: Any | None = ...): ...
5-
def highlight(code, lexer, formatter, outfile: Any | None = ...): ...
9+
@overload
10+
def format(tokens, formatter: Formatter[_T], outfile: SupportsWrite[_T]) -> None: ...
11+
@overload
12+
def format(tokens, formatter: Formatter[_T], outfile: None = ...) -> _T: ...
13+
@overload
14+
def highlight(code, lexer, formatter: Formatter[_T], outfile: SupportsWrite[_T]) -> None: ...
15+
@overload
16+
def highlight(code, lexer, formatter: Formatter[_T], outfile: None = ...) -> _T: ...

stubs/Pygments/pygments/formatter.pyi

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
from typing import Any
1+
from typing import Any, Generic, TypeVar, overload
22

3-
class Formatter:
3+
_T = TypeVar("_T", str, bytes)
4+
5+
class Formatter(Generic[_T]):
46
name: Any
57
aliases: Any
68
filenames: Any
@@ -10,6 +12,11 @@ class Formatter:
1012
title: Any
1113
encoding: Any
1214
options: Any
13-
def __init__(self, **options) -> None: ...
15+
@overload
16+
def __init__(self: Formatter[str], *, encoding: None = ..., outencoding: None = ..., **options) -> None: ...
17+
@overload
18+
def __init__(self: Formatter[bytes], *, encoding: str, outencoding: None = ..., **options) -> None: ...
19+
@overload
20+
def __init__(self: Formatter[bytes], *, encoding: None = ..., outencoding: str, **options) -> None: ...
1421
def get_style_defs(self, arg: str = ...): ...
1522
def format(self, tokensource, outfile): ...

stubs/Pygments/pygments/formatters/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Generator
1+
from typing import Any, Generator
22

33
from ..formatter import Formatter
44
from .bbcode import BBCodeFormatter as BBCodeFormatter
@@ -18,7 +18,7 @@ from .svg import SvgFormatter as SvgFormatter
1818
from .terminal import TerminalFormatter as TerminalFormatter
1919
from .terminal256 import Terminal256Formatter as Terminal256Formatter, TerminalTrueColorFormatter as TerminalTrueColorFormatter
2020

21-
def get_all_formatters() -> Generator[type[Formatter], None, None]: ...
21+
def get_all_formatters() -> Generator[type[Formatter[Any]], None, None]: ...
2222
def get_formatter_by_name(_alias, **options): ...
2323
def load_formatter_from_file(filename, formattername: str = ..., **options): ...
2424
def get_formatter_for_filename(fn, **options): ...
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from typing import Any
1+
from typing import Any, TypeVar
22

33
from pygments.formatter import Formatter
44

5-
class BBCodeFormatter(Formatter):
5+
_T = TypeVar("_T", str, bytes)
6+
7+
class BBCodeFormatter(Formatter[_T]):
68
name: str
79
aliases: Any
810
filenames: Any
911
styles: Any
10-
def __init__(self, **options) -> None: ...
1112
def format_unencoded(self, tokensource, outfile) -> None: ...

stubs/Pygments/pygments/formatters/html.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from typing import Any
1+
from typing import Any, TypeVar
22

33
from pygments.formatter import Formatter
44

5-
class HtmlFormatter(Formatter):
5+
_T = TypeVar("_T", str, bytes)
6+
7+
class HtmlFormatter(Formatter[_T]):
68
name: str
79
aliases: Any
810
filenames: Any
@@ -30,7 +32,6 @@ class HtmlFormatter(Formatter):
3032
linespans: Any
3133
anchorlinenos: Any
3234
hl_lines: Any
33-
def __init__(self, **options) -> None: ...
3435
def get_style_defs(self, arg: Any | None = ...): ...
3536
def get_token_style_defs(self, arg: Any | None = ...): ...
3637
def get_background_style_defs(self, arg: Any | None = ...): ...

stubs/Pygments/pygments/formatters/img.pyi

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from typing import Any
1+
from typing import Any, TypeVar
22

33
from pygments.formatter import Formatter
44

5+
_T = TypeVar("_T", str, bytes)
6+
57
class PilNotAvailable(ImportError): ...
68
class FontNotFound(Exception): ...
79

@@ -15,7 +17,7 @@ class FontManager:
1517
def get_text_size(self, text): ...
1618
def get_font(self, bold, oblique): ...
1719

18-
class ImageFormatter(Formatter):
20+
class ImageFormatter(Formatter[_T]):
1921
name: str
2022
aliases: Any
2123
filenames: Any
@@ -42,23 +44,22 @@ class ImageFormatter(Formatter):
4244
hl_lines: Any
4345
hl_color: Any
4446
drawables: Any
45-
def __init__(self, **options) -> None: ...
4647
def get_style_defs(self, arg: str = ...) -> None: ...
4748
def format(self, tokensource, outfile) -> None: ...
4849

49-
class GifImageFormatter(ImageFormatter):
50+
class GifImageFormatter(ImageFormatter[_T]):
5051
name: str
5152
aliases: Any
5253
filenames: Any
5354
default_image_format: str
5455

55-
class JpgImageFormatter(ImageFormatter):
56+
class JpgImageFormatter(ImageFormatter[_T]):
5657
name: str
5758
aliases: Any
5859
filenames: Any
5960
default_image_format: str
6061

61-
class BmpImageFormatter(ImageFormatter):
62+
class BmpImageFormatter(ImageFormatter[_T]):
6263
name: str
6364
aliases: Any
6465
filenames: Any
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
from typing import Any
1+
from typing import Any, TypeVar
22

33
from pygments.formatter import Formatter
44

5-
class IRCFormatter(Formatter):
5+
_T = TypeVar("_T", str, bytes)
6+
7+
class IRCFormatter(Formatter[_T]):
68
name: str
79
aliases: Any
810
filenames: Any
911
darkbg: Any
1012
colorscheme: Any
1113
linenos: Any
12-
def __init__(self, **options) -> None: ...
1314
def format_unencoded(self, tokensource, outfile) -> None: ...

stubs/Pygments/pygments/formatters/latex.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from typing import Any
1+
from typing import Any, TypeVar
22

33
from pygments.formatter import Formatter
44
from pygments.lexer import Lexer
55

6-
class LatexFormatter(Formatter):
6+
_T = TypeVar("_T", str, bytes)
7+
8+
class LatexFormatter(Formatter[_T]):
79
name: str
810
aliases: Any
911
filenames: Any
@@ -21,7 +23,6 @@ class LatexFormatter(Formatter):
2123
left: Any
2224
right: Any
2325
envname: Any
24-
def __init__(self, **options) -> None: ...
2526
def get_style_defs(self, arg: str = ...): ...
2627
def format_unencoded(self, tokensource, outfile) -> None: ...
2728

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
from typing import Any
1+
from typing import Any, TypeVar
22

33
from pygments.formatter import Formatter
44

5-
class NullFormatter(Formatter):
5+
_T = TypeVar("_T", str, bytes)
6+
7+
class NullFormatter(Formatter[_T]):
68
name: str
79
aliases: Any
810
filenames: Any
911
def format(self, tokensource, outfile) -> None: ...
1012

11-
class RawTokenFormatter(Formatter):
13+
class RawTokenFormatter(Formatter[_T]):
1214
name: str
1315
aliases: Any
1416
filenames: Any
1517
unicodeoutput: bool
1618
encoding: str
1719
compress: Any
1820
error_color: Any
19-
def __init__(self, **options) -> None: ...
2021
def format(self, tokensource, outfile) -> None: ...
2122

22-
class TestcaseFormatter(Formatter):
23+
class TestcaseFormatter(Formatter[_T]):
2324
name: str
2425
aliases: Any
25-
def __init__(self, **options) -> None: ...
2626
def format(self, tokensource, outfile) -> None: ...
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from typing import Any
1+
from typing import Any, TypeVar
22

33
from pygments.formatter import Formatter
44

5-
class PangoMarkupFormatter(Formatter):
5+
_T = TypeVar("_T", str, bytes)
6+
7+
class PangoMarkupFormatter(Formatter[_T]):
68
name: str
79
aliases: Any
810
filenames: Any
911
styles: Any
10-
def __init__(self, **options) -> None: ...
1112
def format_unencoded(self, tokensource, outfile) -> None: ...
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
from typing import Any
1+
from typing import Any, TypeVar
22

33
from pygments.formatter import Formatter
44

5-
class RtfFormatter(Formatter):
5+
_T = TypeVar("_T", str, bytes)
6+
7+
class RtfFormatter(Formatter[_T]):
68
name: str
79
aliases: Any
810
filenames: Any
911
fontface: Any
1012
fontsize: Any
11-
def __init__(self, **options) -> None: ...
1213
def format_unencoded(self, tokensource, outfile) -> None: ...

stubs/Pygments/pygments/formatters/svg.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from typing import Any
1+
from typing import Any, TypeVar
22

33
from pygments.formatter import Formatter
44

5-
class SvgFormatter(Formatter):
5+
_T = TypeVar("_T", str, bytes)
6+
7+
class SvgFormatter(Formatter[_T]):
68
name: str
79
aliases: Any
810
filenames: Any
@@ -17,5 +19,4 @@ class SvgFormatter(Formatter):
1719
linenostart: Any
1820
linenostep: Any
1921
linenowidth: Any
20-
def __init__(self, **options) -> None: ...
2122
def format_unencoded(self, tokensource, outfile) -> None: ...
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
from typing import Any
1+
from typing import Any, TypeVar
22

33
from pygments.formatter import Formatter
44

5-
class TerminalFormatter(Formatter):
5+
_T = TypeVar("_T", str, bytes)
6+
7+
class TerminalFormatter(Formatter[_T]):
68
name: str
79
aliases: Any
810
filenames: Any
911
darkbg: Any
1012
colorscheme: Any
1113
linenos: Any
12-
def __init__(self, **options) -> None: ...
1314
def format(self, tokensource, outfile): ...
1415
def format_unencoded(self, tokensource, outfile) -> None: ...

stubs/Pygments/pygments/formatters/terminal256.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from typing import Any
1+
from typing import Any, TypeVar
22

33
from pygments.formatter import Formatter
44

5+
_T = TypeVar("_T", str, bytes)
6+
57
class EscapeSequence:
68
fg: Any
79
bg: Any
@@ -16,7 +18,7 @@ class EscapeSequence:
1618
def true_color_string(self): ...
1719
def reset_string(self): ...
1820

19-
class Terminal256Formatter(Formatter):
21+
class Terminal256Formatter(Formatter[_T]):
2022
name: str
2123
aliases: Any
2224
filenames: Any
@@ -27,11 +29,10 @@ class Terminal256Formatter(Formatter):
2729
useunderline: Any
2830
useitalic: Any
2931
linenos: Any
30-
def __init__(self, **options) -> None: ...
3132
def format(self, tokensource, outfile): ...
3233
def format_unencoded(self, tokensource, outfile) -> None: ...
3334

34-
class TerminalTrueColorFormatter(Terminal256Formatter):
35+
class TerminalTrueColorFormatter(Terminal256Formatter[_T]):
3536
name: str
3637
aliases: Any
3738
filenames: Any

stubs/Pygments/pygments/plugin.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Generator, Iterable
1+
from typing import Any, Generator, Iterable
22

33
from pkg_resources import EntryPoint
44
from pygments.filter import Filter
@@ -13,6 +13,6 @@ FILTER_ENTRY_POINT: str
1313

1414
def iter_entry_points(group_name: str) -> Iterable[EntryPoint]: ...
1515
def find_plugin_lexers() -> Generator[type[Lexer], None, None]: ...
16-
def find_plugin_formatters() -> Generator[tuple[str, type[Formatter]], None, None]: ...
16+
def find_plugin_formatters() -> Generator[tuple[str, type[Formatter[Any]]], None, None]: ...
1717
def find_plugin_styles() -> Generator[tuple[str, type[Style]], None, None]: ...
1818
def find_plugin_filters() -> Generator[tuple[str, type[Filter]], None, None]: ...

0 commit comments

Comments
 (0)