Consider contracts when evaluating Optional

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

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
25/100
Issue-Typ
Feature
Klarheit
Muss geklärt werden
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
devtools

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.

Beschreibung

feature

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.

Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
Ø Merge
1 T. 18 Std.
Gemergte PRs (30 T.)
54

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python/mypy

Alle Issues in python/mypy

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

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