Types for "truthy" and "falsy" values
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 30/100
Rechercherichtung
Es werden keine Implementierungsdateien oder Tests genannt. Beginne mit der Prüfung der truthy- und falsy-Beispiele, lies anschließend die verlinkten Vorschläge zu Intersection Types und die Leitlinien des typing-Projekts; die Arbeit ist erst abgeschlossen, wenn das Design und das erwartete Verhalten der Typprüfung vereinbart sind und die relevante typing-Unterstützung spezifiziert ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 20 Std. 26 Min.
- Gemergte PRs (30 T.)
- 10
Entwicklungsumgebung
Die Einrichtungsdateien dieses Projekts haben wir noch nicht geprüft. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/typing
-
topic: typing spec
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
python/typing#2356 · 2 Kommentare · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
-
topic: typing spec
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
Maintainer antworten meist innerhalb von 1 Tag
-
topic: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
python/typing#2227 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
topic: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
python/typing#2150 · 2 Kommentare · 2 Reaktionen ·
Maintainer antworten meist innerhalb von 1 Tag
-
topic: conformance tests topic: typing spec
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 72/100
python/typing#2351 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
solana-foundation/pay-kit#341 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
nasa/python_cmr#123 ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 92/100
EleutherAI/lm-evaluation-harness#4243 ·
Maintainer antworten meist innerhalb von 1 Tag
-
area: dashboard bug perceived difficulty: 3
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
Nitjsefnie-Harness-Commons/daedalus#1179 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
cusp-ai-oss/tojax#17 ·