python / python/typeshed

Replace `float` with `float | int`

Offen
#16,059 5 Kommentare 5 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

project: policy
Vorherrschende Sprache
Python
Sterne
5.1k
Forks
2.1k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
82

Beschreibung

As discussed in python/typing#1748: I propose to (slowly) replace float with float | int and complex with complex | float | int, where applicable.

  • It's more correct, even if float is current implicitly equivalent to int | float.
  • We are already prepared should the implicit promotion ever be removed.
  • We give type checkers the ability to optionally disable the promotion so that type checker authors and users can experiment with it.
  • Typeshed would always need to be the first step for this, so let's remove this blocker.

This issue proposes an ugly and slow, but correct and safe procedure:

  1. Disable flake8-pyi code Y041.
    #16096
  2. Introduce two type aliases FloatInt = float | int and ComplexInt = complex | float | int to _typeshed.
    #16068
  3. Globally replace all instances of float with FloatInt and of complex with ComplexInt in the stdlib.
  4. After 2026-09-22 we do the same for third-party stubs.
  5. Review FloatInt and ComplexInt instances are replace them with the proper types. (This will take a while.)
  6. Remove the type aliases once they are not needed anymore (in 10 years or so ...)

Using FloatInt and ComplexInt would be disallowed for new code and the type aliases should explicitly be marked as not to be used by non-typeshed code.

We should also probably add flake8-pyi checks that checks that argument types use float | int and complex | float | int. Cases where only float/complex is allowed are probably rare.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit den stdlib-Stubs und den _typeshed-Aliasen FloatInt und ComplexInt, und untersuche dann die flake8-pyi-Y041-Konfiguration. Suche in der gesamten stdlib nach float- und complex-Annotationen und prüfe jeden Kandidaten; abgeschlossen ist die Arbeit mit der geplanten stdlib-weiten Migration, während Third-Party-Stubs bis zum 2026-09-22 zurückgestellt bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Refactoring
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.