Typing stubs for Pygments
types-pygments is a PEP 561 type stub package providing external type annotations for the Pygments library. It enables static type checkers like MyPy and PyCharm to verify code that uses Pygments. This package, currently at version 2.20.0.20260408, is part of the typeshed project and is regularly updated, often daily, to reflect changes in the underlying Pygments library.
Warnings
- gotcha The 'types-pygments' package provides *only* type annotations. It does not include the Pygments runtime library itself. You must install 'Pygments' separately for your code to function.
- breaking The version of 'types-pygments' should ideally align with the major and minor version of your installed 'Pygments' library. For example, 'types-pygments==2.20.0.x' is designed for 'Pygments==2.20.*'. Using a significantly mismatched version might lead to inaccurate type checking results.
- gotcha This stub package is marked as 'partial'. This means not all parts of the Pygments library may have complete or perfectly accurate type annotations. You might encounter 'Any' types or missing annotations for less common functions/classes.
Install
-
pip install types-pygments Pygments
Imports
- highlight
from pygments import highlight
- PythonLexer
from pygments.lexers import PythonLexer
- HtmlFormatter
from pygments.formatters import HtmlFormatter
Quickstart
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
code = 'print("Hello, world!")'
# Highlight the code
highlighted_code = highlight(code, PythonLexer(), HtmlFormatter())
print(highlighted_code)
# To verify types with a checker like MyPy, save this to a .py file and run:
# mypy your_file_name.py
# The 'types-pygments' package provides the necessary type hints for the above operations.