python / python/mypy

Consider contracts when evaluating Optional

Offen
#9,947 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

While mypy considers assert to infer properties (e.g., is not None for Optional's), it would be great if it could also take contracts into account. Some properties can be easily inferred from contracts, in particular exclusivity.

Here is an example:

@icontract.ensure(lambda result: (result[0] is None) ^ (result[1] is None))
def some_func() -> Tuple[Optional[str], Optional[str]]:
    ...
    return something, errors

...

result, errors = some_func()
if errors is not None:
   ...

# We know here according to the contract that ``result`` can not be None.

The exclusivity is only but one example, and there are many more possible. I don't expect mypy to infer all the possible conclusions, but it would be already very nice to have at least some of the basic properties inferred.

There are a few contracts libraries around (e.g., icontract, for which I am the main author and maintainer, or deal) and they are gaining slowly some popularity.

I would be glad to help out on integration with icontract.

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

Keine Datei, kein Test und kein Einstiegspunkt werden genannt. Beginne damit, mypy's bestehende Optional-Einengung aus assert-Anweisungen und die vorgeschlagene icontract/deal-Integration zu untersuchen; als erledigt würde gelten, eine begrenzte Menge aus Verträgen abgeleiteter Eigenschaften wie Ausschließlichkeit zu definieren und zu testen.

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

Neue Issues direkt in Ihr Postfach

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