Annotating functions which don't raise exceptions
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 1.8k
- フォーク
- 302
- 平均マージ
- 23時間
- マージ済み PR(30日)
- 8
説明
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.
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リポジトリのファイルやテストは指定されていません。まず、既存の NoReturn アノテーションと、ここで説明されている Python 3.7 の文法の挙動を確認し、そのうえで目的が例外のセマンティクス、アノテーション構文、またはその両方のどれであるかを明確にしてください。NoRaise または NoThrow の合意された意味と、指定された有効な構文が定義されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 25/100