Types for "truthy" and "falsy" values
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 1.8k
- フォーク
- 302
- 平均マージ
- 23時間
- マージ済み PR(30日)
- 8
説明
I found this function in our codebase and I'm trying to type it:
def run_until_truthy(fn, num_tries=5):
for _ in range(num_tries):
if res := fn():
return res
Exception('Giving up.')
The function repeatedly calls a callable without arguments until it returns a "truthy" value. Then it returns that value. Easy enough:
def run_until_truthy[T](fn: Callable[[], T], num_tries: int = 5) -> T: ...
This is correct but too restrictive. Usages like this don't work:
def load_something() -> str | None:
pass
# mypy: Incompatible types in assignment (expression has type "str | None", variable has type "str")
something: str = run_until_truthy(load_something)
I can hack it to make it work:
type Falsy = Literal[None, False, 0, "", b""] | tuple[()]
def run_until_truthy[T](fn: Callable[[], T | Falsy], num_tries: int = 5) -> T: ...
Does it make sense to add official Truthy and Falsy types to the standard library?
Maybe a Truthy type would also make sense. Truthy can't be defined as a type alias of existing types, but could probably also be useful sometimes, e.g. to type the following function:
def get_falsy_values[T](seq: list[T | Truthy]) -> list[T]:
return [i for i in seq if not i]
From thinking about it for a few minutes, I think Falsy would be more often useful than Truthy, at least in combination with the current typing features.
If there's an intersection type constructor at some point, the above examples could be written like this instead, which might be easier to understand:
def run_until_truthy[T](fn: Callable[[], T], num_tries: int = 5) -> T & Truthy: ...
def get_falsy_values[T](seq: list[T]) -> list[T & Falsy]:
2024-11-25
- Updated to PEP 695 syntax.
- Using
&instead ofIntersection[]in example using proposed intersection types.
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
実装ファイルやテストは指定されていません。まず truthy と falsy の例を確認し、次にリンクされている intersection-types の提案と typing project のガイダンスを読んでください。設計と想定される型チェックの動作について合意し、関連する typing サポートを明記して初めて、作業は完了となります。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- tooling
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 30/100