Add a __class_getitem__ to Formatter by Akuli · Pull Request #2665 · pygments/pygments
This is a simpler and less disruptive alternative to #2081.
The type stubs in typeshed distinguish between formatters that output strings and bytes. In type annotations, these are distinguished with Formatter[str] and Formatter[bytes]. For the most part, this works fine without any changes in pygments: users just tell Python to not evaluate type annotations at runtime (with from __future__ import annotations), and then a Formatter[str] type annotation won't cause an error even though subscripting the Formatter class wouldn't work.
But one special case happens when you try to subclass a Formatter. For that, you actually need to specify a class, not a type annotation. So users end up with:
from typing import TYPE_CHECKING from pygments.formatter import Formatter if TYPE_CHECKING: StringFormatter = Formatter[str] else: StringFormatter = Formatter class MyFormatter(StringFormatter): ...
With this PR, this simplifies to:
from pygments.formatter import Formatter class MyFormatter(Formatter[str]): ...