Several methods on built-in types are too strict.
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 5.1k
- Fork
- 2.1k
- Merge medio
- 1g 19h
- PR unite (30g)
- 82
Descrizione
I have identified several methods on built-in types that are too strict and can cause annoying false positives:
- set and frozenset methods (#15470, #15160)
-
dict.{get, pop}(#15471) -
list.{count, index, remove}(#15472) -
MutableSet.discard -
Mapping.get -
MutableMapping.pop -
Sequence.{count, index} -
MutableSequence.remove - probably some more.
It seems like the annotations for these were designed to help detect user errors. For instance, we know that .get on a dict[int, int] will always return the default as the types are incompatible, and so .get was restricted in typeshed to only accept the key type. However, this hurts, even in simple cases like using literals:
from typing import Mapping, Literal
def demo_get(
d: Mapping[Literal["foo", "bar"], int],
dynamic_key: str,
) -> None:
# No overload variant of "get" of "Mapping" matches argument type "str"
d.get(dynamic_key)
def demo_sub(
left: set[Literal["foo", "bar"]],
right: set[str],
) -> None:
# Unsupported operand types for - ("set[Literal['foo', 'bar']]" and "set[str]")
left - right
I believe typeshed is trying to act as a linter here, which shouldn't be its job. Type-checkers and type-informed linters are better at this and can use more complex logic.
For example, in the set difference case set[A] - set[B], the proper test would be to check whether the types have a non-empty intersection or not. The same could be said for Mapping[K, V].get(K2).
Related Topics:
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.
Direzione di ricerca
Inizia individuando gli stub di typeshed per i metodi elencati di dict, list, set, Mapping, Sequence e Mutable*, quindi leggi le issue correlate 8219, 9155 e 9004. L’ambito non è ancora definito perché l’issue menziona anche probabili metodi aggiuntivi; per considerare il lavoro completato è necessario concordare l’insieme delle API interessate e apportare modifiche coerenti alle annotazioni.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Tranquilla
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 35/100