TypeGuard typechecking on `filter` is too strict
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 42/100
Direzione di ricerca
Riproduci il report con l'esempio filter-mypy.py fornito e ispeziona gli overload di builtins.filter mostrati nell'issue, quindi determina se la correzione appartiene a mypy o a typeshed. Il lavoro è completato quando gli usi di None, bool e isinstance superano il controllo dei tipi come previsto, senza indebolire il controllo di filter non correlato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Merge medio
- 1g 18h
- PR unite (30g)
- 54
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python/mypy
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
bug topic-configuration topic-error-reporting
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
Issue simili
-
area/auth bug comp/agent P3 platform/discord type/security
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
NousResearch/hermes-agent#117848 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
bancolombia/sentinel#23 ·
-
test md ApertaCI
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100