Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

Assertion and checking functions

未關閉
#703 19 則留言 2 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
5/5
預估耗時
一週以上
新手友好度
25/100
Issue 類型
功能
描述清晰度
需要釐清
活躍度
停滯
技術堆疊
python, typescript
領域
compilers

研究方向

從連結的 TypeScript 3.7 assertion-functions 文件以及 issue 中的 assert_positive 和 is_positive 範例開始。由於沒有指定 repository 檔案或測試,首先定義通用斷言和檢查註解應如何表達型別縮窄,然後記錄已達成共識的行為和所需的測試涵蓋範圍。

由索引模型根據 Issue 內容生成。

描述

topic: feature

I'll just leave this here for potential future consideration as it came up on gitter's typing-dev channel. Typescript has something called assertion functions, which enables the type checker to understand that a function does not return if a certain condition is false. I think something similar, but more generalized, would be interesting, especially for unit test functions like assert_is_instance(), assert_true() etc.

def assert_positive(x: object) -> AssertsInstance["x", int]:
    assert isinstance(x, int) and x > 0

Related, checking functions could work like this:


def is_positive(x: object) -> ChecksInstance["x", int]:
    return isinstance(x, int) and x > 0

def my_func(x: Union[int, str]) -> None:
    if is_positive(x):
        # x must be an int
        ...
    else:
        # x can be an int or str
        ...
主要語言
Python
星號
1.8k
分支
302
平均合併
20 小時 26 分鐘
30 天內合併 PR
10

環境準備

我們還沒有檢查這個專案的環境設定檔。先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

python/typing 的其他 Issue

查看 python/typing 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。