Open
Description
In #6819 pygments.formatter.Formatter
was made generic to improve the type safety:
_T = TypeVar("_T", str, bytes)
class Formatter(Generic[_T]):
@overload
def __init__(self: Formatter[str], *, encoding: None = ..., outencoding: None = ..., **options) -> None: ...
@overload
def __init__(self: Formatter[bytes], *, encoding: str, outencoding: None = ..., **options) -> None: ...
@overload
def __init__(self: Formatter[bytes], *, encoding: None = ..., outencoding: str, **options) -> None: ...
Apparently this was never tested, since it does not actually work:
from pygments.formatters.html import HtmlFormatter
reveal_type(HtmlFormatter())
reveal_type(HtmlFormatter(encoding='utf-8'))
$ mypy foo.py
foo.py:3: note: Revealed type is "pygments.formatters.html.HtmlFormatter[builtins.str*]"
foo.py:4: note: Revealed type is "pygments.formatters.html.HtmlFormatter[builtins.str*]"
$ pyright foo.py
/tmp/foo.py:3:13 - information: Type of "HtmlFormatter(encoding='utf-8')" is "HtmlFormatter[Unknown]"
/tmp/foo.py:4:13 - information: Type of "HtmlFormatter()" is "HtmlFormatter[Unknown]"
The intention of the overloads is that HtmlFormatter(encoding='utf-8')
should be of type HtmlFormatter[builtins.bytes]
.
I am not sure if this is a mypy bug or if such overload inference should even work in the first place (given that pyright also infers the type variable to be Unknown
: microsoft/pyright#3146).
Metadata
Metadata
Assignees
Labels
No labels