Make Formatter generic for the benefit of typeshed by not-my-profile · Pull Request #2081 · pygments/pygments

The type stubs for Pygments in the typeshed have made pygments.formatter.Formatter generic in python/typeshed#6819 so that an outfile passed to pygments.highlight() must be opened in binary mode when the formatter was created with a given encoding, for example:

import pygments
import pygments.lexers
import pygments.formatters

lexer = pygments.lexers.get_lexer_by_name('python')
formatter = pygments.formatters.html.HtmlFormatter(encoding='utf-8')

with open('out.html', 'wb') as f: # mypy reports a type error if opened with 'w'
    pygments.highlight('test', lexer, formatter, outfile=f)

This does however come with the disadvantage that subclassing Formatter no longer type checks with mypy --strict:

class Foo(pygments.formatter.Formatter):
     pass

because mypy complains with Missing type parameters for generic type "Formatter". The problem with that is that fixing this by specifying the type parameter would result in a runtime error because the real pygments.formatter.Formatter is not generic and thus not subscriptable.

Making the actual Formatter generic improves the situation for pygments users using static type checkers and should not change anything for the pygments users that don't.