Pygments `Formatter.__init__` overloads do not work as intended
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 5.1k
- Forks
- 2.1k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 82
Descripción
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: https://github.com/microsoft/pyright/issues/3146).
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comience con las sobrecargas de Formatter introducidas en #6819 y el punto de entrada HtmlFormatter. Reproduzca los dos ejemplos de reveal_type con mypy y pyright, y determine después si las declaraciones de sobrecarga o el comportamiento del comprobador impiden que encoding='utf-8' infiera HtmlFormatter[bytes]. Se considera completado cuando se admiten los tipos inferidos previstos o la limitación está claramente documentada.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- tooling
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 30/100