TypeGuard typechecking on `filter` is too strict
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
Bug Report
NB: This may be more properly an issue in Typeshed, or require changes in that project to fix, but our team encountered it via MyPy.
The type annotations on builtins.filter cause correct and idiomatic usages to fail to typecheck:
To Reproduce
# filter-mypy.py
path_parts: List[Optional[str]] = ["foo", None, "bar"]
"/".join(filter(None, path_parts))
"/".join(filter(bool, path_parts))
"/".join(filter(lambda s: isinstance(s, str), path_parts))
Expected:
$> mypy filter-mypy.py
Success: no issues found in 1 source file
Actual result:
$> mypy filter-mypy.py
filter-mypy.py:6: error: Argument 1 to "filter" has incompatible type "Type[bool]"; expected "Callable[[Optional[str]], TypeGuard[str]]"
filter-mypy.py:7: error: Argument 1 to "filter" has incompatible type "Callable[[Any], bool]"; expected "Callable[[Optional[str]], TypeGuard[str]]"
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used:
$> mypy --version
mypy 0.950 (compiled: yes)
- Python version used:
$> python --version
Python 3.10.4
- Operating system and version: Ubuntu 20.04
The following is what my editor (VSCode) pulls up when I investigate the type hints for filter, though I doubt this is the same file that MyPy itself is consulting:
class filter(Iterator[_T], Generic[_T]):
@overload
def __init__(self, __function: None, __iterable: Iterable[_T | None]) -> None: ...
@overload
def __init__(self, __function: Callable[[_S], TypeGuard[_T]], __iterable: Iterable[_S]) -> None: ...
@overload
def __init__(self, __function: Callable[[_T], Any], __iterable: Iterable[_T]) -> None: ...
Note that both offending lines conform to the third type signature, but fail the second.
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
Reproduce el informe con el ejemplo filter-mypy.py proporcionado e inspecciona las sobrecargas de builtins.filter que se muestran en el issue; después determina si la corrección corresponde a mypy o a typeshed. Se considera terminado cuando los usos de None, bool e isinstance pasan la comprobación de tipos como se espera, sin debilitar la comprobación de filter no relacionada.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 42/100