python / python/typing

spec/conformance: function with TypeVar default and default argument used in context.

オープン
#2,213 コメント 3 件 リアクション 5 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

topic: typing spec
主要言語
Python
スター
1.8k
フォーク
302
平均マージ
23時間
マージ済み PR(30日)
8

説明

I want to suggest adding the following test to the conformance test suite:

class Getter[T]:
    def get[S=None](self, default: S = None) -> T | S: ...  # OK

def test(arg: Getter[str]) -> None:
    result: str = arg.get()  # type: ignore
  • Since the default argument value is assignable to the default type var type, def get is well-defined
  • arg.get() implicitly uses None of type None as the argument. Therefore, we have an implicit constraint S <: None when .get is called without explicit default.

Therefore, result: str = arg.get() should raise an assignment type error (str | None not assignalbe to str).

Currently, only pyright succeeds on this test, and type checker results diverge.

  1. pyright: no errors (true negative on def get, true positive on result) (pyright playground)
  2. mypy: false positive on def get, false negative on result.
  3. ty: false positive on def get, true positive on result. (playground)
  4. pyrefly: false positive on def get, false negative on result (playground)
  5. zuban: false postive on def get, false negative on result.

Related Issues

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず typing の適合性テストスイートと、TypeVar のデフォルトおよびデフォルト引数に関する既存のテストを見つけます。提案された Getter の例を、期待される定義と代入診断とともに追加し、その後、適合性に関する期待が明確になるよう、列挙された各 type checker の結果を比較します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
testing
issue の種類
機能追加
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。