python / python/mypy

Cannot instantiate type "Type[None]"

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

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

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

説明

Bug Report

Mypy claims Cannot instantiate type "Type[None]" when you try to do so. However, you actually can do this in python, and perhaps have been able to do this ever since python 3...

This affects both types.NoneType and type(None). Also, function signatures that get at those.

To Reproduce

https://mypy-play.net/?mypy=master&python=3.12&gist=26b0ab20fc6d45de0edff4f8a374b0a7

from types import NoneType
type(None)()
NoneType()
def f(n: type[None]):
    n()
def g(n: type[NoneType]):
    n()
f(NoneType)
g(NoneType)

Actual Behavior
main.py:2: error: Cannot instantiate type "type[None]" [misc]
main.py:3: error: Cannot instantiate type "type[None]" [misc]
main.py:5: error: Cannot instantiate type "type[None]" [misc]
main.py:6: error: NoneType should not be used as a type, please use None instead [valid-type]
main.py:7: error: Cannot instantiate type "type[None]" [misc]
Found 5 errors in 1 file (checked 1 source file)

Expected Behavior
Running this python file doesn't result in any runtime problems, so I would expect no errors. (Except perhaps main.py:6: error: NoneType should not be used as a type, please use None instead [valid-type], which seems like more of a style suggstion, and so might still be valid.)

Possibly related, perhaps even fixable by this issue: https://github.com/python/mypy/issues/19308

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

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

はじめの一歩

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

調査の方向性

まず、main.py からリンクされた mypy-play の再現コードを実行し、5 つの診断がどこで生成されるかを追跡します。NoneType と type(None) の有効なインスタンス化ケースで誤ったエラーが生成されなくなれば完了です。ただし、許容可能な NoneType の型使用に関する診断は残っていてもかまいません。

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

評価

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

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

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