Allow `float("inf")` and `float("-inf")` in literals
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beschreibung
I have been frequently using positive and negative infinity as default values for values that should otherwise be ints. Because of Python's duck typing this is a convenient pattern that sometimes allows reducing some special-casing logic.
def less_than(value: int, limit: int | None) -> bool:
if limit is None:
return True
return value < limit
vs
def less_than(value: int, limit: int | Literal[float("inf")]) -> bool:
return value < limit
Would it be feasible to special-case the expressions float("inf") and float("-inf") and make type-checkers regard them as literal values, even though they aren't strictly speaking language-level literals?
For posterity, PEP 586 mentions this with:
Representing Literals of infinity or NaN in a clean way is tricky; real-world APIs are unlikely to vary their behavior based on a float parameter.
So I guess what I'm asking here is: am I an odd duckling or could it be worth considering +/- infinity as legal values in literal types?
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Lies zunächst die Diskussion in PEP 586 über unzulässige Literal-Parameter und die 21 Kommentare zu diesem Issue. Ermittle, ob die Typing-Community eine Entscheidung darüber getroffen hat, float("inf") und float("-inf") als Literal-Werte zu behandeln; abgeschlossen wäre die Aufgabe erst, wenn eine abgestimmte Spezifikation und die erforderlichen Änderungen an den Type-Checkern identifiziert wären, was das Issue nicht liefert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100