python / python/typing

Annotating functions which don't raise exceptions

Aperta
#604 7 commenti 7 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic: feature
Lingua principale
Python
Stelle
1.8k
Fork
302
Merge medio
23h
PR unite (30g)
8

Descrizione

I would find it interesting to be able to annotate functions which never throw exceptions. "Never" can be either in the absolute sense, or in the sense of "checked exceptions", with some subset of exception designated as "unchecked" (details of this designation are perhaps up to the actual tool to process annotations).

Example of a function which absolutely never raises exceptions (well, at least per language semantics, particular implementations might still have means to break with some implementation-related exceptions):

def foo(l):
    if isinstance(l, list) and len(l) > 0:
        return l[0]

Example of a function which may throw (unchecked) exception if API contract is violated:

def foo(l: list):
    if len(l) > 0:
        return l[0]

So, hopefully these examples show that the notion does exist in Python.

Now the question how to annotate it. Following the existing NoReturn, it would be called NoRaise or NoThrow. "nothrow" terminology if familiar from C++ (and it seems to be replaced even there), so perhaps sticking with native Python terminology makes sense (but "throw" is native to Python too, re: exception handling with generators).

More interesting question is where to put that annotation. Taking the example above, a natural annotation would be:

def foo(l) -> Optional(Any), NoRaise:

And I was quite surprised that the usual "implicit tuple" syntax rule doesn't apply here, and the above is SyntaxError as of CPy3.7:

Python 3.7.1 (default, Oct 22 2018, 11:21:55) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo() -> list, int:
  File "<stdin>", line 1
    def foo() -> list, int:
                     ^
SyntaxError: invalid syntax

I wonder if that warrants a separate issue report. And whether it was a cunning design choice mere mortals need to decipher, because that seems too obvious thing to overlook with an implicit tuple syntax, again (neither problematic from forma grammar perspective, even in LL it's "->" expr ("," expr)* ":", which is trivial, Python has more complex grammar rules in many places.

So, all in all, now it would need to be written as:

def foo(l) -> (Optional(Any), NoRaise):

Which is of course not as pretty as without parens.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Non vengono indicati file del repository né test. Inizia esaminando l’annotazione NoReturn esistente e il comportamento della grammatica di Python 3.7 descritto qui, quindi chiarisci se l’obiettivo riguarda la semantica delle eccezioni, la sintassi delle annotazioni o entrambe. Il lavoro sarà completato quando saranno definiti un significato concordato per NoRaise o NoThrow e una sintassi valida specificata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.