Skip to content

Commit d24f07c

Browse files
pygments: Correct Formatter generics
933fec0 made pygments.formatter.Formatter generic to improve the type-safety of pygments.format() and pygments.highlight(). The actual pygments.formatter.Formatter however is not generic, meaning that pygments.formatter.Formatter[str] should not type check (since it leads to a runtime type error). Furthermore the pygments ImageFormatter always expects BinaryIO, no matter which constructor arguments are passed. This commit addresses both problems.
1 parent 342e384 commit d24f07c

15 files changed

+54
-46
lines changed

stubs/Pygments/pygments/__init__.pyi

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from _typeshed import SupportsWrite
22
from typing import TypeVar, overload
33

4-
from pygments.formatter import Formatter
4+
from pygments.formatter import _Formatter
55

66
_T = TypeVar("_T", str, bytes)
77

88
def lex(code, lexer): ...
99
@overload
10-
def format(tokens, formatter: Formatter[_T], outfile: SupportsWrite[_T]) -> None: ...
10+
def format(tokens, formatter: _Formatter[_T], outfile: SupportsWrite[_T]) -> None: ...
1111
@overload
12-
def format(tokens, formatter: Formatter[_T], outfile: None = ...) -> _T: ...
12+
def format(tokens, formatter: _Formatter[_T], outfile: None = ...) -> _T: ...
1313
@overload
14-
def highlight(code, lexer, formatter: Formatter[_T], outfile: SupportsWrite[_T]) -> None: ...
14+
def highlight(code, lexer, formatter: _Formatter[_T], outfile: SupportsWrite[_T]) -> None: ...
1515
@overload
16-
def highlight(code, lexer, formatter: Formatter[_T], outfile: None = ...) -> _T: ...
16+
def highlight(code, lexer, formatter: _Formatter[_T], outfile: None = ...) -> _T: ...

stubs/Pygments/pygments/formatter.pyi

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from typing import Any, Generic, TypeVar, overload
1+
from typing import Any, Generic, TypeVar, overload, type_check_only
22

33
_T = TypeVar("_T", str, bytes)
44

5-
class Formatter(Generic[_T]):
5+
class Formatter:
66
name: Any
77
aliases: Any
88
filenames: Any
@@ -12,11 +12,21 @@ class Formatter(Generic[_T]):
1212
title: Any
1313
encoding: Any
1414
options: Any
15+
def get_style_defs(self, arg: str = ...): ...
16+
def format(self, tokensource, outfile): ...
17+
18+
@type_check_only
19+
class _Formatter(Generic[_T], Formatter): ...
20+
21+
@type_check_only
22+
class _BinaryFormatter(_Formatter[bytes]):
23+
def __init__(self, **options) -> None: ...
24+
25+
@type_check_only
26+
class _TextFormatter(_Formatter[_T]):
1527
@overload
16-
def __init__(self: Formatter[str], *, encoding: None = ..., outencoding: None = ..., **options) -> None: ...
28+
def __init__(self: _Formatter[str], *, encoding: None = ..., outencoding: None = ..., **options) -> None: ...
1729
@overload
18-
def __init__(self: Formatter[bytes], *, encoding: str, outencoding: None = ..., **options) -> None: ...
30+
def __init__(self: _Formatter[bytes], *, encoding: str, outencoding: None = ..., **options) -> None: ...
1931
@overload
20-
def __init__(self: Formatter[bytes], *, encoding: None = ..., outencoding: str, **options) -> None: ...
21-
def get_style_defs(self, arg: str = ...): ...
22-
def format(self, tokensource, outfile): ...
32+
def __init__(self: _Formatter[bytes], *, encoding: None = ..., outencoding: str, **options) -> None: ...

stubs/Pygments/pygments/formatters/__init__.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, Generator
22

3-
from ..formatter import Formatter
3+
from ..formatter import _Formatter
44
from .bbcode import BBCodeFormatter as BBCodeFormatter
55
from .html import HtmlFormatter as HtmlFormatter
66
from .img import (
@@ -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[Any]], 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): ...

stubs/Pygments/pygments/formatters/bbcode.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from typing import Any, TypeVar
22

3-
from pygments.formatter import Formatter
3+
from pygments.formatter import _TextFormatter
44

55
_T = TypeVar("_T", str, bytes)
66

7-
class BBCodeFormatter(Formatter[_T]):
7+
class BBCodeFormatter(_TextFormatter[_T]):
88
name: str
99
aliases: Any
1010
filenames: Any

stubs/Pygments/pygments/formatters/html.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from typing import Any, TypeVar
22

3-
from pygments.formatter import Formatter
3+
from pygments.formatter import _TextFormatter
44

55
_T = TypeVar("_T", str, bytes)
66

7-
class HtmlFormatter(Formatter[_T]):
7+
class HtmlFormatter(_TextFormatter[_T]):
88
name: str
99
aliases: Any
1010
filenames: Any

stubs/Pygments/pygments/formatters/img.pyi

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from typing import Any, TypeVar
1+
from typing import Any
22

3-
from pygments.formatter import Formatter
4-
5-
_T = TypeVar("_T", str, bytes)
3+
from pygments.formatter import _BinaryFormatter
64

75
class PilNotAvailable(ImportError): ...
86
class FontNotFound(Exception): ...
@@ -17,7 +15,7 @@ class FontManager:
1715
def get_text_size(self, text): ...
1816
def get_font(self, bold, oblique): ...
1917

