python / python/mypy

Bad error message surrounding generic type aliases being `Any` when it's really just unbound

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

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

bug
主要言語
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

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

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

はじめの一歩

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

調査の方向性

まず issue の再現を mypy で実行し、生のジェネリッククラス、A_Alias1、A_Alias2、foo の診断を比較します。関連する型表示とジェネリックエイリアスの処理を追跡します。診断が未束縛の型変数と Any を正確に区別し、報告された動作が各例で一貫していれば完了です。

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

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
30/100

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

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