Types for "truthy" and "falsy" values
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 30/100
Direzione di ricerca
Non sono indicati file di implementazione né test. Inizia esaminando gli esempi truthy e falsy, poi leggi le proposte collegate sugli intersection types e le linee guida del typing project; il lavoro è completo solo dopo aver concordato il design e il comportamento previsto del controllo dei tipi e aver specificato il supporto pertinente per typing.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
I found this function in our codebase and I'm trying to type it:
def run_until_truthy(fn, num_tries=5):
for _ in range(num_tries):
if res := fn():
return res
Exception('Giving up.')
The function repeatedly calls a callable without arguments until it returns a "truthy" value. Then it returns that value. Easy enough:
def run_until_truthy[T](fn: Callable[[], T], num_tries: int = 5) -> T: ...
This is correct but too restrictive. Usages like this don't work:
def load_something() -> str | None:
pass
# mypy: Incompatible types in assignment (expression has type "str | None", variable has type "str")
something: str = run_until_truthy(load_something)
I can hack it to make it work:
type Falsy = Literal[None, False, 0, "", b""] | tuple[()]
def run_until_truthy[T](fn: Callable[[], T | Falsy], num_tries: int = 5) -> T: ...
Does it make sense to add official Truthy and Falsy types to the standard library?
Maybe a Truthy type would also make sense. Truthy can't be defined as a type alias of existing types, but could probably also be useful sometimes, e.g. to type the following function:
def get_falsy_values[T](seq: list[T | Truthy]) -> list[T]:
return [i for i in seq if not i]
From thinking about it for a few minutes, I think Falsy would be more often useful than Truthy, at least in combination with the current typing features.
If there's an intersection type constructor at some point, the above examples could be written like this instead, which might be easier to understand:
def run_until_truthy[T](fn: Callable[[], T], num_tries: int = 5) -> T & Truthy: ...
def get_falsy_values[T](seq: list[T]) -> list[T & Falsy]:
2024-11-25
- Updated to PEP 695 syntax.
- Using
&instead ofIntersection[]in example using proposed intersection types.
- Lingua principale
- Python
- Stelle
- 1.8k
- Fork
- 302
- Merge medio
- 23h
- PR unite (30g)
- 8
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
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/typing
-
topic: typing spec
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
topic: typing spec
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
topic: documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
topic: documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
-
topic: conformance tests topic: typing spec
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100