Request: an AssertingTypeGuard type for TypeGuard-like semantics
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 35/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 静か
- 技術スタック
- python
調査の方向性
このリクエストでは、実装ファイルやテストは特定されていません。まず TypeGuard の例、リンクされている Pyright issue、typeguard ライブラリに関する議論を確認し、AssertingTypeGuard を typing 仕様に含めるべきかどうかを判断したうえで、実装前に期待される narrowing と例外のセマンティクスを定義してください。
索引モデルが issue の本文から書いたものです。
説明
There is a frequent pattern of TypeGuard-like functions which assert or otherwise raise an exception if a type constraint is not met.
For example, https://github.com/microsoft/pyright/issues/2007 points to a case in which unittest provides assertIsNotNone, but a type-checker cannot infer that type narrowing has occurred. Arguably, the popular typeguard library is based around an implementation of "asserting" type guards. (One which deduces what assertions should be made from the annotations.)
TypeGuards allow for semantics like
y: str
assert is_list_of_str(x)
assert len(x) > 0
y = x[0]
An AssertingTypeGuard would allow for
y: str
assert_is_nonempty_list_of_str(x) # note, this encodes another runtime check, the len check
y = x[0]
This becomes especially valuable if we consider that you might not want to do this all with assert. I may, as an author, prefer my own custom exceptions, e.g.
def assert_is_nonempty_list_of_str(x) -> AssertingTypeGuard[list[str]]:
if not isinstance(x, list):
raise ExpectedListError(x)
if not x:
raise EmptyContainerError(x)
if not all(isinstance(y, str) for y in x):
raise ContainedInvalidTypeError(x, str)
return x
(Apologies if this repo is the wrong place to submit this request/idea. I'm happy to go through another process if necessary.)
- 主要言語
- Python
- スター
- 1.8k
- フォーク
- 302
- 平均マージ
- 23時間
- マージ済み PR(30日)
- 8
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/typing のほかの issue
-
topic: typing spec
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
topic: typing spec
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
topic: documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
topic: documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
-
topic: conformance tests topic: typing spec
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
似ている issue
-
🐛 Bug 🔔 Pending processing
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
jumpserver/jumpserver#17584 ·
-
link-check link-check:sphinx-theme
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
modelscope/DiffSynth-Studio#1702 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
OpenHands/extensions#626 · コメント 1 件 ·