python / python/mypy

Unable to make generic subclass using type variable with value restriction

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

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

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

説明

Bug Report

Mypy reports some strange false positive errors when I try to define a generic subclass of a generic class where the type variable has a value restriction.

To Reproduce

I made this minimal example to reproduce the bug:

from typing import Generic, TypeVar

T = TypeVar("T", str, int)


class GenericSuperClass(Generic[T]):
    def __init__(self, value: T) -> None:
        # mypy complains if self.value is not annotated, for some reason
        self.value: T = value


class GenericSubClass(GenericSuperClass[T]):
    def __init__(self, value: T, flag: bool) -> None:
        super().__init__(value)
        self.flag = flag


instance = GenericSubClass("string", True)
reveal_type(instance.value)

This example is a bit simple, of course, and does not make use of the value restriction. But you could imagine that there is some logic here that assumes T is either a str or an int.

Expected Behavior

I would expect the above example to not report any errors with Mypy, and I would expect Mypy to infer that self.value is of type T.

I expect the revealed type to be builtins.str.

Actual Behavior

Mypy reports the following errors:

testing_inheriting_generics.py:14: error: Argument 1 to "__init__" of "GenericSuperClass" has incompatible type "str"; expected "T"
testing_inheriting_generics.py:14: error: Argument 1 to "__init__" of "GenericSuperClass" has incompatible type "int"; expected "T"
testing_inheriting_generics.py:19: note: Revealed type is 'builtins.str*'
Found 2 errors in 1 file (checked 1 source file)

Additionally, if self.value is not annotated, Mypy reports the error testing_inheriting_generics.py:9: error: Need type annotation for 'value'

The revealed type is correct, but I believe the errors are false positives?

If I replace T = TypeVar("T", str, int) with T = TypeVar("T"), everything works as expected, but then I would not be able to assume that T is either a str or an int.

Your Environment

  • Mypy version used: mypy 0.812
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
    [mypy]
    disallow_untyped_calls = True
    warn_redundant_casts = True
    warn_return_any = True
    warn_unused_configs = True
    warn_unused_ignores = True
    
    [mypy-responses.*]
    ignore_missing_imports = True
    
  • Python version used: Python 3.9.1
  • Operating system and version: macOS Catalina 10.15.7

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

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

はじめの一歩

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

調査の方向性

testing_inheriting_generics.py の最小再現例から始め、報告されている Python 3.9 のセットアップで mypy を実行します。GenericSubClass と GenericSuperClass を通過する、値に制限された TypeVar のチェックを追跡します。完了条件は、誤検出されたコンストラクターエラーと不要な属性アノテーションエラーがなくなり、reveal_type が builtins.str を報告することです。

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

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
48/100

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

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