20-
class ImageFormatter(Formatter[_T]):
18+
class ImageFormatter(_BinaryFormatter):
2119
name: str
2220
aliases: Any
2321
filenames: Any
@@ -47,19 +45,19 @@ class ImageFormatter(Formatter[_T]):
4745
def get_style_defs(self, arg: str = ...) -> None: ...
4846
def format(self, tokensource, outfile) -> None: ...
4947

50-
class GifImageFormatter(ImageFormatter[_T]):
48+
class GifImageFormatter(ImageFormatter):
5149
name: str
5250
aliases: Any
5351
filenames: Any
5452
default_image_format: str
5553

56-
class JpgImageFormatter(ImageFormatter[_T]):
54+
class JpgImageFormatter(ImageFormatter):
5755
name: str
5856
aliases: Any
5957
filenames: Any
6058
default_image_format: str
6159

62-
class BmpImageFormatter(ImageFormatter[_T]):
60+
class BmpImageFormatter(ImageFormatter):
6361
name: str
6462
aliases: Any
6563
filenames: Any

stubs/Pygments/pygments/formatters/irc.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from typing import Any, TypeVar
22

3-
from pygments.formatter import Formatter
3+
from pygments.formatter import _TextFormatter
44

55
_T = TypeVar("_T", str, bytes)
66

7-
class IRCFormatter(Formatter[_T]):
7+
class IRCFormatter(_TextFormatter[_T]):
88
name: str
99
aliases: Any
1010
filenames: Any

stubs/Pygments/pygments/formatters/latex.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from typing import Any, TypeVar
22

3-
from pygments.formatter import Formatter
3+
from pygments.formatter import _TextFormatter
44
from pygments.lexer import Lexer
55

66
_T = TypeVar("_T", str, bytes)
77

8-
class LatexFormatter(Formatter[_T]):
8+
class LatexFormatter(_TextFormatter[_T]):
99
name: str
1010
aliases: Any
1111
filenames: Any
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from typing import Any, TypeVar
22

3-
from pygments.formatter import Formatter
3+
from pygments.formatter import _TextFormatter
44

55
_T = TypeVar("_T", str, bytes)
66

7-
class NullFormatter(Formatter[_T]):
7+
class NullFormatter(_TextFormatter[_T]):
88
name: str
99
aliases: Any
1010
filenames: Any
1111
def format(self, tokensource, outfile) -> None: ...
1212

13-
class RawTokenFormatter(Formatter[_T]):
13+
class RawTokenFormatter(_TextFormatter[_T]):
1414
name: str
1515
aliases: Any
1616
filenames: Any
@@ -20,7 +20,7 @@ class RawTokenFormatter(Formatter[_T]):
2020
error_color: Any
2121
def format(self, tokensource, outfile) -> None: ...
2222

23-
class TestcaseFormatter(Formatter[_T]):
23+
class TestcaseFormatter(_TextFormatter[_T]):
2424
name: str
2525
aliases: Any
2626
def format(self, tokensource, outfile) -> None: ...

stubs/Pygments/pygments/formatters/pangomarkup.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from typing import Any, TypeVar
22

3-
from pygments.formatter import Formatter
3+
from pygments.formatter import _TextFormatter
44

55
_T = TypeVar("_T", str, bytes)
66

7-
class PangoMarkupFormatter(Formatter[_T]):
7+
class PangoMarkupFormatter(_TextFormatter[_T]):
88
name: str
99
aliases: Any
1010
filenames: Any

stubs/Pygments/pygments/formatters/rtf.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from typing import Any, TypeVar
22

3-
from pygments.formatter import Formatter
3+
from pygments.formatter import _TextFormatter
44

55
_T = TypeVar("_T", str, bytes)
66

7-
class RtfFormatter(Formatter[_T]):
7+
class RtfFormatter(_TextFormatter[_T]):
88
name: str
99
aliases: Any
1010
filenames: Any

stubs/Pygments/pygments/formatters/svg.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from typing import Any, TypeVar
22

3-
from pygments.formatter import Formatter
3+
from pygments.formatter import _TextFormatter
44

55
_T = TypeVar("_T", str, bytes)
66

7-
class SvgFormatter(Formatter[_T]):
7+
class SvgFormatter(_TextFormatter[_T]):
88
name: str
99
aliases: Any
1010
filenames: Any

stubs/Pygments/pygments/formatters/terminal.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from typing import Any, TypeVar
22

3-
from pygments.formatter import Formatter
3+
from pygments.formatter import _TextFormatter
44

55
_T = TypeVar("_T", str, bytes)
66

7-
class TerminalFormatter(Formatter[_T]):
7+
class TerminalFormatter(_TextFormatter[_T]):
88
name: str
99
aliases: Any
1010
filenames: Any

stubs/Pygments/pygments/formatters/terminal256.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, TypeVar
22

3-
from pygments.formatter import Formatter
3+
from pygments.formatter import _TextFormatter
44

55
_T = TypeVar("_T", str, bytes)
66

@@ -18,7 +18,7 @@ class EscapeSequence:
1818
def true_color_string(self): ...
1919
def reset_string(self): ...
2020

21-
class Terminal256Formatter(Formatter[_T]):
21+
class Terminal256Formatter(_TextFormatter[_T]):
2222
name: str
2323
aliases: Any
2424
filenames: Any

stubs/Pygments/pygments/plugin.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from typing import Any, Generator, Iterable
22

33
from pkg_resources import EntryPoint
44
from pygments.filter import Filter
5-
from pygments.formatter import Formatter
5+
from pygments.formatter import _Formatter
66
from pygments.lexer import Lexer
77
from pygments.style import Style
88

@@ -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[Any]]], 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)