python / python/mypy

Generic default type in `type` revealed as `Any`

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

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

bug topic-pep-696
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Bug Report

mypy reveals a certain nested default generic type in type as Any, while other type checkers reveal it as the default type.

This issue is motivated by pandas-dev/pandas-stubs#1764.

To Reproduce

This is complicated, see https://github.com/python/mypy/issues/21608#issuecomment-4692751812 for a much simplified example.

from typing import Any, Generic, TypeVar, reveal_type

_ItemT_co = TypeVar("_ItemT_co", default=Any, covariant=True)

class generic(Generic[_ItemT_co]): ...

_ScalarT_co = TypeVar("_ScalarT_co", bound=generic, default=Any, covariant=True)

class dtype(Generic[_ScalarT_co]): ...

_DT64ItemT_co = TypeVar(
    "_DT64ItemT_co", bound=int | None, default=int | None, covariant=True
)

class datetime64(generic[_DT64ItemT_co], Generic[_DT64ItemT_co]): ...

GenericT_co = TypeVar("GenericT_co", bound=generic, default=Any, covariant=True)
GenericT = TypeVar("GenericT", bound=generic, default=Any)

class Series(Generic[GenericT_co]):
    def to_numpy_simplified(
        self, dtype: dtype[GenericT] | type[GenericT]
    ) -> GenericT: ...

reveal_type(Series().to_numpy_simplified(datetime64))  # mypy: datetime64[Any], pyright & pyright: datetime64[int | None]

Expected Behavior

datetime64[int | None]

Actual Behavior

datetime64[Any]

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

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

はじめの一歩

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

調査の方向性

Issue にある最小化された Python の再現コードから始め、mypy を実行して reveal_type が datetime64[Any] を報告することを確認します。結果を、リンクされた pyright の例にある期待される datetime64[int | None] と比較します。mypy がこの期待どおりにジェネリックのデフォルト型を一貫して解決し、回帰テストのカバレッジが追加されれば完了です。

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

評価

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

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

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