python / python/mypy

Safe cast (upcast)

Offen
#5,756 4 Kommentare 17 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature needs discussion priority-1-normal topic-casts topic-type-form
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Sometimes I want to give a hint to mypy about a type of an expression. cast() works here, but it's inherently unsafe. If I make a mistake, my program may crash at runtime. If we had a "safe cast" operation instead that would only allow casts that can't introduce runtime failures, mypy could catch some additional errors, and the use of cast() could be reduced a bit.

Here's an example derived from an example in mypy of where it would be helpful:

from typing import List, Optional
from mypy_extensions import safe_cast

def f(x: List[Optional[int]], n: int) -> List[Optional[int]]:
    # Mypy doesn't allow + for List[None] and List[Optional[int]], so we need a cast
    return safe_cast(List[Optional[int]], [None]) * n + x

In this case an alternative would be to improve type checking or type inference in mypy, but there will likely always be cases where mypy won't get thing right, and being able to perform a cast would be helpful.

Semantics of safe_cast:

  • Provides a type context for the expression (useful in the above example)
  • Requires that the expression has a type that is a "proper subtype" (mypy terminology) of the target type

At runtime safe_cast just returns the second argument, similar to cast.

More generally, if we had safe_cast, we could plausibly use binder on initial assignment (#2008), since safe_cast would be a simple workaround for cases where using binder is not desirable. Example where this would make a difference:

x: Union[int, str] = 0
reveal_type(x)  # currently Union[int, str], but perhaps should be 'int'

Now if the revealed type was int, we could use safe_cast to get the original behavior without introducing type unsafety:

x = safe_cast(Union[int, str], 0)  # Type safe!
reveal_type(x)  # Union[int, str]

Safe cast has these benefits over cast():

  • Prevents accidentally introducing an unsafe cast
  • Helps catch issues where a previously safe cast silently becomes unsafe because some types have changed
  • Makes things obvious for a reader or reviewer -- an unsafe cast always needs some care, as it could hide a real issue

Related issues: #5750 (example where this could be helpful), #5687 (safer downcasts)

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 damit, mypy's bestehendes cast-Verhalten und die zugehörigen Issues #5750, #5687 und #2008 zu überprüfen. Definiere die safe_cast-API anhand der genannten Beispiele: Sie sollte einen Typkontext bereitstellen, verlangen, dass der Ausdruck ein echter Untertyp des Zieltyps ist, und den Ausdruck zur Laufzeit unverändert zurückgeben.

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
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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