python / python/mypy

Emit errors with `--disallow-any-generics` if `type[T]` or `TypeForm[T]` receives a generic `T` with missing type arguments

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

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

feature
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Feature

If --disallow-any-generics or --strict is on, I'd like mypy to emit E: Missing type parameters for generic type "list" [type-arg] when calling f(list) for the following function definitions:

  • def f[T](arg: type[T], /) -> T: ...
  • def f[T](arg: TypeForm[T], /) -> T: ...

Currently, there's no error report, and reveal_type(f(list)) shows Any.

Pitch

Due to the lack of error reporting in the above situation, implicit Anys currently easily leak when using type[T] or TypeForm[T], creating an implicit source of unsafety, which is surprising under --strict mode. Adapting the examples from PEP 747: Motivation:

# mypy: enable-incomplete-feature=TypeForm, disable-error-code=empty-body

from collections.abc import Callable
from typing_extensions import TypeForm, TypeIs

def trycast_list_item[T](typx: TypeForm[list[T]], value: object) -> T | None: ...
reveal_type(trycast_list_item(list, [1]))  # N: Revealed type is "Any | None"


def isassignable_list_item[T](value: object, typx: TypeForm[list[T]]) -> TypeIs[T]: ...

a: object
if isassignable_list_item(a, list):
    reveal_type(a)  # N: Revealed type is "Any"
else:
    reveal_type(a)  # N: Revealed type is "builtins.object"

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

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

はじめの一歩

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

調査の方向性

まず、--disallow-any-generics または --strict を指定して、reveal_type の呼び出しと TypeForm の未完成機能設定を含む 2 つのジェネリック関数の例を再現します。list を使用する呼び出しが type[T] と TypeForm[T] に対してどのように解析されるかを追跡します。完了条件は、型引数が不足していることを示すエラーが出力され、暗黙の Any が示された reveal に漏れ込まなくなることです。

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

評価

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

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

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