Skip to content

[Pygments] Complete stubs for Formatter#15608

Open
brianschubert wants to merge 1 commit intopython:mainfrom
brianschubert:stubs-pygments-complete-formatter
Open

[Pygments] Complete stubs for Formatter#15608
brianschubert wants to merge 1 commit intopython:mainfrom
brianschubert:stubs-pygments-complete-formatter

Conversation

@brianschubert
Copy link
Copy Markdown
Collaborator

  • Complete stubs for Formatter itself
  • Add stubs for format and format_unencoded methods in subclasses.
  • Removed bad overrides of Formatter class attributes in subclasses.
  • Make ImageFormatter inherit from Formatter[bytes] (doesn't support writing to text files)
  • Change return type of ImageFormatter.get_style_defs to Never (raises NotImplementedError at runtime)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 4, 2026

Diff from mypy_primer, showing the effect of this PR on open source code:

pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/_io/terminalwriter.py:223: error: Argument "style" to "TerminalFormatter" has incompatible type "str | None"; expected "type[Style] | str"  [arg-type]

@brianschubert
Copy link
Copy Markdown
Collaborator Author

brianschubert commented Apr 4, 2026

The pytest hit looks like a true positive: https://github.com/pytest-dev/pytest/blob/f93656d611f3c9cd22f346dfeda52cddb778c589/src/_pytest/_io/terminalwriter.py#L223

There's no explicit handling for passing None to style, and various Formatter subclasses assume that it is not None (example), so I don't think it can be None in general.

The specific Formatter subclass they're using (TerminalFormatter) never actually uses style itself (and is documented as such), so it's not clear why they're even trying to set it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant