Bad error message surrounding generic type aliases being `Any` when it's really just unbound
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
from __future__ import annotations
from typing import Generic, TypeVar, ClassVar, Any
T = TypeVar("T")
class A(Generic[T]):
@classmethod
def bar(cls: type[A[int]]) -> None:
...
A.bar() # error: Invalid self argument "Type[A[Any]]" to attribute function "bar" with type "Callable[[Type[A[int]]], None]"
A_Alias1 = A
A_Alias1.bar() # error: Invalid self argument "Type[A[Any]]" to attribute function "bar" with type "Callable[[Type[A[int]]], None]"
reveal_type(A_Alias1) # note: Revealed type is "def [T] () -> __main__.A[T`1]"
A_Alias2 = A[Any]
A_Alias2.bar() # valid
reveal_type(A_Alias2) # note: Revealed type is "def () -> __main__.A[Any]"
def foo(a: type[A[int]]) -> None:
...
foo(A) # no error
foo(A_Alias1) # error: Argument 1 to "foo" has incompatible type "Type[A[Any]]"; expected "Type[A[int]]"
Invalid self argument "Type[A[Any]]" to attribute function "bar" with type "Callable[[Type[A[int]]], None]"
This error message is confusing, because it's implying it's Any when it's actually not, it looks like the error message is wrong.
I would think an error message like Invalid self argument "Type[A[T]]" to attribute function "bar" with type "Callable[[Type[A[int]]], None]" would be more accurate.
Also, with the foo example, why is it only complaining on the type alias and not on the raw usage? This is inconsistent with the class method example
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず issue の再現を mypy で実行し、生のジェネリッククラス、A_Alias1、A_Alias2、foo の診断を比較します。関連する型表示とジェネリックエイリアスの処理を追跡します。診断が未束縛の型変数と Any を正確に区別し、報告された動作が各例で一貫していれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 30/100